- 浏览: 2567811 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Python Library 2019(1)requests and aiohttp
Try AIOHttp
> mkdir aiohttp
> cd aiohttp/
Check Version
> python -V
Python 3.7.5
> pip -V
pip 19.3.1 from /Users/hluo/.pyenv/versions/3.7.5/lib/python3.7/site-packages/pip (python 3.7)
> pip install aiohttp
> pip install cchardet
> pip install aiodns
Write a simple HTTP Server
> cat web.py
import asyncio
from aiohttp import web
routes = web.RouteTableDef()
@routes.get('/')
async def index(request):
await asyncio.sleep(2)
return web.json_response({
'name': 'index'
})
@routes.get('/about')
async def about(request):
await asyncio.sleep(0.5)
return web.Response(text="<h1>about us</h1>")
def init():
app = web.Application()
app.add_routes(routes)
web.run_app(app)
init()
Start the server, but it complains in 3.7.x python version
> python web.py
Downgrade the python version to 3.6.x
> pyenv local 3.6.8
> python -V
Python 3.6.8
Start the server again
> python web.py
Then we can visit these URLs
http://localhost:8080/about
http://localhost:8080/
Check library requests
> pip install requests
Get Sample
>>> import requests
>>> playload={'page':'1','per_page':'10'}
>>> r = requests.get("http://httpbin.org/get", params=playload)
>>> print(r.url)
http://httpbin.org/get?page=1&per_page=10
Post the JSON information
>>> import json
>>> import requests
>>> playload={'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", data=json.dumps(playload))
>>> print(r.text)
{
"args": {},
"data": "{\"page\": 1, \"per_page\": 10}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "27",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"page": 1,
"per_page": 10
},
"origin": "207.114.244.5, 207.114.244.5",
"url": "https://httpbin.org/post"
}
Alternative way to do the JSON
>>> import requests
>>> playload = { 'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", json=playload)
>>> print(r.text)
Add to the Headers
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> payload = { 'page': 1, 'per_page': 10 }
>>> headers = { 'User-Agent': 'Python Script' }
>>> r = requests.post(url, json=payload, headers=headers)
>>> print(r.request.headers)
{'User-Agent': 'Python Script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '27', 'Content-Type': 'application/json'}
>>> print(r.headers)
{'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Mon, 16 Dec 2019 20:04:28 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '264', 'Connection': 'keep-alive'}
Working on Response
>>> import requests
>>> r = requests.get("http://httpbin.org/get")
>>> print(r.status_code)
200
Get JSON response
>>> import requests
>>> r = requests.get("https://github.com/timeline.json")
>>> print(r.json())
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
Session to Keep the Cookie
>>> import requests
>>> s = requests.Session()
>>> s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
<Response [200]>
>>> r = s.get("http://httpbin.org/cookies")
>>> print(r.text)
{
"cookies": {
"sessioncookie": "123456789"
}
}
Session help send Default Parameters
>>> import requests
>>> s = requests.Session()
>>> s.auth = ('user','password')
>>> s.headers.update({'api-key':'adsfsadfsdf'})
>>> r = s.get('http://httpbin.org/headers', headers={'User-Agent':'python script'})
>>> print(r.request.headers)
{'User-Agent': 'python script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'api-key': 'adsfsadfsdf', 'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}
References:
https://www.jianshu.com/p/acb6a1b95fcb
https://aiohttp.readthedocs.io/en/stable/
Try AIOHttp
> mkdir aiohttp
> cd aiohttp/
Check Version
> python -V
Python 3.7.5
> pip -V
pip 19.3.1 from /Users/hluo/.pyenv/versions/3.7.5/lib/python3.7/site-packages/pip (python 3.7)
> pip install aiohttp
> pip install cchardet
> pip install aiodns
Write a simple HTTP Server
> cat web.py
import asyncio
from aiohttp import web
routes = web.RouteTableDef()
@routes.get('/')
async def index(request):
await asyncio.sleep(2)
return web.json_response({
'name': 'index'
})
@routes.get('/about')
async def about(request):
await asyncio.sleep(0.5)
return web.Response(text="<h1>about us</h1>")
def init():
app = web.Application()
app.add_routes(routes)
web.run_app(app)
init()
Start the server, but it complains in 3.7.x python version
> python web.py
Downgrade the python version to 3.6.x
> pyenv local 3.6.8
> python -V
Python 3.6.8
Start the server again
> python web.py
Then we can visit these URLs
http://localhost:8080/about
http://localhost:8080/
Check library requests
> pip install requests
Get Sample
>>> import requests
>>> playload={'page':'1','per_page':'10'}
>>> r = requests.get("http://httpbin.org/get", params=playload)
>>> print(r.url)
http://httpbin.org/get?page=1&per_page=10
Post the JSON information
>>> import json
>>> import requests
>>> playload={'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", data=json.dumps(playload))
>>> print(r.text)
{
"args": {},
"data": "{\"page\": 1, \"per_page\": 10}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "27",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"page": 1,
"per_page": 10
},
"origin": "207.114.244.5, 207.114.244.5",
"url": "https://httpbin.org/post"
}
Alternative way to do the JSON
>>> import requests
>>> playload = { 'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", json=playload)
>>> print(r.text)
Add to the Headers
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> payload = { 'page': 1, 'per_page': 10 }
>>> headers = { 'User-Agent': 'Python Script' }
>>> r = requests.post(url, json=payload, headers=headers)
>>> print(r.request.headers)
{'User-Agent': 'Python Script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '27', 'Content-Type': 'application/json'}
>>> print(r.headers)
{'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Mon, 16 Dec 2019 20:04:28 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '264', 'Connection': 'keep-alive'}
Working on Response
>>> import requests
>>> r = requests.get("http://httpbin.org/get")
>>> print(r.status_code)
200
Get JSON response
>>> import requests
>>> r = requests.get("https://github.com/timeline.json")
>>> print(r.json())
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
Session to Keep the Cookie
>>> import requests
>>> s = requests.Session()
>>> s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
<Response [200]>
>>> r = s.get("http://httpbin.org/cookies")
>>> print(r.text)
{
"cookies": {
"sessioncookie": "123456789"
}
}
Session help send Default Parameters
>>> import requests
>>> s = requests.Session()
>>> s.auth = ('user','password')
>>> s.headers.update({'api-key':'adsfsadfsdf'})
>>> r = s.get('http://httpbin.org/headers', headers={'User-Agent':'python script'})
>>> print(r.request.headers)
{'User-Agent': 'python script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'api-key': 'adsfsadfsdf', 'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}
References:
https://www.jianshu.com/p/acb6a1b95fcb
https://aiohttp.readthedocs.io/en/stable/
发表评论
-
Stop Update Here
2020-04-28 09:00 332I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 493NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 378Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 381Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 351Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 440Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 450Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 392Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 475VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 401Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 496NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 440Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 346Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 262GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 463GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 336GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 322Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 331Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 321Serverless with NodeJS and Tenc ...
相关推荐
在Python的世界里,库(Library)是预编写好的代码集合,它们提供了各种功能,如数据处理、网络通信、图形界面等。开发者可以借助这些库快速构建应用,而无需从零开始编写所有基础功能。"improf" 这个库可能专注于某...
- **Pillow**:Python Imaging Library的友好分支,支持多种图像处理功能。 - **OpenCV-Python**:OpenCV的Python接口,提供了丰富的图像处理能力。 #### Implementations(实现) - **Cython**:将Python代码转换...
PIL(Python Imaging Library)或它的分支Pillow库可以帮助检查图片格式,并进行必要的转换。 7. **反爬虫策略**:网站可能会有反爬虫措施,如IP限制、验证码等。我们可以使用代理IP池、随机User-Agent、延时策略等...
1. **安装库**: 首先,你需要确保你的Python环境中有`requests`库,它是用来发送HTTP请求的常用库。如果尚未安装,可以通过`pip install requests`命令进行安装。 2. **API调用**: 使用`requests`库向API发送GET...
3. **图片处理**:下载下来的图片可能需要进一步处理,例如调整尺寸、转换格式等,这可能涉及到了PIL(Python Imaging Library)或者OpenCV库。 4. **文件操作**:在保存图片时,需要进行文件I/O操作,Python的内置...
它支持多种HTTP客户端库,如`requests`和`aiohttp`,以便在同步和异步环境中使用。 ### 6. Swagger支持 虽然OpenAPI是规范,但Swagger是其最知名的实现之一,提供了一套工具集。`openapi-core` 可以与Swagger UI...
Python有PIL(Python Imaging Library)和OpenCV等库,用于读取、修改和处理图像。在Image-Webscrapper中,我们可能需要用到这些库来下载图片并保存到本地。 项目结构通常会包含以下几个部分: 1. **配置文件**:...