{"id":204775,"date":"2025-05-29T15:18:14","date_gmt":"2025-05-29T07:18:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204775\/"},"modified":"2025-05-29T15:18:14","modified_gmt":"2025-05-29T07:18:14","slug":"python-socket-%e8%81%8a%e5%a4%a9%e5%ae%a4%e6%95%b0%e6%8d%ae%e4%bc%a0%e8%be%93%e9%97%ae%e9%a2%98%ef%bc%9a%e4%b8%ba%e4%bd%95%e7%ac%ac%e4%b8%80%e4%b8%aa%e7%94%a8%e6%88%b7%e6%97%a0%e6%b3%95%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204775\/","title":{"rendered":"Python Socket \u804a\u5929\u5ba4\u6570\u636e\u4f20\u8f93\u95ee\u9898\uff1a\u4e3a\u4f55\u7b2c\u4e00\u4e2a\u7528\u6237\u65e0\u6cd5\u63a5\u6536\u6d88\u606f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python Socket \u804a\u5929\u5ba4\u6570\u636e\u4f20\u8f93\u95ee\u9898\uff1a\u4e3a\u4f55\u7b2c\u4e00\u4e2a\u7528\u6237\u65e0\u6cd5\u63a5\u6536\u6d88\u606f\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aPython Socket \u804a\u5929\u5ba4\u6570\u636e\u4f20\u8f93\u95ee\u9898\uff1a\u4e3a\u4f55\u7b2c\u4e00\u4e2a\u7528\u6237\u65e0\u6cd5\u63a5\u6536\u6d88\u606f\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/1731725498673808ba4ecf5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 python \u4e2d\u4f7f\u7528 socket \u7f16\u7a0b\u6784\u5efa\u7b80\u6613\u804a\u5929\u5ba4\u65f6\u9047\u5230\u7684\u6570\u636e\u4f20\u8f93\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u521b\u5efa\u804a\u5929\u5ba4\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4ee5\u4e0b\u6570\u636e\u4f20\u8f93\u95ee\u9898\uff1a<\/p>\n<p><strong>\u80cc\u666f\uff1a<\/strong><\/p>\n<p>\u5df2\u521b\u5efa\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c\u4f46\u9047\u5230\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u5ba2\u6237\u7aef\u4e3a\u4e0d\u540c\u7528\u6237\u540d\u65f6\uff0c\u4f1a\u51fa\u73b0\u7b2c\u4e00\u4e2a\u7528\u6237\u65e0\u6cd5\u63a5\u6536\u6d88\u606f\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u95ee\u9898\u5206\u6790\uff1a<\/strong><\/p>\n<p><strong>1. \u53d1\u9001\u6570\u636e\u683c\u5f0f\u9519\u8bef\uff1a<\/strong><\/p>\n<p>\u5728\u53d1\u9001\u6570\u636e\u65f6\uff0c\u672a\u6307\u5b9a\u7528\u6237\u540d\uff0c\u5bfc\u81f4\u63a5\u6536\u65b9\u65e0\u6cd5\u8bc6\u522b\u53d1\u9001\u8005\u3002\u5e94\u8be5\u5728\u5c06\u6d88\u606f\u53d1\u9001\u7ed9\u5176\u4ed6\u5ba2\u6237\u7aef\u65f6\u6dfb\u52a0\u7528\u6237\u540d\u3002<\/p>\n<p><strong>2. \u63a5\u6536\u6570\u636e\u65f6\u4e22\u5931\u6570\u636e\uff1a<\/strong><\/p>\n<p>\u5728\u63a5\u6536\u6570\u636e\u65f6\uff0c\u4ee3\u7801\u4e2d\u5b58\u5728 try-except \u8bed\u53e5\u5c1d\u8bd5\u63a5\u6536\u6570\u636e\uff0c\u5982\u679c\u63a5\u6536\u5931\u8d25\u5219\u9000\u51fa\u5faa\u73af\u3002\u4f46\u9000\u51fa\u5faa\u73af\u4f1a\u4e22\u5931\u540e\u7eed\u7684\u6570\u636e\uff0c\u5bfc\u81f4\u4e00\u4e2a\u7528\u6237\u65e0\u6cd5\u6536\u5230\u6d88\u606f\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>1. \u53d1\u9001\u6570\u636e\u683c\u5f0f\u9519\u8bef\uff1a<\/strong><\/p>\n<pre>for user, addr in user_dict.items():\n    if user != present_user:\n        s.sendto(f\"{present_user}:{real_info}\".encode(\"utf-8\"), addr)<\/pre>\n<p><strong>2. \u63a5\u6536\u6570\u636e\u65f6\u4e22\u5931\u6570\u636e\uff1a<\/strong><\/p>\n<pre>def recv(socket_obj):\n    while True:\n        recv_data, addr = socket_obj.recvfrom(1024)\n        recv_data = recv_data.decode(\"utf-8\")<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Socket \u804a\u5929&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204775","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204775"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204775\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}