aiohttp
3.11.10
若要從網路取得某些內容:
import aiohttp
import asyncio
async def main ():
async with aiohttp . ClientSession () as session :
async with session . get ( 'http://python.org' ) as response :
print ( "Status:" , response . status )
print ( "Content-type:" , response . headers [ 'content-type' ])
html = await response . text ()
print ( "Body:" , html [: 15 ], "..." )
asyncio . run ( main ())
這列印:
狀態:200 內容類型:text/html;字符集=utf-8 正文: <!doctype html> ...
來自請求?了解為什麼我們需要這麼多行。
使用簡單伺服器的範例:
# examples/server_simple.py
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 . WSMsgType . text :
await ws . send_str ( "Hello, {}" . format ( msg . data ))
elif msg . type == web . WSMsgType . binary :
await ws . send_bytes ( msg . data )
elif msg . type == web . WSMsgType . close :
break
return ws
app = web . Application ()
app . add_routes ([ web . get ( '/' , handle ),
web . get ( '/echo' , wshandle ),
web . get ( '/{name}' , handle )])
if __name__ == '__main__' :
web . run_app ( app )
https://aiohttp.readthedocs.io/
https://github.com/aio-libs/aiohttp-demos
請隨意提出請求以將連結添加到這些頁面!
aio-libs 討論:https://github.com/aio-libs/aiohttp/discussions
矩陣:#aio-libs:matrix.org
我們支援堆疊溢位。請在您的問題中加入aiohttp標籤。
您也可以選擇安裝 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