{"id":94083,"date":"2026-08-13T17:40:50","date_gmt":"2026-08-13T09:40:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94083.html"},"modified":"2026-08-13T17:40:50","modified_gmt":"2026-08-13T09:40:50","slug":"%e7%89%b9%e6%ae%8a%e5%ad%97%e7%ac%a6-python-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e7%b2%be%e9%80%9a%ef%bc%9asocket%e3%80%81%e5%b9%b6%e5%8f%91%e3%80%81%e5%bc%82","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94083.html","title":{"rendered":"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218"},"content":{"rendered":"<h5>&#x1f4a1; \u4e00\u3001 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u7f51\u7edc\u7f16\u7a0b\u5230\u5e95\u5728\u505a\u4ec0\u4e48&#xff1f;<\/h5>\n<p>\u5f88\u591a\u4eba\u89c9\u5f97\u7f51\u7edc\u7f16\u7a0b\u96be&#xff0c;\u662f\u56e0\u4e3a\u88ab IP\u3001\u7aef\u53e3\u3001Socket\u3001TCP\u3001UDP\u3001\u963b\u585e\u3001\u975e\u963b\u585e\u7b49\u540d\u8bcd\u5413\u5230\u4e86\u3002\u5176\u5b9e\u6838\u5fc3\u903b\u8f91\u53ea\u6709\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>\u8ba9\u4e24\u4e2a\u8fdb\u7a0b\u6309\u7167\u7ea6\u5b9a&#xff0c;\u5728\u7f51\u7edc\u4e2d\u53ef\u9760\u6216\u9ad8\u6548\u5730\u4ea4\u6362\u5b57\u8282\u3002<\/p>\n<p>\u8fd9\u91cc\u5305\u542b\u4e09\u4e2a\u5173\u952e\u8bcd&#xff1a;<\/p>\n<li>\u4e24\u4e2a\u8fdb\u7a0b&#xff1a;\u901a\u5e38\u662f\u5ba2\u6237\u7aef&#xff08;Client&#xff09;\u548c\u670d\u52a1\u7aef&#xff08;Server&#xff09;\u3002<\/li>\n<li>\u4ea4\u6362\u5b57\u8282&#xff1a;\u7f51\u7edc\u4f20\u8f93\u7684\u4e0d\u662f Python \u5bf9\u8c61&#xff0c;\u800c\u662f\u4e8c\u8fdb\u5236\u6570\u636e&#xff08;bytes&#xff09;\u3002<\/li>\n<li>\u6309\u7167\u7ea6\u5b9a&#xff1a;\u5373\u534f\u8bae\u3002\u53cc\u65b9\u5fc5\u987b\u9075\u5b88\u540c\u4e00\u5957\u89c4\u5219&#xff08;\u7f16\u7801\u3001\u5206\u9694\u3001\u6821\u9a8c&#xff09;\u3002<\/li>\n<h5>&#x1f5fa;\ufe0f \u4e8c\u3001 \u5efa\u7acb\u7f51\u7edc\u7f16\u7a0b\u7684\u201c\u5730\u56fe\u201d<\/h5>\n<p>\u5728\u5199\u4ee3\u7801\u524d&#xff0c;\u6211\u4eec\u9700\u8981\u7406\u89e3\u51e0\u4e2a\u57fa\u7840\u6a21\u578b&#xff1a;<\/p>\n<p>1. \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u6a21\u578b<\/p>\n<ul>\n<li>\u670d\u52a1\u7aef&#xff1a;\u7ed1\u5b9a IP \u548c\u7aef\u53e3 \u2192 \u76d1\u542c \u2192 \u63a5\u6536\u8fde\u63a5 \u2192 \u5904\u7406\u4e1a\u52a1 \u2192 \u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<li>\u5ba2\u6237\u7aef&#xff1a;\u77e5\u9053\u670d\u52a1\u7aef\u5730\u5740 \u2192 \u53d1\u8d77\u8fde\u63a5 \u2192 \u53d1\u9001\u8bf7\u6c42 \u2192 \u63a5\u6536\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<p>2. TCP \u4e0e UDP \u7684\u6289\u62e9<\/p>\n<p>\u8868\u683c<\/p>\n<p>\u4e0b\u8f7d\u4e3a\u8868\u683c<\/p>\n<p>\u5bfc\u51fa\u4e3a\u56fe\u7247<\/p>\n<table>\n<tr>\u7279\u6027TCPUDP<\/tr>\n<tbody>\n<tr>\n<td>\u8fde\u63a5\u6027<\/td>\n<td>\u9762\u5411\u8fde\u63a5&#xff08;\u9700\u4e09\u6b21\u63e1\u624b&#xff09;<\/td>\n<td>\u65e0\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u9760\u6027<\/td>\n<td>\u53ef\u9760\u3001\u6709\u5e8f\u3001\u4e0d\u4e22\u5931<\/td>\n<td>\u4e0d\u53ef\u9760\u3001\u53ef\u80fd\u4e22\u5305\u3001\u4e71\u5e8f<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u5f62\u5f0f<\/td>\n<td>\u5b57\u8282\u6d41&#xff08;\u65e0\u8fb9\u754c&#xff09;<\/td>\n<td>\u6570\u636e\u62a5&#xff08;\u6709\u8fb9\u754c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u573a\u666f<\/td>\n<td>HTTP\u3001\u6587\u4ef6\u4f20\u8f93\u3001\u804a\u5929<\/td>\n<td>\u89c6\u9891\u76f4\u64ad\u3001DNS\u3001\u8bed\u97f3\u901a\u8bdd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3. Socket \u662f\u4ec0\u4e48&#xff1f;<br \/>\nSocket \u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u7f51\u7edc\u901a\u4fe1\u63a5\u53e3\u3002\u5728 Python \u4e2d&#xff0c;socket\u00a0\u5bf9\u8c61\u8d1f\u8d23\u5efa\u7acb\u8fde\u63a5\u3001\u6536\u53d1\u6570\u636e\u548c\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import socket<br \/>\n2<br \/>\n3# \u521b\u5efa IPv4 TCP Socket<br \/>\n4sock &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h5>&#x1f6e0;\ufe0f \u4e09\u3001 \u5b9e\u6218&#xff1a;\u5199\u51fa\u7b2c\u4e00\u4e2a TCP \u670d\u52a1\u7aef<\/h5>\n<p>\u8fd9\u662f\u6700\u7ecf\u5178\u7684\u201c\u56de\u663e\u670d\u52a1\u201d&#xff08;Echo Server&#xff09;&#xff1a;\u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u7aef\u56de\u4ec0\u4e48\u3002<\/p>\n<p>1. \u670d\u52a1\u7aef\u4ee3\u7801 (tcp_server.py)<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import socket<br \/>\n2<br \/>\n3HOST &#061; &#034;127.0.0.1&#034;<br \/>\n4PORT &#061; 5000<br \/>\n5BUFFER_SIZE &#061; 4096<br \/>\n6<br \/>\n7def main():<br \/>\n8    # \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u81ea\u52a8\u5173\u95ed\u8d44\u6e90<br \/>\n9    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:<br \/>\n10        # \u5141\u8bb8\u7aef\u53e3\u91cd\u7528&#xff0c;\u9632\u6b62\u91cd\u542f\u62a5\u9519 Address already in use<br \/>\n11        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)<br \/>\n12<br \/>\n13        server.bind((HOST, PORT))<br \/>\n14        server.listen()<br \/>\n15        print(f&#034;\u670d\u52a1\u7aef\u5df2\u542f\u52a8&#xff1a;{HOST}:{PORT}&#034;)<br \/>\n16<br \/>\n17        # \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n18        conn, address &#061; server.accept()<br \/>\n19        with conn:<br \/>\n20            print(f&#034;\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5&#xff1a;{address}&#034;)<br \/>\n21            while True:<br \/>\n22                data &#061; conn.recv(BUFFER_SIZE)<br \/>\n23                if not data: # \u5ba2\u6237\u7aef\u65ad\u5f00<br \/>\n24                    break<br \/>\n25                print(f&#034;\u6536\u5230&#xff1a;{data!r}&#034;)<br \/>\n26                conn.sendall(data) # \u539f\u6837\u53d1\u56de<br \/>\n27<br \/>\n28if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n29    main()<\/p>\n<p>2. \u5ba2\u6237\u7aef\u4ee3\u7801 (tcp_client.py)<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import socket<br \/>\n2<br \/>\n3def main():<br \/>\n4    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:<br \/>\n5        client.connect((&#034;127.0.0.1&#034;, 5000))<br \/>\n6        message &#061; &#034;\u4f60\u597d&#xff0c;Python \u7f51\u7edc\u7f16\u7a0b&#xff01;&#034;<br \/>\n7        client.sendall(message.encode(&#034;utf-8&#034;))<br \/>\n8<br \/>\n9        response &#061; client.recv(4096)<br \/>\n10        print(&#034;\u670d\u52a1\u7aef\u8fd4\u56de&#xff1a;&#034;, response.decode(&#034;utf-8&#034;))<br \/>\n11<br \/>\n12if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n13    main()<\/p>\n<p>\u5173\u952e\u70b9\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>with socket.socket(&#8230;)&#xff1a;\u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u786e\u4fdd\u5f02\u5e38\u65f6\u4e5f\u80fd\u81ea\u52a8\u5173\u95ed Socket\u3002<\/li>\n<li>send()\u00a0vs\u00a0sendall()&#xff1a;send\u00a0\u53ef\u80fd\u53ea\u53d1\u9001\u4e86\u90e8\u5206\u6570\u636e&#xff0c;sendall\u00a0\u4f1a\u786e\u4fdd\u6570\u636e\u5168\u90e8\u5199\u5165\u7f13\u51b2\u533a&#xff0c;\u4e1a\u52a1\u4ee3\u7801\u4f18\u5148\u7528\u00a0sendall\u3002<\/li>\n<\/ul>\n<h5>\u26a0\ufe0f \u56db\u3001 \u907f\u5751\u6307\u5357&#xff1a;TCP \u7684\u201c\u7c98\u5305\u201d\u4e0e\u6d88\u606f\u8fb9\u754c<\/h5>\n<p>TCP \u662f\u6ca1\u6709\u6d88\u606f\u8fb9\u754c\u7684\u5b57\u8282\u6d41\u3002<br \/>\n\u5ba2\u6237\u7aef\u00a0send\u00a0\u4e24\u6b21 &#034;Hello&#034; \u548c &#034;World&#034;&#xff0c;\u670d\u52a1\u7aef\u00a0recv\u00a0\u53ef\u80fd\u4f1a\u4e00\u6b21\u6027\u6536\u5230 &#034;HelloWorld&#034;&#xff0c;\u4e5f\u53ef\u80fd\u5206\u4e09\u6b21\u6536\u5230 &#034;Hel&#034;, &#034;loW&#034;, &#034;orld&#034;\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5b9a\u4e49\u5e94\u7528\u5c42\u534f\u8bae<br \/>\n\u6700\u5e38\u7528\u7684\u662f\u00a0\u957f\u5ea6\u524d\u7f00\u534f\u8bae&#xff1a;\u5148\u53d1 4 \u4e2a\u5b57\u8282\u8868\u793a\u957f\u5ea6&#xff0c;\u518d\u53d1\u5177\u4f53\u5185\u5bb9\u3002<\/p>\n<p>\u901a\u7528\u534f\u8bae\u5de5\u5177 (protocol.py)<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import socket<br \/>\n2import struct<br \/>\n3<br \/>\n4HEADER_SIZE &#061; 4<br \/>\n5<br \/>\n6def recv_exact(sock, size):<br \/>\n7    &#034;&#034;&#034;\u786e\u4fdd\u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u8282&#034;&#034;&#034;<br \/>\n8    chunks &#061; []<br \/>\n9    remaining &#061; size<br \/>\n10    while remaining &gt; 0:<br \/>\n11        chunk &#061; sock.recv(remaining)<br \/>\n12        if not chunk:<br \/>\n13            raise ConnectionError(&#034;\u8fde\u63a5\u5173\u95ed&#034;)<br \/>\n14        chunks.append(chunk)<br \/>\n15        remaining -&#061; len(chunk)<br \/>\n16    return b&#034;&#034;.join(chunks)<br \/>\n17<br \/>\n18def send_message(sock, payload):<br \/>\n19    &#034;&#034;&#034;\u53d1\u9001&#xff1a;4\u5b57\u8282\u957f\u5ea6 &#043; \u5185\u5bb9&#034;&#034;&#034;<br \/>\n20    header &#061; struct.pack(&#034;!I&#034;, len(payload)) # !I \u8868\u793a\u7f51\u7edc\u5b57\u8282\u5e8f\u65e0\u7b26\u53f7\u6574\u6570<br \/>\n21    sock.sendall(header &#043; payload)<br \/>\n22<br \/>\n23def recv_message(sock):<br \/>\n24    &#034;&#034;&#034;\u63a5\u6536&#xff1a;\u5148\u8bfb\u957f\u5ea6&#xff0c;\u518d\u8bfb\u5185\u5bb9&#034;&#034;&#034;<br \/>\n25    header &#061; recv_exact(sock, HEADER_SIZE)<br \/>\n26    (msg_len,) &#061; struct.unpack(&#034;!I&#034;, header)<br \/>\n27    return recv_exact(sock, msg_len)<\/p>\n<h5>&#x1f680; \u4e94\u3001 \u8fdb\u9636&#xff1a;\u5e76\u53d1\u6a21\u578b\u7684\u9009\u62e9<\/h5>\n<p>\u5355\u7ebf\u7a0b\u670d\u52a1\u7aef\u4e00\u6b21\u53ea\u80fd\u670d\u52a1\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002\u5982\u4f55\u89e3\u51b3\u9ad8\u5e76\u53d1&#xff1f;<\/p>\n<p>1. \u591a\u7ebf\u7a0b\u6a21\u578b (Threading)<br \/>\n\u9002\u5408\u8fde\u63a5\u6570\u51e0\u767e\u5230\u51e0\u5343&#xff0c;\u903b\u8f91\u7b80\u5355\u7684\u573a\u666f\u3002<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1from concurrent.futures import ThreadPoolExecutor<br \/>\n2<br \/>\n3# \u5728\u7ebf\u7a0b\u6c60\u4e2d\u5904\u7406\u8fde\u63a5<br \/>\n4with ThreadPoolExecutor(max_workers&#061;20) as pool:<br \/>\n5    while True:<br \/>\n6        conn, addr &#061; server.accept()<br \/>\n7        pool.submit(handle_client, conn, addr) # \u63d0\u4ea4\u4efb\u52a1<\/p>\n<p>2. I\/O \u591a\u8def\u590d\u7528 (selectors)<br \/>\n\u8fd9\u662f\u9ad8\u6027\u80fd\u7f51\u7edc\u5e93\u7684\u5e95\u5c42\u539f\u7406\u3002\u4e00\u4e2a\u7ebf\u7a0b\u7ba1\u7406\u591a\u4e2a\u8fde\u63a5&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u544a\u8bc9\u7a0b\u5e8f\u201c\u54ea\u4e2a Socket \u53ef\u8bfb\/\u53ef\u5199\u201d\u3002<\/p>\n<p>3. \u5f02\u6b65 I\/O (asyncio)<br \/>\nPython \u5b98\u65b9\u63a8\u8350\u7684\u73b0\u4ee3\u9ad8\u5e76\u53d1\u65b9\u6848\u3002\u57fa\u4e8e\u534f\u7a0b&#xff0c;\u5355\u7ebf\u7a0b\u5373\u53ef\u5904\u7406\u6570\u4e07\u8fde\u63a5\u3002<\/p>\n<p>Asyncio \u670d\u52a1\u7aef\u793a\u4f8b&#xff1a;<\/p>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import asyncio<br \/>\n2<br \/>\n3async def handle_client(reader, writer):<br \/>\n4    peer &#061; writer.get_extra_info(&#034;peername&#034;)<br \/>\n5    print(f&#034;\u8fde\u63a5&#xff1a;{peer}&#034;)<br \/>\n6    try:<br \/>\n7        while True:<br \/>\n8            # \u8bfb\u53d6\u4e00\u884c\u6570\u636e&#xff08;\u81ea\u52a8\u5904\u7406\u8fb9\u754c&#xff09;<br \/>\n9            data &#061; await reader.readline()<br \/>\n10            if not data: break<br \/>\n11<br \/>\n12            # \u4e1a\u52a1\u903b\u8f91<br \/>\n13            writer.write(b&#034;echo: &#034; &#043; data)<br \/>\n14            await writer.drain() # \u91cd\u8981&#xff1a;\u80cc\u538b\u63a7\u5236&#xff0c;\u9632\u6b62\u5185\u5b58\u6ea2\u51fa<br \/>\n15    except Exception as e:<br \/>\n16        print(e)<br \/>\n17    finally:<br \/>\n18        writer.close()<br \/>\n19        await writer.wait_closed()<br \/>\n20<br \/>\n21async def main():<br \/>\n22    server &#061; await asyncio.start_server(handle_client, &#039;127.0.0.1&#039;, 5005)<br \/>\n23    async with server:<br \/>\n24        await server.serve_forever()<br \/>\n25<br \/>\n26if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n27    asyncio.run(main())<\/p>\n<p>\u6ce8\u610f&#xff1a;\u00a0await writer.drain()\u00a0\u975e\u5e38\u91cd\u8981&#xff0c;\u5b83\u5b9e\u73b0\u4e86\u80cc\u538b&#xff08;Backpressure&#xff09;&#xff0c;\u5f53\u53d1\u9001\u7f13\u51b2\u533a\u6ee1\u65f6\u6682\u505c\u534f\u7a0b&#xff0c;\u9632\u6b62\u5185\u5b58\u66b4\u6da8\u3002<\/p>\n<h5>&#x1f3c6; \u516d\u3001 \u7ec8\u6781\u5b9e\u6218&#xff1a;\u5f02\u6b65\u591a\u4eba\u804a\u5929\u5ba4<\/h5>\n<p>\u7ed3\u5408\u4e0a\u8ff0\u77e5\u8bc6&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301\u591a\u4eba\u7684\u804a\u5929\u5ba4\u3002<\/p>\n<p>\u6838\u5fc3\u903b\u8f91 (chat_server.py)&#xff1a;<\/p>\n<li>\u7ef4\u62a4\u4e00\u4e2a\u5168\u5c40\u7684\u00a0clients\u00a0\u5b57\u5178&#xff0c;\u5b58\u50a8\u6240\u6709\u6d3b\u8dc3\u7684 writer\u3002<\/li>\n<li>\u6536\u5230\u6d88\u606f\u540e&#xff0c;\u904d\u5386\u00a0clients\u00a0\u5e7f\u64ad\u7ed9\u6240\u6709\u4eba\u3002<\/li>\n<li>\u5904\u7406\u7528\u6237\u4e0a\u4e0b\u7ebf\u901a\u77e5\u3002<\/li>\n<p>python<\/p>\n<p>\u7f16\u8f91<\/p>\n<p>1import asyncio<br \/>\n2<br \/>\n3clients &#061; {} # \u5b58\u50a8 {writer: username}<br \/>\n4<br \/>\n5async def broadcast(message, exclude&#061;None):<br \/>\n6    &#034;&#034;&#034;\u5e7f\u64ad\u6d88\u606f\u7ed9\u6240\u6709\u5ba2\u6237\u7aef&#034;&#034;&#034;<br \/>\n7    dead_clients &#061; []<br \/>\n8    for writer in clients:<br \/>\n9        if writer is exclude: continue<br \/>\n10        try:<br \/>\n11            writer.write(message.encode(&#039;utf-8&#039;) &#043; b&#039;\\\\n&#039;)<br \/>\n12            await writer.drain()<br \/>\n13        except ConnectionError:<br \/>\n14            dead_clients.append(writer)<br \/>\n15<br \/>\n16    # \u6e05\u7406\u5931\u6548\u8fde\u63a5<br \/>\n17    for writer in dead_clients:<br \/>\n18        clients.pop(writer, None)<br \/>\n19        writer.close()<br \/>\n20<br \/>\n21async def handle_client(reader, writer):<br \/>\n22    # 1. \u83b7\u53d6\u7528\u6237\u540d<br \/>\n23    await send_line(writer, &#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d&#xff1a;&#034;)<br \/>\n24    raw_name &#061; await reader.readline()<br \/>\n25    username &#061; raw_name.decode().strip()<br \/>\n26    clients[writer] &#061; username<br \/>\n27<br \/>\n28    await broadcast(f&#034;[\u7cfb\u7edf] {username} \u52a0\u5165\u804a\u5929\u5ba4&#034;)<br \/>\n29<br \/>\n30    # 2. \u5faa\u73af\u63a5\u6536\u6d88\u606f<br \/>\n31    try:<br \/>\n32        while True:<br \/>\n33            raw &#061; await reader.readline()<br \/>\n34            if not raw: break<br \/>\n35            msg &#061; raw.decode().strip()<br \/>\n36            if msg &#061;&#061; &#039;\/quit&#039;: break<br \/>\n37            await broadcast(f&#034;[{username}] {msg}&#034;)<br \/>\n38    finally:<br \/>\n39        # 3. \u6e05\u7406\u8d44\u6e90<br \/>\n40        if writer in clients:<br \/>\n41            clients.pop(writer)<br \/>\n42            await broadcast(f&#034;[\u7cfb\u7edf] {username} \u79bb\u5f00&#034;)<br \/>\n43        writer.close()<br \/>\n44        await writer.wait_closed()<br \/>\n45<br \/>\n46async def send_line(writer, msg):<br \/>\n47    writer.write(msg.encode(&#039;utf-8&#039;) &#043; b&#039;\\\\n&#039;)<br \/>\n48    await writer.drain()<br \/>\n49<br \/>\n50# \u542f\u52a8\u670d\u52a1<br \/>\n51asyncio.run(asyncio.start_server(handle_client, &#039;127.0.0.1&#039;, 6000).serve_forever())<\/p>\n<h5>&#x1f6e1;\ufe0f \u4e03\u3001 \u751f\u4ea7\u73af\u5883\u6ce8\u610f\u4e8b\u9879<\/h5>\n<p>\u4ece\u201c\u80fd\u8dd1\u201d\u5230\u201c\u4e0a\u7ebf\u201d&#xff0c;\u4f60\u8fd8\u9700\u8981\u8003\u8651&#xff1a;<\/p>\n<li>\u8d85\u65f6\u8bbe\u7f6e&#xff1a;\u5fc5\u987b\u8bbe\u7f6e\u00a0timeout&#xff0c;\u9632\u6b62\u8fde\u63a5\u50f5\u6b7b\u3002<\/li>\n<li>\u5f02\u5e38\u5904\u7406&#xff1a;ConnectionResetError\u3001BrokenPipeError\u00a0\u662f\u5bb6\u5e38\u4fbf\u996d&#xff0c;\u8981\u4f18\u96c5\u6355\u83b7\u3002<\/li>\n<li>\u5b89\u5168\u6027&#xff1a;\n<ul>\n<li>\u9650\u5236\u6d88\u606f\u6700\u5927\u957f\u5ea6&#xff0c;\u9632\u6b62\u5185\u5b58\u6ea2\u51fa\u653b\u51fb\u3002<\/li>\n<li>\u4e0d\u8981\u76f4\u63a5\u00a0pickle.loads\u00a0\u4e0d\u53ef\u4fe1\u6570\u636e\u3002<\/li>\n<li>\u751f\u4ea7\u73af\u5883\u52a1\u5fc5\u4f7f\u7528 TLS\/SSL \u52a0\u5bc6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5fc3\u8df3\u673a\u5236&#xff1a;\u957f\u8fde\u63a5\u9700\u8981\u5fc3\u8df3\u5305&#xff08;Ping\/Pong&#xff09;\u6765\u68c0\u6d4b\u8fde\u63a5\u662f\u5426\u5b58\u6d3b\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"407\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260813094048-6a7d9120cba8a.png\" width=\"455\" \/><\/li>\n<h5>&#x1f4cc; \u603b\u7ed3<\/h5>\n<p>Python \u7f51\u7edc\u7f16\u7a0b\u7684\u5b66\u4e60\u8def\u5f84&#xff1a;<\/p>\n<li>\u5165\u95e8&#xff1a;\u638c\u63e1\u00a0socket\u00a0\u963b\u585e\u5f0f\u7f16\u7a0b&#xff0c;\u7406\u89e3 TCP \u4e09\u6b21\u63e1\u624b\u4e0e\u56db\u6b21\u6325\u624b\u3002<\/li>\n<li>\u8fdb\u9636&#xff1a;\u7406\u89e3\u201c\u7c98\u5305\u201d\u95ee\u9898&#xff0c;\u5b66\u4f1a\u8bbe\u8ba1\u957f\u5ea6\u524d\u7f00\u534f\u8bae\u3002<\/li>\n<li>\u9ad8\u5e76\u53d1&#xff1a;\u4ece\u591a\u7ebf\u7a0b\u8f6c\u5411\u00a0asyncio&#xff0c;\u7406\u89e3\u4e8b\u4ef6\u5faa\u73af\u4e0e\u534f\u7a0b\u3002<\/li>\n<li>\u5de5\u7a0b\u5316&#xff1a;\u52a0\u5165\u8d85\u65f6\u3001\u91cd\u8bd5\u3001\u5fc3\u8df3\u3001\u65e5\u5fd7\u4e0e\u76d1\u63a7\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f4a1; \u4e00\u3001 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u7f51\u7edc\u7f16\u7a0b\u5230\u5e95\u5728\u505a\u4ec0\u4e48&#xff1f;\u5f88\u591a\u4eba\u89c9\u5f97\u7f51\u7edc\u7f16\u7a0b\u96be&#xff0c;\u662f\u56e0\u4e3a\u88ab IP\u3001\u7aef\u53e3\u3001Socket\u3001TCP\u3001UDP\u3001\u963b\u585e\u3001\u975e\u963b\u585e\u7b49\u540d\u8bcd\u5413\u5230\u4e86\u3002\u5176\u5b9e\u6838\u5fc3\u903b\u8f91\u53ea\u6709\u4e00\u53e5\u8bdd&#xff1a;\u8ba9\u4e24\u4e2a\u8fdb\u7a0b\u6309\u7167\u7ea6\u5b9a&#xff0c;\u5728\u7f51\u7edc\u4e2d\u53ef\u9760\u6216\u9ad8\u6548\u5730\u4ea4\u6362\u5b57\u8282\u3002\u8fd9\u91cc\u5305\u542b\u4e09\u4e2a\u5173\u952e\u8bcd&#xff1a;\u4e24\u4e2a\u8fdb\u7a0b&#xff1a;\u901a\u5e38\u662f\u5ba2\u6237\u7aef&#xff08;Client&#xff09;\u548c\u670d\u52a1\u7aef&#xff08;Server&#xff09;\u3002\u4ea4\u6362\u5b57\u8282&amp;<\/p>\n","protected":false},"author":2,"featured_media":94082,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,884,205,78,396],"topic":[],"class_list":["post-94083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-python","tag-socket","tag-tcp-ip","tag-78","tag-396"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \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\/94083.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"&#x1f4a1; \u4e00\u3001 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u7f51\u7edc\u7f16\u7a0b\u5230\u5e95\u5728\u505a\u4ec0\u4e48&#xff1f;\u5f88\u591a\u4eba\u89c9\u5f97\u7f51\u7edc\u7f16\u7a0b\u96be&#xff0c;\u662f\u56e0\u4e3a\u88ab IP\u3001\u7aef\u53e3\u3001Socket\u3001TCP\u3001UDP\u3001\u963b\u585e\u3001\u975e\u963b\u585e\u7b49\u540d\u8bcd\u5413\u5230\u4e86\u3002\u5176\u5b9e\u6838\u5fc3\u903b\u8f91\u53ea\u6709\u4e00\u53e5\u8bdd&#xff1a;\u8ba9\u4e24\u4e2a\u8fdb\u7a0b\u6309\u7167\u7ea6\u5b9a&#xff0c;\u5728\u7f51\u7edc\u4e2d\u53ef\u9760\u6216\u9ad8\u6548\u5730\u4ea4\u6362\u5b57\u8282\u3002\u8fd9\u91cc\u5305\u542b\u4e09\u4e2a\u5173\u952e\u8bcd&#xff1a;\u4e24\u4e2a\u8fdb\u7a0b&#xff1a;\u901a\u5e38\u662f\u5ba2\u6237\u7aef&#xff08;Client&#xff09;\u548c\u670d\u52a1\u7aef&#xff08;Server&#xff09;\u3002\u4ea4\u6362\u5b57\u8282&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94083.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-13T09:40:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260813094048-6a7d9120cba8a.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94083.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94083.html\",\"name\":\"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-13T09:40:50+00:00\",\"dateModified\":\"2026-08-13T09:40:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94083.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94083.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94083.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218\"}]},{\"@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":"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \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\/94083.html","og_locale":"zh_CN","og_type":"article","og_title":"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"&#x1f4a1; \u4e00\u3001 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u7f51\u7edc\u7f16\u7a0b\u5230\u5e95\u5728\u505a\u4ec0\u4e48&#xff1f;\u5f88\u591a\u4eba\u89c9\u5f97\u7f51\u7edc\u7f16\u7a0b\u96be&#xff0c;\u662f\u56e0\u4e3a\u88ab IP\u3001\u7aef\u53e3\u3001Socket\u3001TCP\u3001UDP\u3001\u963b\u585e\u3001\u975e\u963b\u585e\u7b49\u540d\u8bcd\u5413\u5230\u4e86\u3002\u5176\u5b9e\u6838\u5fc3\u903b\u8f91\u53ea\u6709\u4e00\u53e5\u8bdd&#xff1a;\u8ba9\u4e24\u4e2a\u8fdb\u7a0b\u6309\u7167\u7ea6\u5b9a&#xff0c;\u5728\u7f51\u7edc\u4e2d\u53ef\u9760\u6216\u9ad8\u6548\u5730\u4ea4\u6362\u5b57\u8282\u3002\u8fd9\u91cc\u5305\u542b\u4e09\u4e2a\u5173\u952e\u8bcd&#xff1a;\u4e24\u4e2a\u8fdb\u7a0b&#xff1a;\u901a\u5e38\u662f\u5ba2\u6237\u7aef&#xff08;Client&#xff09;\u548c\u670d\u52a1\u7aef&#xff08;Server&#xff09;\u3002\u4ea4\u6362\u5b57\u8282&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/94083.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-13T09:40:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260813094048-6a7d9120cba8a.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94083.html","url":"https:\/\/www.wsisp.com\/helps\/94083.html","name":"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-13T09:40:50+00:00","dateModified":"2026-08-13T09:40:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94083.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94083.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94083.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[\u7279\u6b8a\u5b57\u7b26] Python \u7f51\u7edc\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u7cbe\u901a\uff1aSocket\u3001\u5e76\u53d1\u3001\u5f02\u6b65\u4e0e\u5b9e\u6218"}]},{"@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\/94083","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=94083"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/94082"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94083"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}