在 Python 众多的 HTTP 客户端中,最有名的莫过于 、 和 。requests
aiohttp
httpx
在不借助其他第三方库的情况下, 只能发送同步请求; 只能发送异步请求; 既能发送同步请求,又能发送异步请求。requests
aiohttp
httpx
那么怎么选择呢
- 只发同步请求用 ,但可配合多线程变异步。
requests
- 只发异步请求用 ,但可以配合await变同步。
aiohttp
httpx
可以发同步请求也可以异步,但是请求速度同步略差于 ,异步略差于requests
aiohttp
这里不建议使用多线程来做异步请求,建议使用异步IO的方式。
asyncio
的优势:
-
可以异步请求。
-
可以普通请求也可以作为WS客户端连接。
-
可以作为WEB服务器和WEBSOCKET服务器。
-
性能较好。
安装依赖
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>pip</strong></span> install aiohttp
</span></span></span></span>
客户端
默认超时时间
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6">aiohttp.ClientTimeout(
total=<span style="color:#880000">5</span>*<span style="color:#880000">60</span>,
connect=<span style="color:#78a960">None</span>,
sock_connect=<span style="color:#78a960">None</span>,
sock_read=<span style="color:#78a960">None</span>
)
</span></span></span></span>
获取请求
基本请求
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>import</strong></span> aiohttp
<span style="color:#333333"><strong>import</strong></span> asyncio
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>main</strong></span>():
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>with</strong></span> aiohttp.ClientSession() <span style="color:#333333"><strong>as</strong></span> session:
params = {<span style="color:#880000">'key1'</span>: <span style="color:#880000">'value1'</span>, <span style="color:#880000">'key2'</span>: <span style="color:#880000">'value2'</span>}
resp = <span style="color:#333333"><strong>await</strong></span> session.get(
<span style="color:#880000">'https://www.psvmc.cn/login.json'</span>,
params=params
)
result = <span style="color:#333333"><strong>await</strong></span> resp.text()
result2 = <span style="color:#333333"><strong>await</strong></span> resp.json()
print(result)
print(result2)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
</span></span></span></span>
获取状态码
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>import</strong></span> aiohttp
<span style="color:#333333"><strong>import</strong></span> asyncio
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>main</strong></span>():
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>with</strong></span> aiohttp.ClientSession() <span style="color:#333333"><strong>as</strong></span> session:
params = {<span style="color:#880000">'key1'</span>: <span style="color:#880000">'value1'</span>, <span style="color:#880000">'key2'</span>: <span style="color:#880000">'value2'</span>}
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>with</strong></span> session.get(
<span style="color:#880000">'https://www.psvmc.cn/login.json'</span>,
params=params
)<span style="color:#333333"><strong>as</strong></span> resp:
print(resp.status)
print(<span style="color:#333333"><strong>await</strong></span> resp.text())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
</span></span></span></span>
文件下载
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>import</strong></span> aiohttp
<span style="color:#333333"><strong>import</strong></span> asyncio
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>main</strong></span>():
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>with</strong></span> aiohttp.ClientSession() <span style="color:#333333"><strong>as</strong></span> session:
params = {<span style="color:#880000">'key1'</span>: <span style="color:#880000">'value1'</span>, <span style="color:#880000">'key2'</span>: <span style="color:#880000">'value2'</span>}
<span style="color:#333333"><strong>async</strong></span> <span style="color:#333333"><strong>with</strong></span> session.get(
<span style="color:#880000">'https://www.psvmc.cn/search.json'</span>,
params=params
)<span style="color:#333333"><strong>as</strong></span> resp:
filename = <span style="color:#880000">"D://search.json"</span>
chunk_size = <span style="color:#880000">1000</span>
<span style="color:#333333"><strong>with</strong></span> open(filename, <span style="color:#880000">'wb'</span>) <span style="color:#333333"><strong>as</strong></span> fd:
<span style="color:#333333"><strong