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參數中即可[code]import asyncio,aiohttp
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:
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

