异步:asyncio和aiohttp的一些应用(2)
转自:原文链接:http://www.cnblogs.com/ssyfj/p/9222342.html
1.aiohttp的简单使用(配合asyncio模块)

import asyncio,aiohttp async def fetch_async(url):
print(url)
async with aiohttp.request("GET",url) as r:
reponse = await r.text(encoding="utf-8") #或者直接await r.read()不编码,直接读取,适合于图像等无法编码文件
print(reponse) tasks = [fetch_async('http://www.baidu.com/'), fetch_async('http://www.chouti.com/')] event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

2.发起一个session请求

import asyncio,aiohttp async def fetch_async(url):
print(url)
async with aiohttp.ClientSession() as session: #协程嵌套,只需要处理最外层协程即可fetch_async
async with session.get(url) as resp:
print(resp.status)
print(await resp.text()) #因为这里使用到了await关键字,实现异步,所有他上面的函数体需要声明为异步async tasks = [fetch_async('http://www.baidu.com/'), fetch_async('http://www.cnblogs.com/ssyfj/')] event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

除了上面的get方法外,会话还支持post,put,delete....等
session.put('http://httpbin.org/put', data=b'data')
session.delete('http://httpbin.org/delete')
session.head('http://httpbin.org/get')
session.options('http://httpbin.org/get')
session.patch('http://httpbin.org/patch', data=b'data')
不要为每次的连接都创建一次session,一般情况下只需要创建一个session,然后使用这个session执行所有的请求。
每个session对象,内部包含了一个连接池,并且将会保持连接和连接复用(默认开启)可以加快整体的性能。
3.在url中传递参数(其实与requests模块使用大致相同)
只需要将参数字典,传入params参数中即可

import asyncio,aiohttp async def func1(url,params):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
print(r.url)
print(await r.read()) tasks = [func1('https://www.ckook.com/forum.php',{"gid":6}),] event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

4.获取响应内容(由于获取响应内容是一个阻塞耗时过程,所以我们使用await实现协程切换)
(1)使用text()方法
async def func1(url,params):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
print(r.url)
print(r.charset) #查看默认编码为utf-8
print(await r.text()) #不编码,则是使用默认编码 使用encoding指定编码
(2)使用read()方法,不进行编码,为字节形式
async def func1(url,params):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
print(r.url)
print(await r.read())
(3)注意:text(),read()方法是把整个响应体读入内存,如果你是获取大量的数据,请考虑使用”字节流“(StreamResponse)
5.特殊响应内容json(和上面一样)
async def func1(url,params):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
print(r.url)
print(r.charset)
print(await r.json()) #可以设置编码,设置处理函数
6.字节流形式获取数据(不像text,read一次获取所有数据)
注意:我们获取的session.get()是Response对象,他继承于StreamResponse
async def func1(url,params):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
print(await r.content.read(10)) #读取前10字节
下面字节流形式读取数据,保存文件

async def func1(url,params,filename):
async with aiohttp.ClientSession() as session:
async with session.get(url,params=params) as r:
with open(filename,"wb") as fp:
while True:
chunk = await r.content.read(10)
if not chunk:
break
fp.write(chunk) tasks = [func1('https://www.ckook.com/forum.php',{"gid":6},"1.html"),]

注意:
async with session.get(url,params=params) as r: #异步上下文管理器
with open(filename,"wb") as fp: #普通上下文管理器
两者的区别:
在于异步上下文管理器中定义了
__aenter__和__aexit__方法

异步上下文管理器指的是在enter和exit方法处能够暂停执行的上下文管理器
为了实现这样的功能,需要加入两个新的方法:__aenter__ 和__aexit__。这两个方法都要返回一个 awaitable类型的值。
推文:异步上下文管理器async with和异步迭代器async for
7.自定义请求头(和requests一样)

async def func1(url,params,filename):
async with aiohttp.ClientSession() as session:
headers = {'Content-Type':'text/html; charset=utf-8'}
async with session.get(url,params=params,headers=headers) as r:
with open(filename,"wb") as fp:
while True:
chunk = await r.content.read(10)
if not chunk:
break
fp.write(chunk)

8.自定义cookie
注意:对于自定义cookie,我们需要设置在ClientSession(cookies=自定义cookie字典),而不是session.get()中

class ClientSession:
def __init__(self, *, connector=None, loop=None, cookies=None,
headers=None, skip_auto_headers=None,
auth=None, json_serialize=json.dumps,
request_class=ClientRequest, response_class=ClientResponse,
ws_response_class=ClientWebSocketResponse,
version=http.HttpVersion11,
cookie_jar=None, connector_owner=True, raise_for_status=False,
read_timeout=sentinel, conn_timeout=None,
timeout=sentinel,
auto_decompress=True, trust_env=False,
trace_configs=None):

使用:
cookies = {'cookies_are': 'working'}
async with ClientSession(cookies=cookies) as session:
9.获取当前访问网站的cookie
async with session.get(url) as resp:
print(resp.cookies)
10.获取网站的响应状态码
async with session.get(url) as resp:
print(resp.status)
11.查看响应头
resp.headers 来查看响应头,得到的值类型是一个dict:
resp.raw_headers 查看原生的响应头,字节类型
12.查看重定向的响应头(我们此时已经到了新的网址,向之前的网址查看)
resp.history #查看被重定向之前的响应头
13.超时处理
默认的IO操作都有5分钟的响应时间 我们可以通过 timeout 进行重写:
async with session.get('https://github.com', timeout=60) as r:
...
如果 timeout=None 或者 timeout=0 将不进行超时检查,也就是不限时长。
14.ClientSession 用于在多个连接之间(同一网站)共享cookie,请求头等

async def func1():
cookies = {'my_cookie': "my_value"}
async with aiohttp.ClientSession(cookies=cookies) as session:
async with session.get("https://segmentfault.com/q/1010000007987098") as r:
print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
async with session.get("https://segmentfault.com/hottest") as rp:
print(session.cookie_jar.filter_cookies("https://segmentfault.com"))

Set-Cookie: PHPSESSID=web2~d8grl63pegika2202s8184ct2q
Set-Cookie: my_cookie=my_value
Set-Cookie: PHPSESSID=web2~d8grl63pegika2202s8184ct2q
Set-Cookie: my_cookie=my_value
我们最好使用session.cookie_jar.filter_cookies()获取网站cookie,不同于requests模块,虽然我们可以使用rp.cookies有可能获取到cookie,但似乎并未获取到所有的cookies。


async def func1():
cookies = {'my_cookie': "my_value"}
async with aiohttp.ClientSession(cookies=cookies) as session:
async with session.get("https://segmentfault.com/q/1010000007987098") as rp:
print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
print(rp.cookies) #Set-Cookie: PHPSESSID=web2~jh3ouqoabvr4e72f87vtherkp6; Domain=segmentfault.com; Path=/ #首次访问会获取网站设置的cookie
async with session.get("https://segmentfault.com/hottest") as rp:
print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
print(rp.cookies) #为空,服务端未设置cookie
async with session.get("https://segmentfault.com/newest") as rp:
print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
print(rp.cookies) #为空,服务端未设置cookie


总结:
当我们使用rp.cookie时,只会获取到当前url下设置的cookie,不会维护整站的cookie
而session.cookie_jar.filter_cookies("https://segmentfault.com")会一直保留这个网站的所有设置cookies,含有我们在会话时设置的cookie,并且会根据响应修改更新cookie。这个才是我们需要的
而我们设置cookie,也是需要在aiohttp.ClientSession(cookies=cookies)中设置
ClientSession 还支持 请求头,keep-alive连接和连接池(connection pooling)
15.cookie的安全性
默认ClientSession使用的是严格模式的 aiohttp.CookieJar. RFC 2109,明确的禁止接受url和ip地址产生的cookie,只能接受 DNS 解析IP产生的cookie。可以通过设置aiohttp.CookieJar 的 unsafe=True 来配置:
jar = aiohttp.CookieJar(unsafe=True)
session = aiohttp.ClientSession(cookie_jar=jar)
16.控制同时连接的数量(连接池)
TCPConnector维持链接池,限制并行连接的总量,当池满了,有请求退出再加入新请求

async def func1():
cookies = {'my_cookie': "my_value"}
conn = aiohttp.TCPConnector(limit=2) #默认100,0表示无限
async with aiohttp.ClientSession(cookies=cookies,connector=conn) as session:
for i in range(7,35):
url = "https://www.ckook.com/list-%s-1.html"%i
async with session.get(url) as rp:
print('---------------------------------')
print(rp.status)

限制同时打开限制同时打开连接到同一端点的数量((host, port, is_ssl) 三的倍数),可以通过设置 limit_per_host 参数:
limit_per_host: 同一端点的最大连接数量。同一端点即(host, port, is_ssl)完全相同
conn = aiohttp.TCPConnector(limit_per_host=30)#默认是0
在协程下测试效果不明显
17.自定义域名解析地址
我们可以指定域名服务器的 IP 对我们提供的get或post的url进行解析:
from aiohttp.resolver import AsyncResolver resolver = AsyncResolver(nameservers=["8.8.8.8", "8.8.4.4"])
conn = aiohttp.TCPConnector(resolver=resolver)
18.设置代理
aiohttp支持使用代理来访问网页:
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")
19.post传递数据的方法
(1)模拟表单
payload = {'key1': 'value1', 'key2': 'value2'}
async with session.post('http://httpbin.org/post',
data=payload) as resp:
print(await resp.text())
注意:data=dict的方式post的数据将被转码,和form提交数据是一样的作用,如果你不想被转码,可以直接以字符串的形式 data=str 提交,这样就不会被转码。
(2)post json
payload = {'some': 'data'}
async with session.post(url, data=json.dumps(payload)) as resp:
其实json.dumps(payload)返回的也是一个字符串,只不过这个字符串可以被识别为json格式
(3)post 小文件
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')} await session.post(url, data=files)

url = 'http://httpbin.org/post'
data = FormData()
data.add_field('file',
open('report.xls', 'rb'),
filename='report.xls',
content_type='application/vnd.ms-excel') await session.post(url, data=data)

如果将文件对象设置为数据参数,aiohttp将自动以字节流的形式发送给服务器。
(4)post 大文件
aiohttp支持多种类型的文件以流媒体的形式上传,所以我们可以在文件未读入内存的情况下发送大文件。

@aiohttp.streamer
def file_sender(writer, file_name=None):
with open(file_name, 'rb') as f:
chunk = f.read(2**16)
while chunk:
yield from writer.write(chunk)
chunk = f.read(2**16) # Then you can use `file_sender` as a data provider: async with session.post('http://httpbin.org/post',
data=file_sender(file_name='huge_file')) as resp:
print(await resp.text())

(5)从一个url获取文件后,直接post给另一个url
r = await session.get('http://python.org')
await session.post('http://httpbin.org/post',data=r.content)
(6)post预压缩数据
在通过aiohttp发送前就已经压缩的数据, 调用压缩函数的函数名(通常是deflate 或 zlib)作为content-encoding的值:

async def my_coroutine(session, headers, my_data):
data = zlib.compress(my_data)
headers = {'Content-Encoding': 'deflate'}
async with session.post('http://httpbin.org/post',
data=data,
headers=headers)
pass

异步:asyncio和aiohttp的一些应用(2)的更多相关文章
- Python学习---IO的异步[asyncio +aiohttp模块]
aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...
- 异步:asyncio和aiohttp的一些应用(1)
1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...
- 异步网络模块之aiohttp的使用(一)
异步网络模块之aiohttp的使用(一) 平时我们也许用的更多的是requests模块,或者是requests_hml模块,但是他们都属于阻塞类型的不支持异步,速度很难提高,于是后来出现了异步的gre ...
- Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?
最近正在学习Python中的异步编程,看了一些博客后做了一些小测验:对比asyncio+aiohttp的爬虫和asyncio+aiohttp+concurrent.futures(线程池/进程池)在效 ...
- 异步网络编程aiohttp的使用
aiohttp的使用 aiohttp Asynchronous HTTP Client/Server for asyncio and Python. Supports both Client and ...
- 爬虫----异步---高性能爬虫----aiohttp 和asycio 的使用
前情提要: 首先膜拜loco大佬 肯定有人像我一样.不会异步,发一下. 一:性能比对 多进程,多线程,(这里不建议使用,太消耗性能) 进程池和线程池 (可以适当的使用) 单线程+异步协程 (推荐使 ...
- 小白学 Python 爬虫(32):异步请求库 AIOHTTP 基础入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- asyncio和aiohttp
asyncio官网 https://docs.python.org/zh-cn/3/library/asyncio-task.html 下面为伪代码: import aiohttp import as ...
- Python学习---IO的异步[asyncio模块(no-http)]
Asyncio进行异步IO请求操作: 1. @asyncio.coroutine 装饰任务函数 2. 函数内配合yield from 和装饰器@asyncio.coroutine 配合使用[固定格式 ...
随机推荐
- Elastic Load Balancing with Sticky Sessions
Elastic Load Balancing with Sticky Sessions — Shlomo Swidler https://shlomoswidler.com/2010/04/elast ...
- ora-04021 无法锁表的解决办法
案例场景: 备库上有一张分区表,在做数据导入出了点问题,需要truncate掉重新导入,在执行truncate table时发生了04021错误. 错误分析: ora-04021的解释是等待锁定对象时 ...
- Python代码样例列表
扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│ Python用户推荐系统曼哈顿算法实现.py│ ...
- 适配器模式(Adapter Pattern)--不兼容结果的协调
定义:将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper); 分类: 对象适配器:适配器与适配者之间是关联关系; 类适配器:适配器和适配者之间是继承 ...
- CSS冲突1 要关掉的css在项目内:【material-table】 中 checkbox 点击checkbox无法选中or取消,点击旁边才能选中or取消
CSS优先级: !important > 行内样式 > 内嵌样式|链接外部样式(哪个在后面哪个优先级大) id选择器 > class选择器 > 元素选择器 react中好像还不 ...
- nodejs 异步编程 教程(推荐)
有异步I/O就需要异步编程.本课程将着重讲解在学习node.js中关于异步编程的一些问题,以及如何应对这些问题,帮助node.js初学者快速入门. 地址 http://www.hubwiz.com/c ...
- html table表格列数太多添加横向滚动条
HTML的table表格的列数如果太多或者某一列的内容太长,就会导致表格td的内容被挤压变形,对后台的使用体验非常不友好.比如下面的情况: 那么如何在表格列数较多的情况下添加横向滚动条?其实很简单,只 ...
- vim中快速定位到某行以及快捷删除多行
vim filename 在命令行中直接输入 numberG 比如 100G直接定位到100行 输入 :set number即显示行号 : i,.d删除从第i行到目前所在行内容
- socketserver 并发连接
socketserver.TCPServer Example server side 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 无线路由MAC地址过滤安全可靠性讨论
无线路由MAC地址过滤安全可靠性讨论/如何实现,真的有效吗,如何防范 [内容导航] 什么是MAC地址过滤 突破MAC地址过滤步骤 捕获的无线客户端MAC地址 更改MAC地址来伪造身份 在W ...