Python之非同步http庫——aiohttp
官方文件解釋: aiohttp是基於asyncio的一個非同步http客戶端和伺服器 總結官方文件:
https://
aiohttp。readthedocs。io/
en/stable/client_quickstart。html
這裡主要介紹的是aiohttp客戶端的操作
一、傳送http請求
這裡面的用法跟requests庫很相似。
get請求
import
aiohttp
import
asyncio
“”“
aiohttp:傳送http請求
1。建立一個ClientSession物件
2。透過ClientSession物件去傳送請求(get, post, delete等)
3。await 非同步等待返回結果
”“”
async
def
main
():
url
=
‘http://httpbin。org/get’
async
with
aiohttp
。
ClientSession
()
as
session
:
async
with
session
。
get
(
url
)
as
res
:
(
res
。
status
)
(
await
res
。
text
())
loop
=
asyncio
。
get_event_loop
()
task
=
loop
。
create_task
(
main
())
loop
。
run_until_complete
(
task
)
post請求
import
aiohttp
import
asyncio
“”“
aiohttp:傳送POST請求
”“”
async
def
main
():
data
=
{
‘key1’
:
‘value1’
,
‘key2’
:
‘value2’
}
url
=
‘http://httpbin。org/post’
async
with
aiohttp
。
ClientSession
()
as
session
:
async
with
session
。
post
(
url
,
data
=
data
)
as
res
:
(
res
。
status
)
(
await
res
。
text
())
loop
=
asyncio
。
get_event_loop
()
task
=
loop
。
create_task
(
main
())
loop
。
run_until_complete
(
task
)
自定義請求頭
headers
=
{
‘content-type’
:
‘image/gif’
}
session
。
post
(
url
,
data
=
data
,
headers
=
headers
)
cookie
url
=
‘http://httpbin。org/cookies’
cookies
=
{
‘cookies_are’
:
‘working’
}
async
with
ClientSession
(
cookies
=
cookies
)
as
session
:
二、傳遞引數
import
aiohttp
import
asyncio
“”“
aiohttp:傳遞引數
方式一:透過字典的形式 params = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
方式二:透過二元組的形式 params = [(‘key’, ‘value1’), (‘key’, ‘value2’)]
”“”
async
def
main
():
url
=
‘http://httpbin。org/get’
params
=
{
‘key1’
:
‘value1’
,
‘key2’
:
‘value2’
}
async
with
aiohttp
。
ClientSession
()
as
session
:
async
with
session
。
get
(
url
,
params
=
params
)
as
res
:
# http://httpbin。org/get?key1=value1&key2=value2
(
res
。
url
)
(
await
res
。
text
())
loop
=
asyncio
。
get_event_loop
()
task
=
loop
。
create_task
(
main
())
loop
。
run_until_complete
(
task
)
三、狀態碼與響應內容
import
aiohttp
import
asyncio
“”“
aiohttp:相應內容與狀態碼
狀態碼:res。status
響應內容:res。text() 或者 res。text(encoding=‘utf-8’)
二進位制內容:res。read()
json響應內容:res。json()
讀取流:res。content。read(size)
”“”
async
def
main
():
url
=
‘https://api。github。com/events’
async
with
aiohttp
。
ClientSession
()
as
session
:
async
with
session
。
get
(
url
)
as
res
:
(
res
。
status
)
(
await
res
。
text
())
(
‘**********************************’
)
(
await
res
。
read
())
loop
=
asyncio
。
get_event_loop
()
task
=
loop
。
create_task
(
main
())
loop
。
run_until_complete
(
task
)
四、流式響應內容(Streaming Response Content)
import
aiohttp
import
asyncio
“”“
res。content。read(size)
”“”
async
def
main
():
url
=
‘https://api。github。com/events’
async
with
aiohttp
。
ClientSession
()
as
session
:
async
with
session
。
get
(
url
)
as
res
:
#
(
res
。
content
)
(
await
res
。
content
。
read
())
with
open
(
‘test。txt’
,
‘wb’
)
as
fd
:
while
True
:
chunk
=
await
res
。
content
。
read
()
if
not
chunk
:
break
fd
。
write
(
chunk
)
loop
=
asyncio
。
get_event_loop
()
task
=
loop
。
create_task
(
main
())
loop
。
run_until_complete
(
task
)
五、超時設定
timeout
=
aiohttp
。
ClientTimeout
(
total
=
60
)
async
with
aiohttp
。
ClientSession
(
timeout
=
timeout
)
as
session
: