异步http://client/server框架(asyncio)
aiohttp的Python项目详细描述
异步http客户端/服务器框架
主要功能
- 支持HTTP协议的客户端和服务器端。
- 支持现成的客户端和服务器Web套接字,并避免 回拨地狱。
- 为Web服务器提供中间件和可插拔路由。
入门
客户机
从网上获取信息:
importaiohttpimportasyncioasyncdeffetch(session,url):asyncwithsession.get(url)asresponse:returnawaitresponse.text()asyncdefmain():asyncwithaiohttp.ClientSession()assession:html=awaitfetch(session,'http://python.org')print(html)if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(main())
服务器
使用简单服务器的示例:
# examples/server_simple.pyfromaiohttpimportwebasyncdefhandle(request):name=request.match_info.get('name',"Anonymous")text="Hello, "+namereturnweb.Response(text=text)asyncdefwshandle(request):ws=web.WebSocketResponse()awaitws.prepare(request)asyncformsginws:ifmsg.type==web.WSMsgType.text:awaitws.send_str("Hello, {}".format(msg.data))elifmsg.type==web.WSMsgType.binary:awaitws.send_bytes(msg.data)elifmsg.type==web.WSMsgType.close:breakreturnwsapp=web.Application()app.add_routes([web.get('/',handle),web.get('/echo',wshandle),web.get('/{name}',handle)])web.run_app(app)
通信信道
aio libs 谷歌集团: https://groups.google.com/forum/!论坛/aio libs
请在这里自由发表您的问题和想法。
Gitter聊天s://gitter.im/aio-libs/lobby" rel="nofollow">https://gitter.im/aio-libs/lobby
我们支持堆栈溢出。 请在您的问题中添加 aiohttp 标记。
许可证
aiohttp 在apache 2许可下提供。
纪念品
aiohttp社区要感谢keepsafe ( https://www.getkeepsafe.com )早期支持 项目。
源代码
github存储库中提供了最新的开发人员版本: https://github.com/aio-libs/aiohttp
基准
如果您对效率感兴趣,Asyncio社区将维护 官方wiki上的基准列表: https://github.com/python/asyncio/wiki/benchmarks