aiohttp使用教程
aiohttp分为服务器端和客户端,本文只介绍客户端。
前面有一章文章介绍了协程
文章目录
1.1、请求用法
import asyncio
import requests
import aiohttp
async def request():
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org'
async with session.get(url) as response:
print(response.status)
print(response.text())
tasks = [asyncio.ensure_future(request()) for _ in range(5)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
- 其中
response.text()
可以在括号内指定解码方式,编码方式。 - 其中
response.read()
适合读取图像,是无法编码
- 第五行:创建一个
CelientSession
对象命名为session
。 - 第七行:通过
session
的get
方法得到一个ClientResponse
对象,命名为resp
。
除了get
请求还有post
请求
- session.post(‘http://httpbin.org/post’, data=‘data’)
**注意:**不要为每次链接都创建一次session
,一般情况下要创建一个session
,然后使用这个session
执行所有请求。
每个session
对象,内部创建一个连接池,并且会将保持连接和连接复用默认开始
可以加快整体性能。
1.2 在URL中传递参数
async with aiohttp.ClientSession() as session:
params = {'a':1,'b':2,'c':3}
url = 'http://httpbin.org/get'
async with session.get(url,params=params) as response:
print(response.url)
1.3、相应内容
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/get'
async with session.get(url) as response:
print(await response.text())
await response.text(encoding='gb2312')
方法,会将服务器端返回的内容进行解码,可以指定编码方式。
await response.read()
方法,获取的类型是字节的内容。
其中text() read()
是将相体毒读入内存,如果是获取大量数据,使用字节流streaming response
。
1.3、特殊响应内容:json
async with aiohttp.ClientSession() as session:
url = 'https://api.github.com/events'
async with session.get(url) as response:
print(await response.json())
内置的方法处理json
1.4、字节流读取响应内容
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/get'
async with session.get(url) as response:
with open('demo.txt','wb') as f:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
f.write(chunk)
如果是向下载打大内容的文件,上面的方式是直接向响应体读入到内存,用字节流控制字节数来控制读入内存的响应内容。
1.5、自定义头
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/post'
data = {'a':1,'b':2}
headers = {'content-type': 'application/json'}
async with session.post(url,data=json.dumps(data),headers=headers) as response:
print(await response.text())
1.6、自定义Cookie
async def request():
url = 'http://httpbin.org/cookies'
cookies = {'cookie_demo':'demo'}
async with aiohttp.ClientSession(cookies=cookies) as session:
async with session.get(url) as response:
print(await response.json())
1.7、post数据的方式
1.模拟表单post数据
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/post'
payload = {'key1': 'value1', 'key2': 'value2'}
async with session.post(url,data=payload) as response:
print(await response.text())
注意:data=dict
方式post
数据将被转码,和form
提交数据一样作用,如果不想被转码,可以直接以字符串形式data=str
提交。
2.post JSON
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/post'
data = {'a':1,'b':2}
headers = {'content-type': 'application/json'}
async with session.post(url,data=json.dumps(data),headers=headers) as response:
print(await response.text())
3.post 小文件
async with aiohttp.ClientSession() as session:
url = 'http://httpbin.org/post'
data = aiohttp.FormData()
data.add_field('file',
open('demo.txt', 'rb'),
filename = 'demo.txt',
content_type='application/vnd.ms-excel',
)
async with session.post(url,data=data) as response:
print(await response.text())
files = {'file': open('report.xls', 'rb')}
await session.post(url, data=files)
将文件对象设置为数据参数,aiohttp将自动以字节流的形式发送给服务器。
1.8、设置代理
async with aiohttp.ClientSession() as session:
async with session.get("http://python.org",
proxy="http://some.proxy.com") as resp:
print(resp.status)
支持需要授权的页面
async with aiohttp.ClientSession() as session:
proxy_auth = aiohttp.BasicAuth('user', 'pass')
async with session.get("http://python.org",
proxy="http://some.proxy.com",
proxy_auth=proxy_auth) as resp:
print(resp.status)
或者
session.get("http://python.org",
proxy="http://user:pass@some.proxy.com")
1.9返回内容查看
- resp.status:查看状态码
- resp.headers:查看headers
- resp.cookie:查看cookie
- 超时处理
session.get('http://httpbin.org/get', timeout=60)