{"id":171614,"date":"2023-07-31T03:51:33","date_gmt":"2023-07-30T19:51:33","guid":{"rendered":"http:\/\/www.idc.net\/help\/171614\/"},"modified":"2023-07-31T03:51:33","modified_gmt":"2023-07-30T19:51:33","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython-socket%e6%a8%a1%e5%9d%97%e6%98%af%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/171614\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython socket\u6a21\u5757\u662f\u600e\u4e48\u4f7f\u7528\uff1f"},"content":{"rendered":"<p><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;text-align: justify\">\u5e0c\u671b\u5927\u5bb6\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u601d\u8def\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u5185\u5bb9\uff0c\u548c\u4ee5\u5f80\u4e0d\u540c\uff0c\u5f3a\u6709\u529b\u7684\u903b\u8f91\u601d\u7ef4\uff0c\u662f\u505a\u597d\u4f7f\u7528\u6a21\u5757\u7684\u57fa\u7840\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5728\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u4f1a\u4e0d\u4f1a\u5148\u505a\u597d\u89c4\u5212\u5185\u5bb9\uff0c\u5982\u679c\u6ca1\u6709\u89c4\u5212\u8bbe\u7f6e\u7684\u5c0f\u4f19\u4f34\uff0c\u53ef\u4ee5\u8ddf\u7740\u5c0f\u7f16\u6765\u5b66\u4e60\u4e0b\u8fd9\u4e2a\u8981\u600e\u4e48\u53bb\u4f7f\u7528\u3002<\/span><\/p>\n<p style=\"text-align:justify\"><strong><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\">socket\u7f16\u7a0b\u601d\u8def<\/span><\/strong><strong><\/strong><\/p>\n<p style=\"text-align:justify\"><strong><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\"><span style=\"font-family:\u5fae\u8f6f\u96c5\u9ed1\">\u670d\u52a1\u7aef<\/span> TCP<\/span><\/strong><\/p>\n<p style=\"text-align:justify\"><strong><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\">\u5ba2\u6237\u7aef<\/span><\/strong><\/p>\n<p style=\"text-align:justify\"><strong><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\">\u5b9e\u4f8b\u64cd\u4f5c<\/span><\/strong><strong><\/strong><\/p>\n<p style=\"text-align:justify\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\"><span style=\"font-family:\u5fae\u8f6f\u96c5\u9ed1\">\u6211\u4eec\u8c03\u7528<\/span> socket \u5bf9\u8c61\u7684 accept \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\uff0c\u5e76\u8fd4\u56de connection \u5bf9\u8c61\uff0c\u8868\u793a\u5df2\u8fde\u63a5\u5230\u5ba2\u6237\u7aef\u3002<\/span><\/p>\n<pre>#!\/usr\/bin\/python\n&nbsp;\n#&nbsp;-*-&nbsp;coding:&nbsp;UTF-8&nbsp;-*-\n&nbsp;\n#&nbsp;\u6587\u4ef6\u540d\uff1aserver.py\n&nbsp;\nimport&nbsp;socket&nbsp;#&nbsp;\u5bfc\u5165&nbsp;socket&nbsp;\u6a21\u5757\n&nbsp;\ns&nbsp;=&nbsp;socket.socket()&nbsp;#&nbsp;\u521b\u5efa&nbsp;socket&nbsp;\u5bf9\u8c61\n&nbsp;\nhost&nbsp;=&nbsp;socket.gethostname()&nbsp;#&nbsp;\u83b7\u53d6\u672c\u5730\u4e3b\u673a\u540d\n&nbsp;\nport&nbsp;=&nbsp;12345&nbsp;#&nbsp;\u8bbe\u7f6e\u7aef\u53e3\n&nbsp;\ns.bind((host,&nbsp;port))&nbsp;#&nbsp;\u7ed1\u5b9a\u7aef\u53e3\n&nbsp;\ns.listen(5)&nbsp;#&nbsp;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n&nbsp;\nwhile&nbsp;True:\n&nbsp;\nc,addr&nbsp;=&nbsp;s.accept()&nbsp;#&nbsp;\u5efa\u7acb\u5ba2\u6237\u7aef\u8fde\u63a5\n&nbsp;\nprint&nbsp;'\u8fde\u63a5\u5730\u5740\uff1a',&nbsp;addr\n&nbsp;\nc.send('\u6b22\u8fce\u8bbf\u95ee\uff01')\n&nbsp;\nc.close()&nbsp;#&nbsp;\u5173\u95ed\u8fde\u63a5<\/pre>\n<p style=\"text-align: justify\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 14px\"><span style=\"font-family:\u5fae\u8f6f\u96c5\u9ed1\">\u597d\u4e86\uff0c\u4ee5\u4e0a\u5c31\u662f<\/span>socket\u6a21\u5757\u7f16\u7a0b\u4e86\uff0c\u5185\u5bb9\u975e\u5e38\u7b80\u5355\uff0c\u5927\u5bb6\u5982\u679c\u4e0d\u7406\u89e3\u7684\uff0c\u53ef\u4ee5\u4ed4\u7ec6\u770b\u4e0b\u4e0a\u8ff0\u7684\u903b\u8f91\u601d\u7ef4\uff0c\u5728\u8111\u6d77\u4e2d\u5e03\u7f6e\u5370\u8c61\uff0c\u4e4b\u540e\u518d\u7f16\u5199\u65f6\u5019\u4f1a\u975e\u5e38\u987a\u5229\u54e6~<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e0c\u671b\u5927\u5bb6\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u601d\u8def\uff0c\u56e0\u4e3a\u8fd9\u90e8\u5206\u5185\u5bb9\uff0c\u548c\u4ee5\u5f80\u4e0d\u540c\uff0c\u5f3a\u6709\u529b\u7684\u903b\u8f91\u601d\u7ef4\uff0c\u662f\u505a\u597d\u4f7f\u7528\u6a21\u5757\u7684\u57fa\u7840\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5728\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-171614","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=171614"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171614\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=171614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=171614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=171614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}