{"id":243,"date":"2025-07-12T08:04:17","date_gmt":"2025-07-12T00:04:17","guid":{"rendered":"https:\/\/www.rbfwq.com\/blog\/?p=243"},"modified":"2025-07-12T08:04:17","modified_gmt":"2025-07-12T00:04:17","slug":"%e4%bd%bf%e7%94%a8python%e8%a7%a3%e7%a0%81%e7%bd%91%e7%bb%9c%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%b5%81%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.idc61.com\/blog\/243.html","title":{"rendered":"\u4f7f\u7528Python\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idc61.com\/blog\/wp-content\/uploads\/2025\/06\/m8dO7mS.jpg\" alt=\"\u4f7f\u7528Python\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u5b8c\u6574\u6307\u5357\" title=\"\u4f7f\u7528Python\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u5b8c\u6574\u6307\u5357\"><\/p>\n<p><h3>1. \u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u89e3\u7801\u7684\u57fa\u672c\u77e5\u8bc6<\/h3>\n<\/p>\n<p>\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u901a\u5e38\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u4f20\u8f93\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u5f88\u591a\u534f\u8bae\u90fd\u4f9d\u8d56\u4e8e\u4e8c\u8fdb\u5236\u6d41\u6765\u4f20\u9012\u4fe1\u606f\u3002\u5728Python\u4e2d\uff0c\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u65b9\u6cd5\u4e3b\u8981\u6d89\u53ca\u5bf9\u5b57\u8282\u8fdb\u884c\u8f6c\u6362\uff0c\u5305\u62ec\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3001\u6574\u578b\u6216\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3002\u4e86\u89e3\u6570\u636e\u7684\u683c\u5f0f\u548c\u7ed3\u6784\u662f\u89e3\u7801\u7684\u524d\u63d0\uff0c\u63e1\u6709\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u51e0\u4e4e\u6240\u6709\u4e0e\u7f51\u7edc\u6570\u636e\u4ea4\u4e92\u7684\u4efb\u52a1\u90fd\u80fd\u987a\u5229\u8fdb\u884c\u3002 <\/p>\n<\/p>\n<p>\n<h3>2. Python\u4e2d\u5bf9\u4e8c\u8fdb\u5236\u6d41\u7684\u5904\u7406<\/h3>\n<\/p>\n<p>\u4e8c\u8fdb\u5236\u6d41\u5728Python\u4e2d\u901a\u5e38\u4f7f\u7528bytes\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4e3b\u8981\u770b\u4f60\u9700\u8981\u89e3\u6790\u4ec0\u4e48\u7c7b\u578b\u7684\u4fe1\u606f\u3002\u6bd4\u5982\uff0c\u63a5\u6536\u5230\u7684\u4e8c\u8fdb\u5236\u6d41\u53ef\u80fd\u662f\u56fe\u50cf\u3001\u97f3\u9891\u3001\u6587\u672c\u6216\u81ea\u5b9a\u4e49\u534f\u8bae\u7684\u6570\u636e\u3002\u5728\u89e3\u7801\u65f6\uff0c\u9996\u5148\u9700\u660e\u786e\u6570\u636e\u7684\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528\u5408\u9002\u7684\u89e3\u7801\u65b9\u5f0f\u3002 <\/p>\n<\/p>\n<p>\n<h3>3. \u5982\u4f55\u89e3\u7801\u4e8c\u8fdb\u5236\u6d41<\/h3>\n<\/p>\n<p>\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u89e3\u7801\u51fd\u6570\u6216\u8005\u76f8\u5173\u5e93\u3002\u5728\u6536\u5230\u4e8c\u8fdb\u5236\u6d41\u540e\uff0c\u4e00\u822c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>1. \u9996\u5148\u63a5\u6536\u7f51\u7edc\u6570\u636e\uff0c\u901a\u5e38\u5229\u7528socket\u6765\u83b7\u53d6\u4e8c\u8fdb\u5236\u6d41\u3002<\/p>\n<p>2. \u6839\u636e\u534f\u8bae\u6307\u5b9a\u7684\u683c\u5f0f\u8fdb\u884c\u89e3\u7801\uff0c\u5e38\u89c1\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528UTF-8\u89e3\u7801\u6587\u672c\u3001struct\u6a21\u5757\u6765\u89e3\u6790\u7ed3\u6784\u4f53\u7b49\u3002<\/p>\n<p>3. \u6700\u540e\uff0c\u5c06\u89e3\u6790\u540e\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u6216\u5b58\u50a8\uff0c\u6216\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4ecesocket\u63a5\u6536\u6570\u636e\u5e76\u8fdb\u884c\u89e3\u7801\uff1a<\/p>\n<p><pre><code>import socket<\/p>\r\n\r\n<p># \u521b\u5efasocket\u5bf9\u8c61<\/p>\r\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\r\n\r\n<p># \u8fde\u63a5\u5230\u670d\u52a1\u7aef<\/p>\r\n<p>s.connect(('localhost', 12345))<\/p>\r\n\r\n<p># \u63a5\u6536\u6570\u636e<\/p>\r\n<p>data = s.recv(1024)<\/p>\r\n\r\n<p># \u5047\u8bbe\u63a5\u6536\u5230\u7684\u662fUTF-8\u7f16\u7801\u7684\u6587\u672c<\/p>\r\n<p>decoded_data = data.decode('utf-8')<\/p>\r\n\r\n<p>print(decoded_data)<\/p>\r\n<p>s.close()<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>4. \u63a8\u8350\u4f7f\u7528\u7684\u5e93\u548c\u5de5\u5177<\/h3>\n<\/p>\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u4e8c\u8fdb\u5236\u6d41\u5904\u7406\uff0cPython\u6709\u591a\u79cd\u5e93\u53ef\u4ee5\u63a8\u8350\u3002\u6bd4\u5982\uff0cstruct\u5e93\u53ef\u4ee5\u5904\u7406C\u8bed\u8a00\u4e2d\u7684\u7ed3\u6784\u4f53\uff0c\u63d0\u4f9b\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u6253\u5305\u548c\u89e3\u5305\u6570\u636e\u3002\u53e6\u4e00\u4e2a\u63a8\u8350\u662f\u7528protobuf\uff0c\u8fd9\u662fGoogle\u7684\u4e00\u6b3e\u5e95\u5c42\u6570\u636e\u5e8f\u5217\u5316\u6846\u67b6\uff0c\u7279\u522b\u9002\u7528\u4e8e\u7f51\u7edc\u4f20\u8f93\u4e2d\u5bf9\u6027\u80fd\u6709\u9ad8\u8981\u6c42\u7684\u60c5\u51b5\u3002\u5bf9\u4e8eJSON\u6570\u636e\uff0c\u4f7f\u7528\u5185\u5efa\u7684json\u6a21\u5757\u5c31\u975e\u5e38\u65b9\u4fbf\u3002\u8fd9\u4e9b\u5e93\u7684\u4f7f\u7528\u53ef\u4ee5\u5927\u5e45\u5ea6\u51cf\u5c11\u624b\u5de5\u89e3\u7801\u7684\u5de5\u4f5c\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p>\n<h3>5. \u4e3a\u4ec0\u4e48\u4e8c\u8fdb\u5236\u6d41\u9700\u8981\u89e3\u7801<\/h3>\n<\/p>\n<p><b>\u4f55\u8c13\u4e8c\u8fdb\u5236\u6d41\u89e3\u7801\uff1f<\/b>\u4e8c\u8fdb\u5236\u6d41\u89e3\u7801\u6307\u7684\u662f\u5c06\u4ece\u7f51\u7edc\u4e2d\u63a5\u6536\u5230\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u8bfb\u4e3a\u4eba\u4eec\u53ef\u4ee5\u7406\u89e3\u7684\u683c\u5f0f\uff0c\u65e0\u8bba\u662f\u6587\u672c\u3001\u56fe\u50cf\u6216\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u3002\u89e3\u7801\u7684\u8fc7\u7a0b\u5c31\u662f\u628a\u8ba1\u7b97\u673a\u5b58\u50a8\u76840\u548c1\u7ffb\u8bd1\u6210\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7684\u5f62\u5f0f\uff0c\u8fd9\u662f\u7f51\u7edc\u6570\u636e\u4ea4\u4e92\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u73af\u8282\u3002 <\/p>\n<\/p>\n<p>\n<h3>6. \u6570\u636e\u53ef\u4ee5\u4ee5\u54ea\u4e9b\u5f62\u5f0f\u89e3\u7801\uff1f<\/h3>\n<\/p>\n<p><b>\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u5e38\u89c1\u5f62\u5f0f\u662f\u4ec0\u4e48\uff1f<\/b>\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u89e3\u7801\u5f62\u5f0f\u901a\u5e38\u4e3b\u8981\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u52a8\u6570\u3001\u5217\u4e3e\u7c7b\u578b\u548c\u5b57\u5178\u7b49\u5f62\u5f0f\u3002\u6bd4\u5982\uff0c\u4ece\u7f51\u7edc\u63a5\u6536\u5230\u6e29\u5ea6\u6570\u636e\u65f6\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u89e3\u7801\u4e3a\u6d6e\u52a8\u6570\u5f62\u5f0f\uff1b\u4ece\u63a5\u6536\u7684JSON\u6570\u636e\u6d41\u4e2d\uff0c\u5219\u4f1a\u89e3\u7801\u4e3a\u5b57\u5178\u3002\u8fd9\u4e9b\u89e3\u7801\u5f62\u5f0f\u5e94\u5f53\u6839\u636e\u5177\u4f53\u7684\u573a\u666f\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<p>\n<h3>7. \u5982\u4f55\u5b9a\u4f4d\u89e3\u7801\u4e2d\u7684\u95ee\u9898<\/h3>\n<\/p>\n<p><b>\u5728\u89e3\u7801\u4e8c\u8fdb\u5236\u6d41\u65f6\u4f1a\u9047\u5230\u54ea\u4e9b\u95ee\u9898\uff1f<\/b>\u5728\u89e3\u7801\u4e2d\uff0c\u5e38\u89c1\u7684\u95ee\u9898\u6709\uff1a\u63a5\u6536\u5230\u7684\u6570\u636e\u4e0d\u5b8c\u6574\u3001\u89e3\u7801\u683c\u5f0f\u4e0d\u5339\u914d\u7b49\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u5e76\u5728\u8c03\u8bd5\u65f6\u8be6\u7ec6\u6253\u5370\u63a5\u6536\u5230\u7684\u6570\u636e\u72b6\u6001\u3002\u540c\u65f6\uff0c\u660e\u786e\u534f\u8bae\u89c4\u8303\uff0c\u786e\u4fdd\u53d1\u9001\u7aef\u548c\u63a5\u6536\u7aef\u7684\u6570\u636e\u683c\u5f0f\u4e00\u81f4\uff0c\u5c06\u6781\u5927\u51cf\u5c11\u89e3\u7801\u9519\u8bef\u7684\u98ce\u9669\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u89e3\u7801\u7684\u57fa\u672c\u77e5\u8bc6 \u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u901a\u5e38\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u4f20\u8f93\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u5f88\u591a\u534f\u8bae\u90fd\u4f9d\u8d56\u4e8e\u4e8c\u8fdb\u5236\u6d41\u6765\u4f20\u9012\u4fe1\u606f\u3002\u5728Python\u4e2d\uff0c\u89e3\u7801\u7f51\u7edc\u4e8c\u8fdb\u5236\u6d41\u7684\u65b9\u6cd5\u4e3b\u8981\u6d89\u53ca\u5bf9\u5b57\u8282\u8fdb\u884c\u8f6c\u6362\uff0c\u5305\u62ec\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3001\u6574\u578b\u6216\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784&#8230;<\/p>\n","protected":false},"author":1,"featured_media":244,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[59,80,81],"topic":[],"class_list":["post-243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jishu","tag-python","tag-80","tag-81"],"_links":{"self":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":1,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions\/246"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media\/244"}],"wp:attachment":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/tags?post=243"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/topic?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}