{"id":99774,"date":"2024-04-12T11:47:53","date_gmt":"2024-04-12T03:47:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99774.html"},"modified":"2024-04-12T11:47:54","modified_gmt":"2024-04-12T03:47:54","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e4%bd%bf%e7%94%a8aiohttp%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/99774.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528AIOHTTP\u5e93"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02155016\/c265678c-0c4c-467d-836b-76ef76576bf6.webp\" alt=\"\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528AIOHTTP\u5e93\" \/><\/p>\n<p><p><a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>OHTTP\u662f\u4e00\u4e2a\u7528\u4e8e\u5f02\u6b65\u8bf7\u6c42\u7684Python\u5e93\uff0c\u5b83\u4f7f\u7528<code>asyncio<\/code>\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u3002<strong>\u8981\u5728Python\u4e2d\u4f7f\u7528AIOHTTP\u5e93\uff0c\u9996\u5148\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5AIOHTTP<\/strong>\u3001<strong>\u7136\u540e\u5bfc\u5165\u76f8\u5173\u7684\u7ec4\u4ef6<\/strong>\uff0c\u5982<code>ClientSession<\/code>\u7528\u4e8e\u521b\u5efa\u5ba2\u6237\u7aef\u5b9e\u4f8b\u3001<code>aiohttp.web<\/code>\u7528\u4e8e\u521b\u5efaWeb\u670d\u52a1\u5668\u3002\u5728AIOHTTP\u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u57fa\u4e8e\u5f02\u6b65\u7684\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5<strong>\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u5904\u7406\u6570\u767e\u751a\u81f3\u6570\u5343\u4e2a\u5e76\u53d1\u8fde\u63a5<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5\u548c\u7b80\u5355\u4f7f\u7528AIOHTTP<\/strong>\u662f\u4e00\u4e2a\u6bd4\u8f83\u76f4\u63a5\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u6267\u884c<code>pip install aiohttp<\/code>\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c06\u5176\u5b89\u88c5\u5230\u60a8\u7684Python\u73af\u5883\u4e2d\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5f00\u59cb\u521b\u5efa\u7b80\u5355\u7684HTTP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002\u4f7f\u7528<code>async<\/code>\u548c<code>await<\/code>\u5173\u952e\u5b57\u6765\u7f16\u6392\u5f02\u6b65\u7684HTTP\u8c03\u7528\uff0c\u662f\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u7684\u5173\u952e\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001AIOHTTP \u5e93\u5b89\u88c5\u53ca\u5176\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5AIOHTTP<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5AIOHTTP\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u8981\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install aiohttp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u4e86AIOHTTP\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>ClientSession<\/code>\u5b9e\u4f8b\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(session, url):<\/p>\n<p>    async with session.get(url) as response:<\/p>\n<p>        return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        html = await fetch(session, &#039;http:\/\/python.org&#039;)<\/p>\n<p>        print(html)<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001AIOHTTP\u5f02\u6b65\u8bf7\u6c42\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f97\u76ca\u4e8e\u5f02\u6b65IO\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u591a\u4e2aHTTP\u8bf7\u6c42\u3002\u65e0\u9700\u7b49\u5f85\u4e00\u4e2a\u8bf7\u6c42\u5b8c\u6210\u5c31\u80fd\u542f\u52a8\u53e6\u4e00\u4e2a\uff0c\u8fd9\u6781\u5927\u63d0\u9ad8\u4e86\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5f02\u6b65\u8fdb\u884c\u591a\u4e2a\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u540c\u65f6\u53d1\u9001\u591a\u4e2aHTTP\u8bf7\u6c42\u65f6\uff0cAIOHTTP\u80fd\u591f\u901a\u8fc7\u5e76\u53d1\u6267\u884c\u6765\u8282\u7701\u5927\u91cf\u65f6\u95f4\u3002\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(session, url):<\/p>\n<p>    async with session.get(url) as response:<\/p>\n<p>        return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        tasks = [fetch(session, &#039;http:\/\/python.org&#039;) for _ in range(10)]<\/p>\n<p>        results = await asyncio.gather(*tasks)<\/p>\n<p>        for result in results:<\/p>\n<p>            print(result[:100])  # \u6253\u5370\u6bcf\u4e2a\u9875\u9762\u7684\u524d100\u4e2a\u5b57\u7b26<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u591a\u4e2a\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528<code>asyncio.gather<\/code>\u6536\u96c6\u5e76\u53d1\u6267\u884c\u7684\u7ed3\u679c<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001AIOHTTP \u521b\u5efaWeb\u670d\u52a1<\/h3>\n<\/p>\n<p><p>AIOHTTP\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u5f02\u6b65\u7684Web\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u7b80\u5355\u7684\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\u9700\u8981\u4f7f\u7528<code>aiohttp.web.Application<\/code>\u7c7b\u53ca\u76f8\u5173\u7684\u8def\u7531\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7b80\u5355HTTP\u54cd\u5e94\u7684\u670d\u52a1\u5668\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from aiohttp import web<\/p>\n<p>async def handle(request):<\/p>\n<p>    return web.Response(text=&quot;Hello, world&quot;)<\/p>\n<p>app = web.Application()<\/p>\n<p>app.router.add_get(&#039;\/&#039;, handle)<\/p>\n<p>web.run_app(app, host=&#039;127.0.0.1&#039;, port=8080)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u542f\u52a8\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u670d\u52a1\u5668\u5c06\u76d1\u542c\u672c\u5730\u76848080\u7aef\u53e3\uff0c\u5e76\u4e14\u5bf9\u6839\u8def\u5f84\u8bf7\u6c42\u54cd\u5e94&#039;Hello, world&#039;<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001AIOHTTP \u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p>AIOHTTP\u4e0d\u4ec5\u63d0\u4f9b\u57fa\u672c\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u5904\u7406\uff0c\u8fd8\u6709\u5f88\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u652f\u6301WebSocket\u3001\u6d41\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u7b49\u3002<\/p>\n<\/p>\n<p><h4>WebSocket\u652f\u6301<\/h4>\n<\/p>\n<p><p>AIOHTTP\u5177\u6709\u5185\u7f6e\u7684WebSocket\u652f\u6301\uff0c\u4f7f\u5f97\u521b\u5efaWebSocket\u670d\u52a1\u5668\u53d8\u5f97\u5bb9\u6613\u3002\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from aiohttp import web<\/p>\n<p>async def websocket_handler(request):<\/p>\n<p>    ws = web.WebSocketResponse()<\/p>\n<p>    await ws.prepare(request)<\/p>\n<p>    async for msg in ws:<\/p>\n<p>        if msg.type == aiohttp.WSMsgType.TEXT:<\/p>\n<p>            await ws.send_str(&quot;Hello, &quot; + msg.data)<\/p>\n<p>        elif msg.type == aiohttp.WSMsgType.ERROR:<\/p>\n<p>            print(&#039;WebSocket connection closed with exception %s&#039; % ws.exception())<\/p>\n<p>    return ws<\/p>\n<p>app = web.Application()<\/p>\n<p>app.router.add_get(&#039;\/websocket&#039;, websocket_handler)<\/p>\n<p>web.run_app(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6d41\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u6216\u6d41\u5a92\u4f53\u65f6\uff0c\u6d41\u5904\u7406\u529f\u80fd\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002AIOHTTP\u63d0\u4f9b\u4e86\u6d41\u5f0f\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u9010\u5757\u8bfb\u53d6\u548c\u53d1\u9001\u54cd\u5e94\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def stream_handler(request):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(&#039;http:\/\/python.org&#039;) as response:<\/p>\n<p>            while True:<\/p>\n<p>                chunk = await response.content.read(1024)<\/p>\n<p>                if not chunk:<\/p>\n<p>                    break<\/p>\n<p>                yield chunk<\/p>\n<p>app = web.Application()<\/p>\n<p>app.router.add_get(&#039;\/stream&#039;, stream_handler)<\/p>\n<p>web.run_app(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528AIOHTTP\u5e93\u8fdb\u884c\u5f02\u6b65HTTP\u8bf7\u6c42\u548cWeb\u670d\u52a1\u5668\u7684\u521b\u5efa\u3002\u7406\u89e3\u548c\u638c\u63e1AIOHTTP\u7684\u57fa\u672c\u6982\u5ff5\u548c\u9ad8\u7ea7\u529f\u80fd\u5bf9\u4e8e\u5f00\u53d1\u73b0\u4ee3\u9ad8\u6027\u80fdWeb\u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u5f02\u6b65\u7f16\u7a0b\uff0c\u60a8\u7684\u5e94\u7528\u53ef\u4ee5\u627f\u8f7d\u66f4\u591a\u7684\u5e76\u53d1\u7528\u6237\uff0c\u540c\u65f6\u4fdd\u6301\u54cd\u5e94\u8fc5\u901f\uff0c\u63d0\u4f9b\u4f18\u8d28\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. AIOHTTP\u5e93\u662f\u4ec0\u4e48\uff1f\u5b83\u80fd\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e94\u7528\u573a\u666f\uff1f<\/strong><\/p>\n<p>AIOHTTP\u662f\u4e00\u4e2a\u57fa\u4e8e\u534f\u7a0b\u7684\u5f02\u6b65HTTP\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668\u5e93\uff0c\u9002\u7528\u4e8e\u5728Python\u4e2d\u8fdb\u884c\u9ad8\u6548\u7684\u7f51\u7edc\u901a\u4fe1\u3002\u5b83\u9002\u7528\u4e8e\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u4f8b\u5982\u7f16\u5199\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\u3001\u722c\u866b\u548c\u4e0e\u5916\u90e8API\u8fdb\u884c\u5f02\u6b65\u901a\u4fe1\u7b49\u3002\u8be5\u5e93\u7684\u5f02\u6b65\u7279\u6027\u4f7f\u5f97\u5b83\u80fd\u591f\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\uff0c\u63d0\u4f9b\u4e86\u66f4\u5feb\u7684\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u548c\u5bfc\u5165AIOHTTP\u5e93\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u4e2d\u4f7f\u7528AIOHTTP\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<pre><code>pip install aiohttp\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u8be5\u5e93\uff1a<\/p>\n<pre><code class=\"language-python\">import aiohttp\n<\/code><\/pre>\n<p>\u73b0\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528AIOHTTP\u5e93\u4e2d\u63d0\u4f9b\u7684\u5404\u79cd\u529f\u80fd\u548c\u7c7b\u6765\u5904\u7406\u5f02\u6b65HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528AIOHTTP\u5e93\u53d1\u9001\u4e00\u4e2a\u5f02\u6b65GET\u8bf7\u6c42\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528AIOHTTP\u5e93\u53d1\u9001\u5f02\u6b65GET\u8bf7\u6c42\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import aiohttp\nimport asyncio\n\nasync def fetch(url):\n    async with aiohttp.ClientSession() as session:\n        async with session.get(url) as response:\n            return await response.text()\n\nasync def main():\n    url = &#039;http:\/\/api.example.com&#039;\n    response = await fetch(url)\n    print(response)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(main())\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>fetch<\/code>\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u5b83\u4f7f\u7528<code>aiohttp.ClientSession<\/code>\u6765\u53d1\u9001GET\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u8c03\u7528<code>fetch<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u54cd\u5e94\uff0c\u5e76\u5728\u63a7\u5236\u53f0\u6253\u5370\u51fa\u6765\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u6765\u8fd0\u884c<code>main<\/code>\u51fd\u6570\u5e76\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"AIOHTTP\u662f\u4e00\u4e2a\u7528\u4e8e\u5f02\u6b65\u8bf7\u6c42\u7684Python\u5e93\uff0c\u5b83\u4f7f\u7528asyncio\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u3002\u8981\u5728Python\u4e2d\u4f7f\u7528AI [&hellip;]","protected":false},"author":3,"featured_media":99779,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99774"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=99774"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99774\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/99779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=99774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=99774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=99774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}