- 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kun1280437633/article/details/80685334
aiohttp 介绍
aiohttp是什么,官网上有这样一句话介绍:Async HTTP client/server for asyncio and Python
,翻译过来就是 asyncio和Python的异步HTTP客户端/服务器
主要特点是:
支持客户端和HTTP服务器。
无需使用Callback Hell即可支持Server WebSockets和Client WebSockets。
Web服务器具有中间件,信号和可插拔路由。
emmmm,好吧,还是来看代码吧
Client example:
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://httpbin.org/headers')
print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
output:
{ "headers":{ "Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close",