Github: https://github.com/aio-libs/aiohttp
非同期HTTPクライアント/サーバーフレームワーク
主な特徴
- HTTPプロトコルのクライアント側とサーバー側の両方をサポートします。
- コールバック地獄なしですぐにクライアントとサーバーの両方のWebソケットをサポートします。
- Webサーバーにはミドルウェアとプラガブルルーティングがあります。
入門
クライアント
ウェブから何かを取得するには:
import aiohttp
import asyncio
import async_timeout
async def fetch(session, url):
async with async_timeout.timeout(10):
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://python.org')
print(html)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
サーバ
これは簡単な使用例です:
from aiohttp import web
async def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(text=text)
async def wshandle(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == web.MsgType.text:
await ws.send_str("Hello, {}".format(msg.data))
elif msg.type == web.MsgType.binary:
await ws.send_bytes(msg.data)
elif msg.type == web.MsgType.close:
break
return ws
app = web.Application()
app.add_routes([web.get('/', handle),
web.get('/echo', wshandle),
web.get('/{name}', handle)])
web.run_app(app)
ドキュメンテーション
https://aiohttp.readthedocs.io/
外部リンク
これらのページにあなたのリンクを追加するためのプルリクエストをお気軽に!
コミュニケーションチャンネル
aio-libs Googleグループ: https : //groups.google.com/forum/#!forum/aio-libs
質問やアイディアをここに投稿してください。
gitter chat https://gitter.im/aio-libs/Lobby
スタックオーバーフローをサポートしています。 あなたの質問にaiohttpタグを追加してください。
要件
オプションで、 cChardetおよびaiodnsライブラリをインストールすることもできます(高速化のために強く推奨されます)。
ライセンス
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