{"id":2811,"date":"2025-03-16T12:06:08","date_gmt":"2025-03-16T04:06:08","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2811"},"modified":"2025-03-16T12:06:08","modified_gmt":"2025-03-16T04:06:08","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%9asocket-%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%9asocket-%e5%a5%97%e6%8e%a5%e5%ad%97%e7%bc%96%e7%a8%8b\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b\uff1asocket\u00a0\u5957\u63a5\u5b57\u7f16\u7a0b"},"content":{"rendered":"\n<p><br>\u5b66\u4e60<strong>Python3 \u7f51\u7edc\u7f16\u7a0b<\/strong>\uff0c\u4ece<strong>\u57fa\u7840 <code>socket<\/code> \u7f16\u7a0b<\/strong>\u5f00\u59cb\uff0c\u9010\u6b65\u6df1\u5165\u89e3\u6790 Python3 \u7f51\u7edc\u7f16\u7a0b\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. <code>socket<\/code> \u5957\u63a5\u5b57\u7f16\u7a0b<\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1.1 <code>socket<\/code> \u57fa\u7840<\/strong><\/h2>\n\n\n\n<p>Python \u63d0\u4f9b <code>socket<\/code> \u6a21\u5757\u6765\u8fdb\u884c\u5e95\u5c42\u7f51\u7edc\u7f16\u7a0b\uff0c\u652f\u6301 <strong>TCP\uff08\u6d41\u5f0f\u534f\u8bae\uff09<\/strong> \u548c <strong>UDP\uff08\u6570\u636e\u62a5\u534f\u8bae\uff09<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1.1 <code>socket<\/code> \u76f8\u5173\u6982\u5ff5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IP \u5730\u5740<\/strong>\uff1a\u7528\u4e8e\u6807\u8bc6\u7f51\u7edc\u4e2d\u7684\u8bbe\u5907\uff08\u5982 <code>192.168.1.1<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7aef\u53e3\u53f7<\/strong>\uff1a\u7528\u4e8e\u533a\u5206\u540c\u4e00\u8bbe\u5907\u4e0a\u7684\u4e0d\u540c\u5e94\u7528\u7a0b\u5e8f\uff08\u5982 HTTP \u7aef\u53e3 <code>80<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u534f\u8bae<\/strong>\uff1a<\/li>\n\n\n\n<li><strong>TCP\uff08\u4f20\u8f93\u63a7\u5236\u534f\u8bae\uff09<\/strong>\uff1a\u9762\u5411\u8fde\u63a5\uff0c\u4fdd\u8bc1\u6570\u636e\u53ef\u9760\u4f20\u8f93\u3002<\/li>\n\n\n\n<li><strong>UDP\uff08\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff09<\/strong>\uff1a\u65e0\u8fde\u63a5\uff0c\u901f\u5ea6\u5feb\uff0c\u4f46\u4e0d\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u3002<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef<\/strong>\uff1a<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\uff08Server\uff09<\/strong>\uff1a\u76d1\u542c\u7aef\u53e3\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u5ba2\u6237\u7aef\uff08Client\uff09<\/strong>\uff1a\u5411\u670d\u52a1\u5668\u53d1\u8d77\u8fde\u63a5\u5e76\u4f20\u8f93\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1.2 <code>socket<\/code> TCP \u7f16\u7a0b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2.1 TCP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<p>\u4e00\u4e2a\u57fa\u672c\u7684 TCP \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\n# \u521b\u5efa TCP \u5957\u63a5\u5b57\uff08\u9ed8\u8ba4 SOCK_STREAM \u4e3a TCP\uff09\nserver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# \u7ed1\u5b9a IP \u548c\u7aef\u53e3\nserver_socket.bind((\"0.0.0.0\", 12345))\n\n# \u5f00\u59cb\u76d1\u542c\uff08\u6700\u591a 5 \u4e2a\u5ba2\u6237\u7aef\u7b49\u5f85\uff09\nserver_socket.listen(5)\nprint(\"\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u8fde\u63a5...\")\n\nwhile True:\n    # \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n    client_socket, addr = server_socket.accept()\n    print(f\"\u5ba2\u6237\u7aef {addr} \u8fde\u63a5\u6210\u529f\")\n\n    # \u63a5\u6536\u6570\u636e\uff08\u6700\u5927 1024 \u5b57\u8282\uff09\n    data = client_socket.recv(1024)\n    print(f\"\u6536\u5230\u6570\u636e\uff1a{data.decode()}\")\n\n    # \u53d1\u9001\u54cd\u5e94\n    client_socket.send(b\"Hello from server!\")\n\n    # \u5173\u95ed\u8fde\u63a5\n    client_socket.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2.2 TCP \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<p>\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u5668\u5e76\u53d1\u9001\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\n# \u521b\u5efa TCP \u5957\u63a5\u5b57\nclient_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# \u8fde\u63a5\u670d\u52a1\u5668\nclient_socket.connect((\"127.0.0.1\", 12345))\n\n# \u53d1\u9001\u6570\u636e\nclient_socket.send(b\"Hello Server!\")\n\n# \u63a5\u6536\u6570\u636e\nresponse = client_socket.recv(1024)\nprint(f\"\u670d\u52a1\u5668\u54cd\u5e94\uff1a{response.decode()}\")\n\n# \u5173\u95ed\u8fde\u63a5\nclient_socket.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2.3 TCP \u670d\u52a1\u5668 &#8211; \u591a\u7ebf\u7a0b\u652f\u6301<\/strong><\/h3>\n\n\n\n<p>\u591a\u4e2a\u5ba2\u6237\u7aef\u5e76\u53d1\u8fde\u63a5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\nimport threading\n\ndef handle_client(client_socket, addr):\n    print(f\"\u5ba2\u6237\u7aef {addr} \u8fde\u63a5\u6210\u529f\")\n    data = client_socket.recv(1024)\n    print(f\"\u6536\u5230\u6570\u636e\uff1a{data.decode()}\")\n    client_socket.send(b\"Hello from server!\")\n    client_socket.close()\n\nserver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nserver_socket.bind((\"0.0.0.0\", 12345))\nserver_socket.listen(5)\nprint(\"\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u8fde\u63a5...\")\n\nwhile True:\n    client_socket, addr = server_socket.accept()\n    thread = threading.Thread(target=handle_client, args=(client_socket, addr))\n    thread.start()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1.3 <code>socket<\/code> UDP \u7f16\u7a0b<\/strong><\/h2>\n\n\n\n<p>UDP \u662f\u65e0\u8fde\u63a5\u7684\uff0c\u9002\u7528\u4e8e\u5bf9\u901f\u5ea6\u8981\u6c42\u9ad8\uff0c\u4f46\u4e0d\u8981\u6c42\u6570\u636e\u53ef\u9760\u6027\u7684\u573a\u666f\uff0c\u5982\u89c6\u9891\u6d41\u6216\u6e38\u620f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3.1 UDP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nserver_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nserver_socket.bind((\"0.0.0.0\", 12345))\n\nprint(\"UDP \u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u6570\u636e...\")\n\nwhile True:\n    data, addr = server_socket.recvfrom(1024)\n    print(f\"\u6536\u5230\u6765\u81ea {addr} \u7684\u6570\u636e\uff1a{data.decode()}\")\n    server_socket.sendto(b\"Hello from UDP Server!\", addr)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3.2 UDP \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nclient_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n\n# \u53d1\u9001\u6570\u636e\nclient_socket.sendto(b\"Hello UDP Server!\", (\"127.0.0.1\", 12345))\n\n# \u63a5\u6536\u670d\u52a1\u5668\u54cd\u5e94\ndata, server = client_socket.recvfrom(1024)\nprint(f\"\u670d\u52a1\u5668\u54cd\u5e94\uff1a{data.decode()}\")\n\nclient_socket.close()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1.4 \u5957\u63a5\u5b57\u9009\u9879<\/strong><\/h2>\n\n\n\n<p>\u4f7f\u7528 <code>setsockopt<\/code> \u8bbe\u7f6e\u4e0d\u540c\u7684\u5957\u63a5\u5b57\u884c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# \u8bbe\u7f6e SO_REUSEADDR \u5141\u8bb8\u7aef\u53e3\u590d\u7528\uff0c\u907f\u514d\u201c\u5730\u5740\u5df2\u5728\u4f7f\u7528\u201d\u9519\u8bef\nsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n\n# \u7ed1\u5b9a\u7aef\u53e3\nsock.bind((\"0.0.0.0\", 12345))\nsock.listen(5)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u7ee7\u7eed\u8bb2\u89e3 <code>asyncio<\/code> \u5f02\u6b65\u7f51\u7edc\u7f16\u7a0b\uff0c\u5305\u62ec <code>asyncio<\/code> TCP \u670d\u52a1\u5668\u3001<code>asyncio<\/code> WebSocket \u670d\u52a1\u5668\u7b49\u5185\u5bb9\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60Python3 \u7f51\u7edc\u7f16\u7a0b\uff0c\u4ece\u57fa\u7840 socket \u7f16\u7a0b\u5f00\u59cb\uff0c\u9010\u6b65\u6df1\u5165\u89e3\u6790 Python3 \u7f51\u7edc\u7f16\u7a0b\u7684\u6240\u6709\u5185\u5bb9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2811"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2811\/revisions"}],"predecessor-version":[{"id":2813,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2811\/revisions\/2813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2812"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}