{"id":110886,"date":"2026-09-29T17:08:48","date_gmt":"2026-09-29T09:08:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/110886.html"},"modified":"2026-09-29T17:08:48","modified_gmt":"2026-09-29T09:08:48","slug":"python-asyncio-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%ae%9e%e6%88%98%e6%8c%87%e5%8d%97%ef%bc%9a%e4%bb%8e-tcp-udp-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%88%b0-ssl-tls-%e4%b8%8e%e8%bf%9e%e6%8e%a5%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/110886.html","title":{"rendered":"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09"},"content":{"rendered":"<ul class=\"gcb-tag-list\">\n<li><span class=\"gcb-tag\">\u6587\u6863<\/span><\/li>\n<li><span class=\"gcb-tag\">\u6559\u7a0b<\/span><\/li>\n<li><span class=\"gcb-tag\">\u5f00\u53d1\u5de5\u5177<\/span><\/li>\n<\/ul>\n<h3 class=\"gcb-project-title\">\u3010\u514d\u8d39\u4e0b\u8f7d\u94fe\u63a5\u3011pysheeet<\/h3>\n<p class=\"gcb-project-desc\">Python Cheat Sheet<\/p>\n<p>    <span class=\"gcb-project-addr-label\">\u9879\u76ee\u5730\u5740&#xff1a;<\/span><br \/>\n    <span class=\"gcb-project-addr-link\">https:\/\/gitcode.com\/gh_mirrors\/py\/pysheeet<\/span> <\/p>\n<p>  <span class=\"gcb-project-btn\"> <span class=\"gcb-project-btn-main\">\u70b9\u51fb\u67e5\u770b<\/span> <span class=\"gcb-project-btn-sub\">\u514d\u8d39\u4e0b\u8f7d<\/span> <\/span> <\/p>\n<h3>\u5bfc\u8bfb<\/h3>\n<p>\u672c\u6587\u57fa\u4e8e pysheeet \u4ed3\u5e93\u7684 docs\/notes\/asyncio\/python-asyncio-server.rst \u6587\u6863&#xff0c;\u7cfb\u7edf\u8bb2\u89e3 Python asyncio \u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5b8c\u6574\u5b9e\u6218\u8def\u5f84&#xff1a;\u4ece\u57fa\u4e8e Streams \u9ad8\u5c42 API \u7684 TCP \u670d\u52a1\u5668\/\u5ba2\u6237\u7aef\u3001\u5e95\u5c42 Socket \u4e0e Transport\/Protocol \u63a7\u5236\u3001UDP \u6570\u636e\u62a5\u670d\u52a1&#xff0c;\u5230 HTTPS \u5ba2\u6237\u7aef\u4e0e SSL\/TLS \u52a0\u5bc6\u670d\u52a1\u7aef&#xff0c;\u518d\u5230 DNS \u89e3\u6790\u3001sendfile \u96f6\u62f7\u8d1d\u4f20\u8f93\u4e0e\u8fde\u63a5\u6c60\u7b49\u9ad8\u9636\u6a21\u5f0f\u3002\u8bfb\u5b8c\u672c\u6587&#xff0c;\u4f60\u5c06\u80fd\u591f\u7528\u5355\u7ebf\u7a0b\u4e8b\u4ef6\u5faa\u73af\u6784\u5efa\u5e76\u53d1\u7f51\u7edc\u670d\u52a1&#xff0c;\u7406\u89e3\u9ad8\u5c42 Streams API \u4e0e\u5e95\u5c42 Transport\/Protocol API \u7684\u53d6\u820d&#xff0c;\u5e76\u638c\u63e1\u751f\u4ea7\u7ea7\u5ba2\u6237\u7aef\u5e38\u7528\u7684\u8fde\u63a5\u590d\u7528\u4e0e\u5e76\u53d1\u63a7\u5236\u624b\u6bb5\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48 asyncio \u5929\u751f\u9002\u5408\u7f51\u7edc\u7f16\u7a0b<\/h3>\n<p>\u7f51\u7edc I\/O \u672c\u8d28\u4e0a\u662f\u5f02\u6b65\u7684&#xff1a;\u4f60\u53d1\u51fa\u4e00\u4e2a\u8bf7\u6c42&#xff0c;\u7136\u540e\u7b49\u5f85\u54cd\u5e94\u3002\u5728\u4f20\u7edf\u963b\u585e\u5f0f\u7f16\u7a0b\u4e2d&#xff0c;\u7b49\u5f85\u671f\u95f4\u7ebf\u7a0b\u88ab\u767d\u767d\u5360\u7528&#xff1b;\u800c asyncio \u91c7\u7528\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1&#xff08;cooperative multitasking&#xff09;&#xff0c;\u4efb\u52a1\u5728 await \u6302\u8d77\u65f6\u4e3b\u52a8\u8ba9\u51fa\u63a7\u5236\u6743&#xff0c;\u8ba9\u4e8b\u4ef6\u5faa\u73af\u53bb\u8c03\u5ea6\u5176\u4ed6\u5c31\u7eea\u4efb\u52a1\u3002\u6b63\u5982 docs\/notes\/asyncio\/index.rst \u6240\u8ff0&#xff0c;asyncio \u901a\u8fc7\u534f\u7a0b\u5728\u5355\u7ebf\u7a0b\u5185\u591a\u8def\u590d\u7528 socket \u53ca\u5176\u5b83\u8d44\u6e90\u7684 I\/O&#xff0c;\u975e\u5e38\u9002\u5408 web \u670d\u52a1\u5668\u3001\u6570\u636e\u5e93\u5ba2\u6237\u7aef\u8fd9\u7c7b\u4ee5\u7b49\u5f85\u5916\u90e8\u8d44\u6e90\u4e3a\u4e3b\u8981\u74f6\u9888\u7684\u5e94\u7528\u3002<\/p>\n<p>\u4ed3\u5e93\u4e2d\u7684 src\/basic\/asyncio_.py \u63d0\u4f9b\u4e86\u5927\u91cf\u914d\u5957\u6d4b\u8bd5&#xff08;\u5982 test_gather\u3001test_wait_for_timeout\u3001test_semaphore&#xff09;&#xff0c;\u53ef\u4f5c\u4e3a\u672c\u6587\u6240\u6709\u4ee3\u7801\u793a\u4f8b\u7684\u53ef\u8fd0\u884c\u9a8c\u8bc1&#xff1b;\u800c docs\/notes\/network\/python-socket-async.rst \u5219\u4ece select\/epoll\/kqueue \u7684\u89d2\u5ea6\u89e3\u91ca\u4e86 asyncio \u5e95\u5c42 I\/O \u591a\u8def\u590d\u7528\u7684\u539f\u7406\u3002\u4e24\u79cd\u6a21\u578b\u7684\u5bf9\u6bd4\u5982\u4e0b\u56fe\u6240\u793a&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260929090847-6abb801f0e3a7.png\" alt=\"\u4e8b\u4ef6\u5faa\u73af\u6a21\u578b\u4e0e\u591a\u7ebf\u7a0b\u6a21\u578b\u7684 TCP \u670d\u52a1\u5668\u5b9e\u73b0\u5bf9\u6bd4\" \/><\/p>\n<p>\u5de6\u4fa7\u4e8b\u4ef6\u5faa\u73af\u6a21\u578b\u7528 await loop.sock_accept \/ loop.sock_recv \u52a0 loop.create_task \u5728\u5355\u7ebf\u7a0b\u5185\u5e76\u53d1\u5904\u7406\u8fde\u63a5&#xff1b;\u53f3\u4fa7\u591a\u7ebf\u7a0b\u6a21\u578b\u5219\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u3002\u7406\u89e3\u8fd9\u5f20\u56fe&#xff0c;\u5c31\u7406\u89e3\u4e86 asyncio \u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3&#xff1a;\u7528\u534f\u4f5c\u5f0f\u8c03\u5ea6\u66ff\u4ee3\u7ebf\u7a0b\u5f00\u9500\u3002<\/p>\n<h3>\u57fa\u4e8e Streams \u7684 TCP Echo \u670d\u52a1\u5668<\/h3>\n<p>asyncio.start_server() \u4e0e asyncio.open_connection() \u7ec4\u6210\u7684 Streams API \u662f TCP \u7f51\u7edc\u7f16\u7a0b\u7684\u63a8\u8350\u9ad8\u5c42\u63a5\u53e3&#xff0c;\u5b83\u81ea\u52a8\u5904\u7406\u7f13\u51b2\u3001\u7f16\u7801\u4e0e\u8fde\u63a5\u7ba1\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684 TCP Echo \u670d\u52a1\u5668&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>async def handle_client(reader, writer):<br \/>\n    addr &#061; writer.get_extra_info(&#039;peername&#039;)<br \/>\n    print(f&#034;Connected: {addr}&#034;)<\/p>\n<p>    while True:<br \/>\n        data &#061; await reader.read(1024)<br \/>\n        if not data:<br \/>\n            break<br \/>\n        message &#061; data.decode()<br \/>\n        print(f&#034;Received: {message!r} from {addr}&#034;)<br \/>\n        writer.write(data)<br \/>\n        await writer.drain()<\/p>\n<p>    print(f&#034;Disconnected: {addr}&#034;)<br \/>\n    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>async def main():<br \/>\n    server &#061; await asyncio.start_server(<br \/>\n        handle_client, &#039;localhost&#039;, 8888<br \/>\n    )<br \/>\n    addr &#061; server.sockets[0].getsockname()<br \/>\n    print(f&#034;Serving on {addr}&#034;)<\/p>\n<p>    async with server:<br \/>\n        await server.serve_forever()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u8981\u70b9\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>writer.get_extra_info(&#039;peername&#039;) \u83b7\u53d6\u5bf9\u7aef\u5730\u5740&#xff08;(host, port) \u5143\u7ec4&#xff09;&#xff0c;\u8fd9\u662f Streams API \u4e2d\u83b7\u53d6\u8fde\u63a5\u5143\u4fe1\u606f\u7684\u6807\u51c6\u65b9\u5f0f&#xff1b;<\/li>\n<li>await reader.read(1024) \u8fd4\u56de\u7a7a\u5b57\u8282\u4e32&#xff08;b&#039;&#039;&#xff09;\u8868\u793a\u5bf9\u7aef\u5df2\u5173\u95ed\u8fde\u63a5&#xff0c;\u636e\u6b64\u8df3\u51fa\u5faa\u73af&#xff1b;<\/li>\n<li>await writer.drain() \u5728\u5199\u5165\u5927\u91cf\u6570\u636e\u65f6\u7b49\u5f85\u5e95\u5c42\u7f13\u51b2\u533a\u6392\u7a7a&#xff0c;\u907f\u514d\u80cc\u538b\u95ee\u9898&#xff1b;<\/li>\n<li>async with server \u4fdd\u8bc1\u670d\u52a1\u5173\u95ed\u65f6\u6b63\u786e\u6e05\u7406&#xff1b;<\/li>\n<li>server.sockets[0].getsockname() \u53ef\u6253\u5370\u5b9e\u9645\u76d1\u542c\u5730\u5740&#xff0c;\u4fbf\u4e8e\u786e\u8ba4\u7aef\u53e3\u7ed1\u5b9a\u3002<\/li>\n<\/ul>\n<h3>TCP \u5ba2\u6237\u7aef&#xff1a;open_connection<\/h3>\n<p>\u5ba2\u6237\u7aef\u901a\u8fc7 asyncio.open_connection() \u5efa\u7acb\u8fde\u63a5&#xff0c;\u8fd4\u56de\u7684 reader \/ writer \u63d0\u4f9b\u5f02\u6b65\u6536\u53d1\u65b9\u6cd5&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>async def tcp_client(message):<br \/>\n    reader, writer &#061; await asyncio.open_connection(<br \/>\n        &#039;localhost&#039;, 8888<br \/>\n    )<\/p>\n<p>    print(f&#034;Sending: {message!r}&#034;)<br \/>\n    writer.write(message.encode())<br \/>\n    await writer.drain()<\/p>\n<p>    data &#061; await reader.read(1024)<br \/>\n    print(f&#034;Received: {data.decode()!r}&#034;)<\/p>\n<p>    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>asyncio.run(tcp_client(&#034;Hello, Server!&#034;))<\/p>\n<p>\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7684\u8bfb\u5199\u6a21\u5f0f\u5b8c\u5168\u5bf9\u79f0&#xff1a;write &#043; drain \u53d1\u9001&#xff0c;read \u63a5\u6536\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f&#xff0c;writer.close() \u540e\u5efa\u8bae await writer.wait_closed()&#xff0c;\u786e\u4fdd\u5e95\u5c42\u8fde\u63a5\u771f\u6b63\u5173\u95ed\u5b8c\u6210\u540e\u518d\u9000\u51fa\u534f\u7a0b\u2014\u2014\u8fd9\u5728\u5e76\u53d1\u573a\u666f\u4e0b\u53ef\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<h3>\u5e95\u5c42 Socket \u63a7\u5236&#xff1a;\u4e8b\u4ef6\u5faa\u73af\u7684 sock_* \u65b9\u6cd5<\/h3>\n<p>\u5f53\u4f60\u9700\u8981\u7cbe\u7ec6\u63a7\u5236 socket \u9009\u9879&#xff08;\u5982 SO_REUSEADDR&#xff09;&#xff0c;\u6216\u9700\u8981\u4e0e\u65e2\u6709 socket \u4ee3\u7801\u96c6\u6210\u65f6&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e8b\u4ef6\u5faa\u73af\u7684 loop.sock_* \u7cfb\u5217\u65b9\u6cd5\u64cd\u4f5c\u975e\u963b\u585e socket&#xff1a;<\/p>\n<p>import asyncio<br \/>\nimport socket<\/p>\n<p>async def handle_client(loop, conn):<br \/>\n    while True:<br \/>\n        data &#061; await loop.sock_recv(conn, 1024)<br \/>\n        if not data:<br \/>\n            break<br \/>\n        await loop.sock_sendall(conn, data)<br \/>\n    conn.close()<\/p>\n<p>async def server():<br \/>\n    loop &#061; asyncio.get_event_loop()<\/p>\n<p>    sock &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\n    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)<br \/>\n    sock.setblocking(False)<br \/>\n    sock.bind((&#039;localhost&#039;, 8888))<br \/>\n    sock.listen(100)<\/p>\n<p>    print(&#034;Server listening on localhost:8888&#034;)<br \/>\n    while True:<br \/>\n        conn, addr &#061; await loop.sock_accept(sock)<br \/>\n        print(f&#034;Connected: {addr}&#034;)<br \/>\n        asyncio.create_task(handle_client(loop, conn))<\/p>\n<p>asyncio.run(server())<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>sock.setblocking(False) \u662f\u5fc5\u987b\u7684\u2014\u2014\u4e8b\u4ef6\u5faa\u73af\u7684 sock_* \u65b9\u6cd5\u8981\u6c42\u975e\u963b\u585e socket&#xff1b;<\/li>\n<li>SO_REUSEADDR \u5141\u8bb8\u7aef\u53e3\u5728\u670d\u52a1\u91cd\u542f\u540e\u7acb\u5373\u590d\u7528&#xff08;\u907f\u514d TIME_WAIT \u72b6\u6001\u5bfc\u81f4\u7684\u7ed1\u5b9a\u5931\u8d25&#xff09;&#xff0c;\u4ed3\u5e93 src\/basic\/socket_.py \u7684 test_reuseaddr \u6d4b\u8bd5\u9a8c\u8bc1\u4e86\u8be5\u9009\u9879\u7684\u8bfb\u5199\u884c\u4e3a&#xff1b;<\/li>\n<li>listen(100) \u8bbe\u7f6e\u7b49\u5f85\u961f\u5217\u957f\u5ea6&#xff1b;<\/li>\n<li>\u6bcf\u4e2a\u65b0\u8fde\u63a5\u7528 asyncio.create_task \u521b\u5efa\u72ec\u7acb\u4efb\u52a1&#xff0c;\u8fd9\u6b63\u662f\u524d\u6587\u56fe\u4e2d&#034;\u4e8b\u4ef6\u5faa\u73af\u6a21\u578b&#034;\u7684\u5178\u578b\u5199\u6cd5\u3002<\/li>\n<\/ul>\n<h3>UDP Echo \u670d\u52a1\u5668&#xff1a;DatagramProtocol<\/h3>\n<p>UDP \u65e0\u8fde\u63a5&#xff0c;API \u4e0e TCP \u622a\u7136\u4e0d\u540c&#xff1a;\u4f7f\u7528 loop.create_datagram_endpoint() \u914d\u5408\u534f\u8bae\u7c7b\u5904\u7406\u6570\u636e\u62a5\u3002\u6bcf\u4e2a\u6570\u636e\u62a5\u76f8\u4e92\u72ec\u7acb&#xff0c;\u53ef\u80fd\u4e71\u5e8f\u5230\u8fbe&#xff0c;\u4e5f\u53ef\u80fd\u4e22\u5931&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>class EchoUDPProtocol(asyncio.DatagramProtocol):<br \/>\n    def connection_made(self, transport):<br \/>\n        self.transport &#061; transport<\/p>\n<p>    def datagram_received(self, data, addr):<br \/>\n        message &#061; data.decode()<br \/>\n        print(f&#034;Received {message!r} from {addr}&#034;)<br \/>\n        self.transport.sendto(data, addr)<\/p>\n<p>async def main():<br \/>\n    loop &#061; asyncio.get_event_loop()<br \/>\n    transport, protocol &#061; await loop.create_datagram_endpoint(<br \/>\n        EchoUDPProtocol,<br \/>\n        local_addr&#061;(&#039;localhost&#039;, 9999)<br \/>\n    )<br \/>\n    print(&#034;UDP server listening on localhost:9999&#034;)<\/p>\n<p>    try:<br \/>\n        await asyncio.sleep(3600)  # Run for 1 hour<br \/>\n    finally:<br \/>\n        transport.close()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u7684\u534f\u8bae\u662f asyncio.DatagramProtocol \u800c\u975e asyncio.Protocol&#xff0c;\u56de\u8c03\u662f datagram_received(data, addr)&#xff08;\u6bd4 TCP \u7684 data_received(data) \u591a\u51fa\u6765\u6e90\u5730\u5740&#xff09;&#xff0c;\u56de\u5305\u4f7f\u7528 self.transport.sendto(data, addr) \u660e\u786e\u6307\u5b9a\u76ee\u6807\u5730\u5740\u3002\u7531\u4e8e UDP \u65e0\u8fde\u63a5\u8bed\u4e49&#xff0c;transport \u4e0d\u4f1a\u5173\u95ed&#xff0c;\u793a\u4f8b\u7528\u5b9a\u65f6\u7761\u7720\u4fdd\u6301\u670d\u52a1\u8fd0\u884c\u3002<\/p>\n<h3>HTTPS \u5ba2\u6237\u7aef&#xff1a;SSL \u4e0a\u4e0b\u6587\u4e0e\u5e76\u53d1\u6293\u53d6<\/h3>\n<p>\u53d1\u8d77 HTTPS \u8bf7\u6c42\u9700\u8981\u914d\u7f6e SSL \u4e0a\u4e0b\u6587\u3002\u4ee5\u4e0b\u793a\u4f8b\u7528\u5e95\u5c42 Streams \u624b\u5199 HTTP\/1.1 \u8bf7\u6c42&#xff0c;\u5e76\u901a\u8fc7 asyncio.gather \u5e76\u53d1\u6293\u53d6\u591a\u4e2a\u7ad9\u70b9&#xff1a;<\/p>\n<p>import asyncio<br \/>\nimport ssl<\/p>\n<p>async def fetch_https(host, path&#061;&#034;\/&#034;):<br \/>\n    # Create SSL context with certificate verification<br \/>\n    ctx &#061; ssl.create_default_context()<\/p>\n<p>    reader, writer &#061; await asyncio.open_connection(<br \/>\n        host, 443, ssl&#061;ctx<br \/>\n    )<\/p>\n<p>    # Send HTTP request<br \/>\n    request &#061; f&#034;GET {path} HTTP\/1.1\\\\r\\\\nHost: {host}\\\\r\\\\nConnection: close\\\\r\\\\n\\\\r\\\\n&#034;<br \/>\n    writer.write(request.encode())<br \/>\n    await writer.drain()<\/p>\n<p>    # Read response<br \/>\n    response &#061; await reader.read()<br \/>\n    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>    return response.decode()<\/p>\n<p>async def main():<br \/>\n    urls &#061; [<br \/>\n        (&#034;www.python.org&#034;, &#034;\/&#034;),<br \/>\n        (&#034;github.com&#034;, &#034;\/&#034;),<br \/>\n    ]<br \/>\n    tasks &#061; [fetch_https(host, path) for host, path in urls]<br \/>\n    responses &#061; await asyncio.gather(*tasks)<\/p>\n<p>    for (host, _), resp in zip(urls, responses):<br \/>\n        status &#061; resp.split(&#039;\\\\r\\\\n&#039;)[0]<br \/>\n        print(f&#034;{host}: {status}&#034;)<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>ssl.create_default_context() \u4f7f\u7528\u7cfb\u7edf CA \u8bc1\u4e66\u5e93\u505a\u670d\u52a1\u7aef\u8bc1\u4e66\u6821\u9a8c&#xff0c;\u8fd9\u662f\u5b89\u5168\u9ed8\u8ba4\u503c&#xff1b;<\/li>\n<li>ssl&#061;ctx \u53c2\u6570\u4f20\u7ed9 open_connection \u5373\u542f\u7528 TLS \u63e1\u624b&#xff08;443 \u7aef\u53e3&#xff09;&#xff1b;<\/li>\n<li>Connection: close \u544a\u77e5\u670d\u52a1\u5668\u54cd\u5e94\u7ed3\u675f\u540e\u5173\u95ed\u8fde\u63a5&#xff0c;\u914d\u5408 await reader.read() \u65e0\u53c2\u8bfb\u53d6\u76f4\u5230 EOF&#xff0c;\u53ef\u4e00\u6b21\u6027\u62ff\u5168\u54cd\u5e94&#xff1b;<\/li>\n<li>asyncio.gather(*tasks) \u5e76\u53d1\u6267\u884c\u591a\u4e2a\u8bf7\u6c42&#xff0c;\u8fd9\u6b63\u662f asyncio \u63d0\u5347 I\/O \u5bc6\u96c6\u4efb\u52a1\u541e\u5410\u7684\u6838\u5fc3\u624b\u6bb5\u2014\u2014\u4ed3\u5e93 src\/basic\/asyncio_.py \u7684 test_gather \u9a8c\u8bc1\u4e86 gather \u7684\u5e76\u53d1\u8bed\u4e49\u3002<\/li>\n<\/ul>\n<h3>HTTPS \u670d\u52a1\u5668&#xff1a;TLS \u52a0\u5bc6\u9759\u6001\u5185\u5bb9\u670d\u52a1<\/h3>\n<p>\u521b\u5efa HTTPS \u670d\u52a1\u5668\u9700\u8981 SSL \u8bc1\u4e66\u3002\u4e0b\u9762\u7528 ssl.SSLContext \u52a0\u8f7d\u8bc1\u4e66\u94fe&#xff0c;\u5c06 TLS \u5c42\u76f4\u63a5\u5d4c\u5165 start_server&#xff1a;<\/p>\n<p>import asyncio<br \/>\nimport ssl<\/p>\n<p>async def handle_request(reader, writer):<br \/>\n    request &#061; await reader.read(1024)<\/p>\n<p>    response &#061; b&#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;<br \/>\n    response &#043;&#061; b&#034;Content-Type: text\/html\\\\r\\\\n\\\\r\\\\n&#034;<br \/>\n    response &#043;&#061; b&#034;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello HTTPS!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#034;<\/p>\n<p>    writer.write(response)<br \/>\n    await writer.drain()<br \/>\n    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>async def main():<br \/>\n    # Create SSL context<br \/>\n    ctx &#061; ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)<br \/>\n    ctx.load_cert_chain(&#039;cert.pem&#039;, &#039;key.pem&#039;)<\/p>\n<p>    server &#061; await asyncio.start_server(<br \/>\n        handle_request, &#039;localhost&#039;, 8443, ssl&#061;ctx<br \/>\n    )<br \/>\n    print(&#034;HTTPS server on https:\/\/localhost:8443&#034;)<\/p>\n<p>    async with server:<br \/>\n        await server.serve_forever()<\/p>\n<p># Generate self-signed cert:<br \/>\n# openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes<br \/>\nasyncio.run(main())<\/p>\n<p>\u670d\u52a1\u7aef SSL \u4e0a\u4e0b\u6587\u5fc5\u987b\u4f7f\u7528 ssl.PROTOCOL_TLS_SERVER&#xff08;\u800c\u975e\u5ba2\u6237\u7aef\u7684 default context&#xff09;&#xff0c;\u5e76\u7528 load_cert_chain(certfile, keyfile) \u52a0\u8f7d\u8bc1\u4e66\u4e0e\u79c1\u94a5\u3002\u6587\u6863\u6ce8\u91ca\u4e2d\u7ed9\u51fa\u7684 openssl \u547d\u4ee4\u53ef\u751f\u6210\u672c\u5730\u81ea\u7b7e\u540d\u8bc1\u4e66\u7528\u4e8e\u6d4b\u8bd5&#xff1a;<\/p>\n<p>openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes<\/p>\n<h3>Transport\/Protocol \u5e95\u5c42 API<\/h3>\n<p>Streams API \u4e4b\u4e0b\u662f Transport\/Protocol \u5206\u5c42&#xff1a;Transport \u8d1f\u8d23\u5b9e\u9645 I\/O \u4f20\u8f93&#xff0c;Protocol \u8d1f\u8d23\u6570\u636e\u5904\u7406\u3002\u8fd9\u79cd\u804c\u8d23\u5206\u79bb\u8ba9\u7f51\u7edc\u4ee3\u7801\u66f4\u7075\u6d3b\u3001\u53ef\u590d\u7528\u3002\u7528 loop.create_server() \u76f4\u63a5\u6ce8\u518c\u534f\u8bae\u7c7b&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>class EchoProtocol(asyncio.Protocol):<br \/>\n    def connection_made(self, transport):<br \/>\n        self.transport &#061; transport<br \/>\n        peername &#061; transport.get_extra_info(&#039;peername&#039;)<br \/>\n        print(f&#034;Connection from {peername}&#034;)<\/p>\n<p>    def data_received(self, data):<br \/>\n        print(f&#034;Received: {data.decode()!r}&#034;)<br \/>\n        self.transport.write(data)<\/p>\n<p>    def connection_lost(self, exc):<br \/>\n        print(&#034;Connection closed&#034;)<\/p>\n<p>async def main():<br \/>\n    loop &#061; asyncio.get_event_loop()<br \/>\n    server &#061; await loop.create_server(<br \/>\n        EchoProtocol, &#039;localhost&#039;, 8888<br \/>\n    )<\/p>\n<p>    async with server:<br \/>\n        await server.serve_forever()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u4e0e Streams \u56de\u8c03\u98ce\u683c\u5bf9\u6bd4&#xff1a;connection_made&#xff08;\u8fde\u63a5\u5efa\u7acb&#xff09;\u3001data_received&#xff08;\u6570\u636e\u5230\u8fbe&#xff09;\u3001connection_lost&#xff08;\u8fde\u63a5\u65ad\u5f00&#xff09;\u662f\u534f\u8bae\u751f\u547d\u5468\u671f\u56de\u8c03&#xff1b;transport.write \u76f4\u63a5\u5199\u56de\u6570\u636e\u3002\u524d\u6587\u7684 UDP \u793a\u4f8b EchoUDPProtocol \u6b63\u662f\u540c\u4e00 API \u5bb6\u65cf\u5728\u6570\u636e\u62a5\u534f\u8bae\u4e0a\u7684\u53d8\u4f53\u3002\u5f53\u9700\u8981\u7cbe\u7ec6\u63a7\u5236&#xff08;\u5982\u81ea\u5b9a\u4e49\u534f\u8bae\u89e3\u6790\u3001\u6d41\u91cf\u63a7\u5236&#xff09;\u65f6&#xff0c;\u6b64 API \u6bd4 Streams \u66f4\u76f4\u63a5\u3002<\/p>\n<h3>\u5f02\u6b65 DNS \u89e3\u6790<\/h3>\n<p>DNS \u89e3\u6790&#xff08;getaddrinfo&#xff09;\u540c\u6837\u662f\u963b\u585e\u64cd\u4f5c&#xff0c;asyncio \u63d0\u4f9b\u5f02\u6b65\u7248\u672c\u907f\u514d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af&#xff1a;<\/p>\n<p>import asyncio<br \/>\nimport socket<\/p>\n<p>async def resolve_host(host, port&#061;80):<br \/>\n    loop &#061; asyncio.get_event_loop()<br \/>\n    infos &#061; await loop.getaddrinfo(<br \/>\n        host, port,<br \/>\n        family&#061;socket.AF_UNSPEC,<br \/>\n        type&#061;socket.SOCK_STREAM<br \/>\n    )<\/p>\n<p>    for family, type_, proto, canonname, sockaddr in infos:<br \/>\n        ip, port &#061; sockaddr[:2]<br \/>\n        family_name &#061; &#034;IPv4&#034; if family &#061;&#061; socket.AF_INET else &#034;IPv6&#034;<br \/>\n        print(f&#034;{host} -&gt; {ip} ({family_name})&#034;)<\/p>\n<p>async def main():<br \/>\n    hosts &#061; [&#034;python.org&#034;, &#034;github.com&#034;, &#034;google.com&#034;]<br \/>\n    await asyncio.gather(*[resolve_host(h) for h in hosts])<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;family&#061;socket.AF_UNSPEC \u8868\u793a\u540c\u65f6\u67e5\u8be2 IPv4 \u4e0e IPv6&#xff1b;type&#061;socket.SOCK_STREAM \u9650\u5b9a\u6d41\u5f0f&#xff08;TCP&#xff09;\u5957\u63a5\u5b57\u3002\u8fd4\u56de\u7684 sockaddr \u5143\u7ec4\u4e2d\u524d\u4e24\u9879\u5373 IP \u4e0e\u7aef\u53e3\u3002\u4ed3\u5e93 src\/basic\/socket_.py \u7684 test_getaddrinfo \u6d4b\u8bd5\u9a8c\u8bc1\u4e86\u540c\u6b65\u7248 socket.getaddrinfo \u7684\u8fd4\u56de\u7ed3\u6784&#xff0c;\u53ef\u4f5c\u4e3a\u5bf9\u7167\u3002\u6ce8\u610f loop.getaddrinfo \u5728 Python 3.7&#043; \u4e2d\u5b9e\u9645\u5728\u9ed8\u8ba4\u6267\u884c\u5668\u4e2d\u8fd0\u884c&#xff0c;\u56e0\u6b64\u4e0d\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h3>\u624b\u5199\u6781\u7b80 HTTP \u670d\u52a1\u5668<\/h3>\n<p>\u4e00\u4e2a\u6700\u5c0f\u7684 HTTP \u670d\u52a1\u5668\u53ef\u4ee5\u5c55\u793a\u8bf7\u6c42\u89e3\u6790\u4e0e\u54cd\u5e94\u7684\u5b8c\u6574\u6d41\u7a0b\u3002\u751f\u4ea7\u73af\u5883\u8bf7\u4f7f\u7528 aiohttp\u3001FastAPI \u7b49\u6846\u67b6&#xff0c;\u4f46\u7406\u89e3\u624b\u5199\u5b9e\u73b0\u6709\u52a9\u4e8e\u638c\u63e1\u534f\u8bae\u7ec6\u8282&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>async def handle_http(reader, writer):<br \/>\n    request &#061; await reader.read(1024)<br \/>\n    request_line &#061; request.decode().split(&#039;\\\\r\\\\n&#039;)[0]<br \/>\n    method, path, _ &#061; request_line.split(&#039; &#039;)<\/p>\n<p>    print(f&#034;{method} {path}&#034;)<\/p>\n<p>    # Simple routing<br \/>\n    if path &#061;&#061; &#039;\/&#039;:<br \/>\n        body &#061; b&#034;&lt;h1&gt;Home&lt;\/h1&gt;&#034;<br \/>\n        status &#061; &#034;200 OK&#034;<br \/>\n    elif path &#061;&#061; &#039;\/about&#039;:<br \/>\n        body &#061; b&#034;&lt;h1&gt;About&lt;\/h1&gt;&#034;<br \/>\n        status &#061; &#034;200 OK&#034;<br \/>\n    else:<br \/>\n        body &#061; b&#034;&lt;h1&gt;404 Not Found&lt;\/h1&gt;&#034;<br \/>\n        status &#061; &#034;404 Not Found&#034;<\/p>\n<p>    response &#061; f&#034;HTTP\/1.1 {status}\\\\r\\\\n&#034;<br \/>\n    response &#043;&#061; f&#034;Content-Length: {len(body)}\\\\r\\\\n&#034;<br \/>\n    response &#043;&#061; &#034;Content-Type: text\/html\\\\r\\\\n\\\\r\\\\n&#034;<\/p>\n<p>    writer.write(response.encode() &#043; body)<br \/>\n    await writer.drain()<br \/>\n    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>async def main():<br \/>\n    server &#061; await asyncio.start_server(<br \/>\n        handle_http, &#039;localhost&#039;, 8080<br \/>\n    )<br \/>\n    print(&#034;HTTP server on http:\/\/localhost:8080&#034;)<\/p>\n<p>    async with server:<br \/>\n        await server.serve_forever()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u8fd9\u91cc\u624b\u5de5\u6784\u9020\u4e86 HTTP \u54cd\u5e94\u7684\u4e09\u8981\u7d20&#xff1a;\u72b6\u6001\u884c&#xff08;HTTP\/1.1 200 OK&#xff09;\u3001\u5934\u90e8&#xff08;Content-Length \u4e0e Content-Type&#xff09;\u3001\u7a7a\u884c\u5206\u9694\u7b26&#xff08;\\\\r\\\\n\\\\r\\\\n&#xff09;\u52a0\u6d88\u606f\u4f53\u3002\u6b63\u786e\u8bbe\u7f6e Content-Length \u662f\u6d4f\u89c8\u5668\u6b63\u5e38\u6e32\u67d3\u7684\u5173\u952e\u3002\u5bf9\u7167\u524d\u6587 HTTPS \u670d\u52a1\u5668\u793a\u4f8b\u53ef\u89c1&#xff1a;\u52a0\u5bc6\u7248\u4e0e\u6b64\u5904\u7ed3\u6784\u5b8c\u5168\u4e00\u81f4&#xff0c;\u533a\u522b\u4ec5\u5728 start_server \u662f\u5426\u4f20\u5165 ssl \u4e0a\u4e0b\u6587\u3002<\/p>\n<h3>sendfile&#xff1a;\u96f6\u62f7\u8d1d\u9ad8\u6548\u6587\u4ef6\u4f20\u8f93<\/h3>\n<p>socket.sendfile&#xff08;Python 3.5&#043;&#xff0c;asyncio \u7684 loop.sendfile \u4e3a 3.7&#043;&#xff09;\u5229\u7528\u64cd\u4f5c\u7cfb\u7edf sendfile \u7cfb\u7edf\u8c03\u7528\u76f4\u63a5\u5728\u6587\u4ef6\u4e0e socket \u4e4b\u95f4\u4f20\u8f93\u6570\u636e&#xff0c;\u907f\u514d\u6570\u636e\u62f7\u8d1d\u8fdb Python \u5185\u5b58&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>async def handle_request(reader, writer):<br \/>\n    await reader.read(1024)  # Read request<\/p>\n<p>    with open(&#039;index.html&#039;, &#039;rb&#039;) as f:<br \/>\n        # Get file size<br \/>\n        f.seek(0, 2)<br \/>\n        size &#061; f.tell()<br \/>\n        f.seek(0)<\/p>\n<p>        # Send headers<br \/>\n        headers &#061; f&#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;<br \/>\n        headers &#043;&#061; f&#034;Content-Length: {size}\\\\r\\\\n&#034;<br \/>\n        headers &#043;&#061; &#034;Content-Type: text\/html\\\\r\\\\n\\\\r\\\\n&#034;<br \/>\n        writer.write(headers.encode())<\/p>\n<p>        # Send file efficiently<br \/>\n        loop &#061; asyncio.get_event_loop()<br \/>\n        await loop.sendfile(writer.transport, f)<\/p>\n<p>    writer.close()<br \/>\n    await writer.wait_closed()<\/p>\n<p>async def main():<br \/>\n    server &#061; await asyncio.start_server(<br \/>\n        handle_request, &#039;localhost&#039;, 8080<br \/>\n    )<br \/>\n    async with server:<br \/>\n        await server.serve_forever()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u6d41\u7a0b&#xff1a;\u5148\u8bfb\u53d6\u8bf7\u6c42\u3001\u8ba1\u7b97\u6587\u4ef6\u5927\u5c0f&#xff08;seek \u5230\u672b\u5c3e\u518d tell&#xff09;\u3001\u53d1\u9001\u5934\u90e8&#xff0c;\u7136\u540e await loop.sendfile(writer.transport, f) \u8ba9\u5185\u6838\u76f4\u63a5\u5b8c\u6210\u6587\u4ef6\u5230 socket \u7684\u4f20\u8f93\u3002\u6ce8\u610f sendfile \u8981\u6c42\u6587\u4ef6\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00&#xff0c;\u4e14\u4f20\u8f93\u540e\u6587\u4ef6\u6307\u9488\u5df2\u79fb\u52a8\u5230\u672b\u5c3e&#xff0c;\u65e0\u9700\u624b\u52a8 reset\u3002<\/p>\n<h3>\u8fde\u63a5\u6c60&#xff1a;\u590d\u7528\u8fde\u63a5\u63d0\u5347\u5ba2\u6237\u7aef\u6027\u80fd<\/h3>\n<p>\u8fde\u63a5\u6c60\u590d\u7528\u5df2\u5efa\u7acb\u7684\u8fde\u63a5&#xff0c;\u907f\u514d\u4e3a\u6bcf\u4e2a\u8bf7\u6c42\u91cd\u65b0\u7ecf\u5386 TCP \u4e09\u6b21\u63e1\u624b\u4e0e TLS \u63e1\u624b&#xff0c;\u662f\u9ad8\u9891\u8bf7\u6c42\u5ba2\u6237\u7aef\u7684\u5fc5\u5907\u4f18\u5316\u3002\u4e0b\u9762\u7684\u5b9e\u73b0\u7528 deque \u4fdd\u5b58\u7a7a\u95f2\u8fde\u63a5\u3001\u7528 asyncio.Lock \u4fdd\u8bc1\u6c60\u64cd\u4f5c\u7684\u539f\u5b50\u6027&#xff1a;<\/p>\n<p>import asyncio<br \/>\nfrom collections import deque<\/p>\n<p>class ConnectionPool:<br \/>\n    def __init__(self, host, port, size&#061;5):<br \/>\n        self.host &#061; host<br \/>\n        self.port &#061; port<br \/>\n        self.size &#061; size<br \/>\n        self._pool &#061; deque()<br \/>\n        self._lock &#061; asyncio.Lock()<\/p>\n<p>    async def get(self):<br \/>\n        async with self._lock:<br \/>\n            if self._pool:<br \/>\n                return self._pool.popleft()<\/p>\n<p>        # Create new connection<br \/>\n        reader, writer &#061; await asyncio.open_connection(<br \/>\n            self.host, self.port<br \/>\n        )<br \/>\n        return reader, writer<\/p>\n<p>    async def put(self, reader, writer):<br \/>\n        async with self._lock:<br \/>\n            if len(self._pool) &lt; self.size:<br \/>\n                self._pool.append((reader, writer))<br \/>\n            else:<br \/>\n                writer.close()<br \/>\n                await writer.wait_closed()<\/p>\n<p>    async def close(self):<br \/>\n        async with self._lock:<br \/>\n            while self._pool:<br \/>\n                reader, writer &#061; self._pool.popleft()<br \/>\n                writer.close()<br \/>\n                await writer.wait_closed()<\/p>\n<p>async def fetch(pool, message):<br \/>\n    reader, writer &#061; await pool.get()<br \/>\n    try:<br \/>\n        writer.write(message.encode())<br \/>\n        await writer.drain()<br \/>\n        data &#061; await reader.read(1024)<br \/>\n        return data.decode()<br \/>\n    finally:<br \/>\n        await pool.put(reader, writer)<\/p>\n<p>async def main():<br \/>\n    pool &#061; ConnectionPool(&#039;localhost&#039;, 8888, size&#061;3)<br \/>\n    try:<br \/>\n        tasks &#061; [fetch(pool, f&#034;msg{i}&#034;) for i in range(10)]<br \/>\n        results &#061; await asyncio.gather(*tasks)<br \/>\n        for r in results:<br \/>\n            print(r)<br \/>\n    finally:<br \/>\n        await pool.close()<\/p>\n<p>asyncio.run(main())<\/p>\n<p>\u8bbe\u8ba1\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>get() \u4f18\u5148\u4ece\u6c60\u4e2d\u53d6\u7a7a\u95f2\u8fde\u63a5&#xff0c;\u6c60\u7a7a\u65f6\u65b0\u5efa\u8fde\u63a5\u2014\u2014\u6ce8\u610f\u5efa\u8fde\u64cd\u4f5c\u653e\u5728\u9501\u5916&#xff0c;\u907f\u514d\u6301\u6709\u9501\u671f\u95f4\u963b\u585e\u5176\u4ed6\u534f\u7a0b&#xff1b;<\/li>\n<li>put() \u5728\u6c60\u672a\u6ee1\u65f6\u5f52\u8fd8\u8fde\u63a5&#xff0c;\u6c60\u6ee1\u5219\u5173\u95ed\u591a\u4f59\u8fde\u63a5&#xff08;writer.close() &#043; await writer.wait_closed()&#xff09;&#xff1b;<\/li>\n<li>fetch() \u7528 try\/finally \u4fdd\u8bc1\u65e0\u8bba\u8bf7\u6c42\u6210\u529f\u4e0e\u5426\u8fde\u63a5\u90fd\u88ab\u5f52\u8fd8&#xff1b;<\/li>\n<li>close() \u904d\u5386\u6c60\u4e2d\u6240\u6709\u8fde\u63a5\u9010\u4e00\u5173\u95ed&#xff0c;\u5b9e\u73b0\u4f18\u96c5\u6e05\u7406\u3002<\/li>\n<\/ul>\n<p>asyncio.Lock \u7684\u4f7f\u7528\u4e0e\u4ed3\u5e93 src\/basic\/asyncio_.py \u4e2d test_lock \u7684\u8bed\u4e49\u4e00\u81f4&#xff1a;\u5b83\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u534f\u7a0b\u80fd\u4fee\u6539\u6c60\u72b6\u6001&#xff0c;\u4f46\u5fc5\u987b\u5728 await \u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528&#xff0c;\u4e14\u53ea\u5728\u540c\u4e00\u4e8b\u4ef6\u5faa\u73af\u5185\u6709\u6548\u3002<\/p>\n<h3>\u7ec4\u5408\u5e94\u7528&#xff1a;\u4e00\u4e2a\u53ef\u8fd0\u884c\u7684\u5b8c\u6574\u94fe\u8def<\/h3>\n<p>\u5c06\u4e0a\u8ff0\u7ec4\u4ef6\u4e32\u8054\u5373\u53ef\u5f97\u5230\u5b8c\u6574\u9a8c\u8bc1&#xff1a;\u5148\u542f\u52a8 TCP Echo \u670d\u52a1\u5668&#xff08;\u7aef\u53e3 8888&#xff09;&#xff0c;\u518d\u8fd0\u884c\u8fde\u63a5\u6c60\u793a\u4f8b&#xff0c;10 \u4e2a\u5e76\u53d1\u8bf7\u6c42\u7ecf 3 \u6761\u8fde\u63a5\u7684\u6c60\u590d\u7528\u5f80\u8fd4&#xff0c;\u8f93\u51fa msg0&#xff5e;msg9 \u7684\u56de\u663e\u3002\u6574\u4e2a\u8fc7\u7a0b\u5355\u7ebf\u7a0b\u5b8c\u6210&#xff0c;\u53ef\u914d\u5408 src\/basic\/asyncio_.py \u4e2d\u7684 test_create_task\u3001test_gather\u3001test_semaphore \u7b49\u6d4b\u8bd5\u7406\u89e3\u5e95\u5c42\u8c03\u5ea6\u8bed\u4e49\u3002\u82e5\u9700\u8fdb\u4e00\u6b65\u5b66\u4e60&#xff0c;\u53ef\u53c2\u9605\u540c\u76ee\u5f55\u7684 python-asyncio-basic.rst&#xff08;\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af\u57fa\u7840&#xff09;\u4e0e python-asyncio-advanced.rst&#xff08;\u540c\u6b65\u539f\u8bed\u3001\u961f\u5217\u3001\u5b50\u8fdb\u7a0b\u4e0e\u4f18\u96c5\u5173\u95ed&#xff09;\u3002<\/p>\n<p>  <img loading=\"lazy\" decoding=\"async\" class=\"gcb-icon-like\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260929090847-6abb801fa37ff.svg\" alt=\"\" width=\"16\" height=\"16\" \/> <span class=\"gcb-like-text\">\u8d5e<\/span>  <\/p>\n<p>   <span class=\"gcb-share-text\">\u5206\u4eab<\/span>  <\/p>\n<ul class=\"gcb-tag-list\">\n<li><span class=\"gcb-tag\">\u6587\u6863<\/span><\/li>\n<li><span class=\"gcb-tag\">\u6559\u7a0b<\/span><\/li>\n<li><span class=\"gcb-tag\">\u5f00\u53d1\u5de5\u5177<\/span><\/li>\n<\/ul>\n<h3 class=\"gcb-project-title\">\u3010\u514d\u8d39\u4e0b\u8f7d\u94fe\u63a5\u3011pysheeet<\/h3>\n<p class=\"gcb-project-desc\">Python Cheat Sheet<\/p>\n<p>    <span class=\"gcb-project-addr-label\">\u9879\u76ee\u5730\u5740&#xff1a;<\/span><br \/>\n    <span class=\"gcb-project-addr-link\">https:\/\/gitcode.com\/gh_mirrors\/py\/pysheeet<\/span> <\/p>\n<p>  <span class=\"gcb-project-btn\"> <span class=\"gcb-project-btn-main\">\u70b9\u51fb\u67e5\u770b<\/span> <span class=\"gcb-project-btn-sub\">\u514d\u8d39\u4e0b\u8f7d<\/span> <\/span> <\/p>\n<h4 class=\"gcb-related-title\">\u76f8\u5173\u63a8\u8350<\/h4>\n<ul class=\"gcb-related-list\">\n<li class=\"gcb-related-item\">\n<h5 class=\"gcb-related-item-title\">3D\u70b9\u4e91\u8865\u5168\u7ec8\u6781\u6307\u5357&#xff1a;\u5982\u4f55\u7528Transformer\u8ba9\u6b8b\u7f3a\u7269\u4f53\u91cd\u83b7\u65b0\u751f&#xff1f;<\/h5>\n<p class=\"gcb-related-item-desc\">3D\u70b9\u4e91\u8865\u5168\u7ec8\u6781\u6307\u5357&#xff1a;\u5982\u4f55\u7528Transformer\u8ba9\u6b8b\u7f3a\u7269\u4f53\u91cd\u83b7\u65b0\u751f&#xff1f; \u4f60\u662f\u5426\u66fe\u9762\u5bf9\u8fc7\u6b8b\u7f3a\u76843D\u626b\u63cf\u6570\u636e\u675f\u624b\u65e0\u7b56&#xff1f;\u5728\u81ea\u52a8\u9a7e\u9a76\u6c7d\u8f66\u8bd5\u56fe\u8bc6\u522b\u88ab\u906e\u6321\u7684\u884c\u4eba\u65f6&#xff0c;\u5728\u8003\u53e4\u5b66<\/p>\n<p>     <span class=\"gcb-tag\">\u4eba\u5de5\u667a\u80fd<\/span><br \/>\n     <span class=\"gcb-tag\">\u6df1\u5ea6\u5b66\u4e60<\/span><br \/>\n     <span class=\"gcb-tag\">\u8ba1\u7b97\u673a\u89c6\u89c9<\/span>\n      <\/li>\n<li class=\"gcb-related-item\">\n<h5 class=\"gcb-related-item-title\"> libIEC61850 \u667a\u80fd\u53d8\u7535\u7ad9\u901a\u4fe1\u534f\u8bae\u7ec8\u6781\u6307\u5357<\/h5>\n<p class=\"gcb-related-item-desc\">libIEC61850\u662f\u4e00\u4e2a\u5f00\u6e90\u5e93&#xff0c;\u4e13\u95e8\u7528\u4e8e\u5b9e\u73b0IEC 61850\u6807\u51c6\u4e2d\u7684\u901a\u4fe1\u534f\u8bae&#xff0c;\u5305\u62ecMMS\u3001GOOSE\u548cSV\u534f\u8bae\u3002\u8be5\u5e93\u63d0\u4f9b\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u7684API&#xff0c;\u5e7f\u6cdb\u5e94\u7528<\/p>\n<p>     <span class=\"gcb-tag\">\u540e\u7aef<\/span><br \/>\n     <span class=\"gcb-tag\">\u8bed\u8a00\u8fd0\u884c\u65f6<\/span>\n      <\/li>\n<li class=\"gcb-related-item\">\n<h5 class=\"gcb-related-item-title\"> Monoio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218&#xff1a;\u6784\u5efa\u9ad8\u6027\u80fd TCP\/UDP \u670d\u52a1\u5668<\/h5>\n<p class=\"gcb-related-item-desc\">Monoio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218&#xff1a;\u6784\u5efa\u9ad8\u6027\u80fd TCP\/UDP \u670d\u52a1\u5668 \u5728\u5f53\u4eca\u9ad8\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\u65e5\u76ca\u666e\u53ca\u7684\u65f6\u4ee3&#xff0c;\u5982\u4f55\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u6210\u4e3a\u4e86\u5f00\u53d1\u8005\u9762\u4e34\u7684\u91cd\u8981\u6311\u6218\u3002Mon<\/p>\n<p>     <span class=\"gcb-tag\">\u8bed\u8a00\u8fd0\u884c\u65f6<\/span><br \/>\n     <span class=\"gcb-tag\">\u540e\u7aef<\/span><br \/>\n     <span class=\"gcb-tag\">\u7f51\u7edc<\/span>\n      <\/li>\n<\/ul>\n<p>  <span class=\"gcb-prev-next-label\">\u4e0a\u4e00\u7bc7&#xff1a;<\/span><br \/>\n  \u82f1\u96c4\u8054\u76df\u667a\u80fd\u52a9\u624bLeague Akari&#xff1a;\u91cd\u65b0\u5b9a\u4e49\u6e38\u620f\u81ea\u52a8\u5316\u4f53\u9a8c <\/p>\n<p>  <span class=\"gcb-prev-next-label\">\u4e0b\u4e00\u7bc7&#xff1a;<\/span><br \/>\n  DLSS Swapper\u5b8c\u5168\u6307\u5357&#xff1a;\u6e38\u620f\u753b\u8d28\u4f18\u5316\u5927\u5e08\u6df1\u5ea6\u89e3\u6790 <\/p>\n<p>\u521b\u4f5c\u58f0\u660e\uff1a\u672c\u6587\u90e8\u5206\u5185\u5bb9\u7531AI\u8f85\u52a9\u751f\u6210\uff08AIGC\uff09\uff0c\u4ec5\u4f9b\u53c2\u8003<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u6863\u6559\u7a0b\u5f00\u53d1\u5de5\u5177 \u3010\u514d\u8d39\u4e0b\u8f7d\u94fe\u63a5\u3011pysheeet Python Cheat Sheet \u9879\u76ee\u5730\u5740&#xff1a; https:\/\/gitcode.com\/gh_mirrors\/py\/pysheeet \u70b9\u51fb\u67e5\u770b \u514d\u8d39\u4e0b\u8f7d  \u5bfc\u8bfb<br \/>\n\u672c\u6587\u57fa\u4e8e pysheeet \u4ed3\u5e93\u7684 docs\/notes\/asyncio\/python-asyncio-server.rst \u6587\u6863&#xff0c;\u7cfb\u7edf\u8bb2\u89e3 Python asyncio \u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5b8c\u6574\u5b9e\u6218\u8def\u5f84&#xff1a;\u4ece\u57fa\u4e8e Stream<\/p>\n","protected":false},"author":2,"featured_media":110884,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-110886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/110886.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u6863\u6559\u7a0b\u5f00\u53d1\u5de5\u5177 \u3010\u514d\u8d39\u4e0b\u8f7d\u94fe\u63a5\u3011pysheeet Python Cheat Sheet \u9879\u76ee\u5730\u5740&#xff1a; https:\/\/gitcode.com\/gh_mirrors\/py\/pysheeet \u70b9\u51fb\u67e5\u770b \u514d\u8d39\u4e0b\u8f7d \u5bfc\u8bfb \u672c\u6587\u57fa\u4e8e pysheeet \u4ed3\u5e93\u7684 docs\/notes\/asyncio\/python-asyncio-server.rst \u6587\u6863&#xff0c;\u7cfb\u7edf\u8bb2\u89e3 Python asyncio \u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5b8c\u6574\u5b9e\u6218\u8def\u5f84&#xff1a;\u4ece\u57fa\u4e8e Stream\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/110886.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-29T09:08:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260929090847-6abb801f0e3a7.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/110886.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/110886.html\",\"name\":\"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-29T09:08:48+00:00\",\"dateModified\":\"2026-09-29T09:08:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/110886.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/110886.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/110886.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/110886.html","og_locale":"zh_CN","og_type":"article","og_title":"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u6863\u6559\u7a0b\u5f00\u53d1\u5de5\u5177 \u3010\u514d\u8d39\u4e0b\u8f7d\u94fe\u63a5\u3011pysheeet Python Cheat Sheet \u9879\u76ee\u5730\u5740&#xff1a; https:\/\/gitcode.com\/gh_mirrors\/py\/pysheeet \u70b9\u51fb\u67e5\u770b \u514d\u8d39\u4e0b\u8f7d \u5bfc\u8bfb \u672c\u6587\u57fa\u4e8e pysheeet \u4ed3\u5e93\u7684 docs\/notes\/asyncio\/python-asyncio-server.rst \u6587\u6863&#xff0c;\u7cfb\u7edf\u8bb2\u89e3 Python asyncio \u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5b8c\u6574\u5b9e\u6218\u8def\u5f84&#xff1a;\u4ece\u57fa\u4e8e Stream","og_url":"https:\/\/www.wsisp.com\/helps\/110886.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-29T09:08:48+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260929090847-6abb801f0e3a7.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/110886.html","url":"https:\/\/www.wsisp.com\/helps\/110886.html","name":"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-29T09:08:48+00:00","dateModified":"2026-09-29T09:08:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/110886.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/110886.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/110886.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python Asyncio \u7f51\u7edc\u7f16\u7a0b\u5b9e\u6218\u6307\u5357\uff1a\u4ece TCP\/UDP \u670d\u52a1\u5668\u5230 SSL\/TLS \u4e0e\u8fde\u63a5\u6c60\uff08pysheeet\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/110886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=110886"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/110886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/110884"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=110886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=110886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=110886"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=110886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}