{"id":19810,"date":"2025-04-19T02:22:37","date_gmt":"2025-04-18T18:22:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/19810.html"},"modified":"2025-04-19T02:22:37","modified_gmt":"2025-04-18T18:22:37","slug":"%e3%80%90python%e9%ab%98%e7%ba%a7%e3%80%91342-tcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%bc%80%e5%8f%91%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/19810.html","title":{"rendered":"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b"},"content":{"rendered":"<p>CS\u6a21\u5f0f&#xff1a;\u5ba2\u6237\u7aef-\u670d\u52a1\u7aef\u6a21\u5f0f<\/p>\n<p>TCP\u5ba2\u6237\u7aef\u5f00\u53d1\u6d41\u7a0b\u4ecb\u7ecd&#xff08;\u4e94\u6b65&#xff09;&#xff08;C\u7aef&#xff09; 1.\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61 2.\u548c\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5efa\u7acb\u8fde\u63a5 3.\u53d1\u9001\u6570\u636e 4.\u63a5\u6536\u6570\u636e 5.\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 TCP\u670d\u52a1\u7aef\u5f00\u53d1\u6d41\u7a0b&#xff08;\u4e03\u6b65&#xff09;&#xff08;S\u7aef&#xff09; 1.\u521b\u5efa\u670d\u52a1\u7aef\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61 2.\u7ed1\u5b9a\u7aef\u53e3\u53f7 3.\u8bbe\u7f6e\u76d1\u542c 4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42 5.\u63a5\u6536\u6570\u636e 6.\u53d1\u9001\u6570\u636e 7.\u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<h2>TCP\u5ba2\u6237\u7aef\u7a0b\u5e8f\u5f00\u53d1<\/h2>\n<p><span class=\"token keyword\">import<\/span> socket<\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e00\u6b65&#xff1a;\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\ntcp_client_socket <span class=\"token operator\">&#061;<\/span> socket<span class=\"token punctuation\">.<\/span>socket<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>AF_INET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SOCK_STREAM<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># socket.AF_INET\u8868\u793aIPV4&#xff0c;socket.SOCK_STREAM\u8868\u793aTCP\u534f\u8bae<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e8c\u6b65&#xff1a;\u521b\u5efa\u8fde\u63a5<\/span><br \/>\ntcp_client_socket<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u53c2\u6570\u662f\u4e2a\u5143\u7ec4<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e09\u6b65&#xff1a;\u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668\u7aef<\/span><br \/>\ntcp_client_socket<span class=\"token punctuation\">.<\/span>send<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;hello&#034;<\/span><span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8fd9\u91cc\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u6210\u4e8c\u8fdb\u5236\u6570\u636e<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u56db\u6b65&#xff1a;\u63a5\u6536\u670d\u52a1\u5668\u7aef\u8fd4\u56de\u7684\u6570\u636e<\/span><br \/>\nrecv_data <span class=\"token operator\">&#061;<\/span> tcp_client_socket<span class=\"token punctuation\">.<\/span>recv<span class=\"token punctuation\">(<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>decode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 1024\u8868\u793a\u672c\u6b21\u63a5\u6536\u7684\u6700\u5927\u5b57\u8282\u6570&#xff0c;decode\u89e3\u7801<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u63a5\u6536\u5230\u7684\u6570\u636e\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>recv_data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u6210\u5b57\u7b26\u4e32<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e94\u6b65&#xff1a;\u5173\u95ed\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\ntcp_client_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>tip&#xff1a;\u53d1\u9001\u548c\u63a5\u53d7\u7684\u90fd\u8981\u662f\u4e8c\u8fdb\u5236\u6570\u636e&#xff0c;\u6240\u4ee5\u8981\u7528encode\u548cdecode\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<ul>\n<li>encode()&#xff1a;\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u6570\u636e<\/li>\n<li>decode()&#xff1a;\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u6210\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<p>\u5173\u4e8esocket.AF_INET\u3001socket.SOCK_STREAM\u5e38\u91cf\u7684\u4ecb\u7ecd&#xff1a;<\/p>\n<ul>\n<li>socket.AF_INET&#xff08;IPv4&#xff09;\n<ul>\n<li>\u8fd9\u662f Python \u4e2dsocket\u6a21\u5757\u91cc\u7684\u4e00\u4e2a\u5e38\u91cf&#xff0c;AF_INET\u4ee3\u8868 Address Family&#xff08;\u5730\u5740\u65cf&#xff09;\u4e3aINET&#xff0c;\u7528\u4e8e\u6307\u5b9a\u7f51\u7edc\u901a\u4fe1\u4f7f\u7528\u7684\u5730\u5740\u65cf\u662f IPv4 \u5730\u5740\u65cf\u3002<\/li>\n<li>\u5f53\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u65f6&#xff0c;\u901a\u8fc7\u6307\u5b9aAF_INET&#xff0c;\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u4e2a\u5957\u63a5\u5b57\u5c06\u7528\u4e8e\u57fa\u4e8e IPv4 \u534f\u8bae\u7684\u7f51\u7edc\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>socket.SOCK_STREAM&#xff08;TCP&#xff09;\n<ul>\n<li>\u8fd9\u662fsocket\u6a21\u5757\u4e2d\u7684\u53e6\u4e00\u4e2a\u5e38\u91cf&#xff0c;\u7528\u4e8e\u6307\u5b9a\u5957\u63a5\u5b57\u7684\u7c7b\u578b\u4e3a\u6d41\u5957\u63a5\u5b57\u3002\n<ul>\n<li>\u5f53\u548cAF_INET\u4e00\u8d77\u4f7f\u7528\u521b\u5efa\u5957\u63a5\u5b57\u65f6&#xff08;\u5982\u524d\u9762\u4ee3\u7801\u793a\u4f8b\u4e2d\u7684socket.socket(socket.AF_INET, socket.SOCK_STREAM)&#xff09;&#xff0c;\u5b83\u8868\u793a\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u57fa\u4e8e TCP&#xff08;Transmission Control Protocol&#xff09;\u534f\u8bae\u7684\u6d41\u5957\u63a5\u5b57\u3002TCP \u662f\u4e00\u79cd\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684\u4f20\u8f93\u534f\u8bae&#xff0c;SOCK_STREAM\u7c7b\u578b\u7684\u5957\u63a5\u5b57\u5229\u7528 TCP \u534f\u8bae\u63d0\u4f9b\u7684\u7279\u6027&#xff0c;\u5982\u4e09\u6b21\u63e1\u624b\u5efa\u7acb\u8fde\u63a5\u3001\u6570\u636e\u7684\u53ef\u9760\u4f20\u8f93&#xff08;\u901a\u8fc7\u786e\u8ba4\u3001\u91cd\u4f20\u7b49\u673a\u5236&#xff09;\u3001\u6d41\u91cf\u63a7\u5236\u548c\u62e5\u585e\u63a7\u5236\u7b49\u3002<\/li>\n<li>\u8fd9\u79cd\u7c7b\u578b\u7684\u5957\u63a5\u5b57\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u8bc1\u6570\u636e\u51c6\u786e\u65e0\u8bef\u5730\u4f20\u8f93\u7684\u5e94\u7528\u573a\u666f&#xff0c;\u6bd4\u5982 HTTP&#xff08;\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae&#xff09;\u7528\u4e8e\u7f51\u9875\u6d4f\u89c8&#xff0c;SMTP&#xff08;\u7b80\u5355\u90ae\u4ef6\u4f20\u8f93\u534f\u8bae&#xff09;\u7528\u4e8e\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7b49\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b57\u8282\u6d41\u7684\u63a5\u53e3&#xff0c;\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u50cf\u8bfb\u5199\u6587\u4ef6\u4e00\u6837\u901a\u8fc7\u8fd9\u4e2a\u5957\u63a5\u5b57\u8fdb\u884c\u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536&#xff0c;\u800c\u4e0d\u5fc5\u62c5\u5fc3\u6570\u636e\u7684\u4e22\u5931\u6216\u635f\u574f&#xff0c;\u56e0\u4e3a TCP \u534f\u8bae\u5728\u5e95\u5c42\u4f1a\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>TCP\u670d\u52a1\u7aef\u7a0b\u5e8f\u5f00\u53d1&#xff08;\u91cd\u70b9&#xff09;<\/h2>\n<p>\u5f00\u53d1\u7684\u4e03\u6b65&#xff1a; 1.\u521b\u5efa\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61 2.\u7ed1\u5b9a\u7aef\u53e3\u53f7 3.\u8bbe\u7f6e\u76d1\u542c 4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff1a;\u7c7b\u4f3c\u4e8einput() \u2192 accept()\u963b\u585e 5.\u63a5\u6536\u6570\u636e 6.\u53d1\u9001\u6570\u636e 7.\u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>\u670d\u52a1\u5668\u5982\u4f55\u5224\u65ad\u662f\u54ea\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5&#xff1a; \u901a\u8fc7accept()\u65b9\u6cd5\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u5bf9\u8c61\u6765\u533a\u5206\u4e0d\u540c\u7684\u5ba2\u6237\u7aef<\/p>\n<p><span class=\"token keyword\">import<\/span> socket<\/p>\n<p><span class=\"token comment\"># 1.\u521b\u5efa\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\ntcp_server_socket <span class=\"token operator\">&#061;<\/span> socket<span class=\"token punctuation\">.<\/span>socket<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>AF_INET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SOCK_STREAM<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># socket.AF_INET\u8868\u793aIPV4&#xff0c;socket.SOCK_STREAM\u8868\u793aTCP\u534f\u8bae<\/span><\/p>\n<p><span class=\"token comment\"># 2.\u7ed1\u5b9a\u7aef\u53e3\u53f7<\/span><br \/>\ntcp_server_socket<span class=\"token punctuation\">.<\/span>bind<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5982\u679c\u662f\u672c\u673a&#xff0c;\u53ef\u4ee5\u4e0d\u5199ip\u5730\u5740<\/span><\/p>\n<p><span class=\"token comment\"># 3.\u8bbe\u7f6e\u76d1\u542c<\/span><br \/>\ntcp_server_socket<span class=\"token punctuation\">.<\/span>listen<span class=\"token punctuation\">(<\/span><span class=\"token number\">128<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 128\u8868\u793a\u6700\u5927\u8fde\u63a5\u6570<\/span><\/p>\n<p><span class=\"token comment\"># 4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42<\/span><br \/>\nnew_socket<span class=\"token punctuation\">,<\/span> ip_port <span class=\"token operator\">&#061;<\/span> tcp_server_socket<span class=\"token punctuation\">.<\/span>accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u963b\u585e\u72b6\u6001&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><br \/>\n<span class=\"token comment\"># tcp_server_socket\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5&#xff1a;\u7ed1\u5b9a\u7aef\u53e3\u3001\u8bbe\u7f6e\u76d1\u542c\u3001\u63a5\u6536\u8fde\u63a5<\/span><br \/>\n<span class=\"token comment\"># new_socket\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u65b0\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u5730\u5740\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>ip_port<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u65b0\u8fde\u63a5\u7684\u5ba2\u6237\u7aefsocket\u5bf9\u8c61\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>new_socket<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/span><br \/>\n<span class=\"token comment\"># 5.\u63a5\u6536\u6570\u636e<\/span><br \/>\nrecv_data <span class=\"token operator\">&#061;<\/span> new_socket<span class=\"token punctuation\">.<\/span>recv<span class=\"token punctuation\">(<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>decode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 1024\u8868\u793a\u672c\u6b21\u63a5\u6536\u7684\u6700\u5927\u5b57\u8282\u6570&#xff0c;decode\u89e3\u7801<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u63a5\u6536\u5230\u7684\u6570\u636e\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>recv_data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 6.\u53d1\u9001\u6570\u636e<\/span><br \/>\nnew_socket<span class=\"token punctuation\">.<\/span>send<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4fe1\u606f\u5df2\u6536\u5230&#034;<\/span><span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u6210\u4e8c\u8fdb\u5236\u6570\u636e<\/span><\/p>\n<p><span class=\"token comment\"># 7.\u5173\u95ed\u65b0\u5957\u63a5\u5b57\u5bf9\u8c61&#xff08;\u5173\u95ed\u540e\u4e0d\u80fd\u6536\u53d1\u6d88\u606f&#xff09;\u548c\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5bf9\u8c61&#xff08;\u4e0d\u80fd\u63a5\u6536\u65b0\u8fde\u63a5&#xff09;<\/span><br \/>\nnew_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\ntcp_server_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u4fe1\u606f\u540e&#xff0c;\u63a5\u6536\u5230\u7684data\u662f\u4e00\u4e2a\u5143\u7ec4&#xff0c;\u4e0b\u9762\u662f\u4e2a\u6817\u5b50&#xff0c;\u5143\u7ec4\u6709\u4e24\u4e2a\u5143\u7d20&#xff0c;\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5957\u63a5\u5b57\u5bf9\u8c61&#xff0c;\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u5ba2\u6237\u7aef\u7684\u5730\u5740&#xff08;\u4e5f\u662f\u5143\u7ec4&#xff09;<\/p>\n<p><span class=\"token punctuation\">(<\/span><br \/>\n<span class=\"token operator\">&lt;<\/span>socket.socket <span class=\"token assign-left variable\">fd<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">432<\/span>, <span class=\"token assign-left variable\">family<\/span><span class=\"token operator\">&#061;<\/span>AddressFamily.AF_INET, <span class=\"token assign-left variable\">ttype<\/span><span class=\"token operator\">&#061;<\/span>SocketKind.SOCK_STREAM, <span class=\"token assign-left variable\">proto<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span>, <span class=\"token assign-left variable\">laddr<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;127.0.0.1&#039;<\/span>, <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span>, <span class=\"token assign-left variable\">raddr<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;127.0.0.1&#039;<\/span>, <span class=\"token number\">60925<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span>,<br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;127.0.0.1&#039;<\/span>, <span class=\"token number\">60925<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/p>\n<ul>\n<li>\u660e\u786e\u81ea\u5df1\u5f00\u53d1\u7684\u5230\u5e95\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef\n<ul>\n<li>\u5ba2\u6237\u7aef&#xff1a;connect()\u3001send()\u3001recv()\u3001close()<\/li>\n<li>\u670d\u52a1\u7aef&#xff1a;socket()\u3001bind()\u3001listen()\u3001accept()\u3001recv()\u3001send()\u3001close()<\/li>\n<\/ul>\n<\/li>\n<li>\u4e24\u4e2a\u5bf9\u8c61\u8981\u5206\u6e05\u695a\n<ul>\n<li>tcp_server_socket&#xff1a;\u4e3b\u8981\u7528\u4e8e\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5\n<ul>\n<li>\u5185\u90e8\u53ea\u6709\u670d\u52a1\u5668\u672c\u8eab\u7684\u4fe1\u606f&#xff0c;\u53ef\u4ee5\u7ed1\u5b9a\u7aef\u53e3\u3001\u8bbe\u7f6e\u76d1\u542c\u3001\u63a5\u6536\u8fde\u63a5<\/li>\n<\/ul>\n<\/li>\n<li>new_socket&#xff1a;\u4e3b\u8981\u7528\u4e8e\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e\n<ul>\n<li>\u5185\u90e8\u65e2\u6709\u5ba2\u6237\u7aef\u53c8\u6709\u670d\u52a1\u5668\u7aef\u4fe1\u606f&#xff0c;\u53ef\u4ee5\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e<\/li>\n<li>\u53ea\u80fd\u901a\u8fc7\u8fd9\u4e2a\u65b0\u5957\u63a5\u5b57\u6765\u6536\u53d1\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u670d\u52a1\u5668\u7aef\u9762\u5411\u5bf9\u8c61\u7248\u672c<\/h2>\n<p>\u90fd\u662f\u4e03\u6b65&#xff0c;\u4e0d\u53d8<\/p>\n<p>\u9762\u5411\u5bf9\u8c61&#xff0c;\u5148\u5206\u6790\u6709\u54ea\u4e9b\u5bf9\u8c61&#xff0c;\u521b\u5efa\u7c7b&#xff0c;\u5c5e\u6027\u548c\u65b9\u6cd5<\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e00\u6b65&#xff1a;\u521b\u5efa\u7c7b<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">WebServer<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token comment\"># \u7b2c\u56db\u6b65&#xff1a;\u521b\u5efa\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u521d\u59cb\u5316\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token comment\"># 1.\u521b\u5efa\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\nself<span class=\"token punctuation\">.<\/span>tcp_server_socket <span class=\"token operator\">&#061;<\/span> socket<span class=\"token punctuation\">.<\/span>socket<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>AF_INET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SOCK_STREAM<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># AF_INET\u8868\u793aIPV4&#xff0c;SOCK_STREAM\u8868\u793aTCP\u534f\u8bae<\/span><\/p>\n<p><span class=\"token comment\"># 2.\u7ed1\u5b9aip\u548c\u7aef\u53e3\u53f7<\/span><br \/>\nself<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>bind<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5982\u679c\u662f\u672c\u673a&#xff0c;\u53ef\u4ee5\u4e0d\u5199ip\u5730\u5740<\/span><br \/>\n<span class=\"token comment\"># \u8fd9\u91cc\u76848000\u7aef\u53e3\u4e0d\u4f1a\u968f\u7740\u670d\u52a1\u5668\u5173\u95ed\u800c\u91ca\u653e&#xff0c;\u9700\u8981\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff0c;\u7aef\u53e3\u590d\u7528\u5728\u4e0b\u4e00\u7bc7\u7b14\u8bb0<\/span><\/p>\n<p><span class=\"token comment\"># 3.\u8bbe\u7f6e\u76d1\u542c<\/span><br \/>\nself<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>listen<span class=\"token punctuation\">(<\/span><span class=\"token number\">128<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 128\u8868\u793a\u6700\u5927\u8fde\u63a5\u6570<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e94\u6b65&#xff1a;\u5b9a\u4e49\u4e00\u4e2astart\u65b9\u6cd5&#xff0c;\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">start<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token comment\"># 4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42<\/span><br \/>\nnew_socket<span class=\"token punctuation\">,<\/span> ip_port <span class=\"token operator\">&#061;<\/span> self<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 5.\u63a5\u6536\u6570\u636e<\/span><br \/>\nrecv_data <span class=\"token operator\">&#061;<\/span> new_socket<span class=\"token punctuation\">.<\/span>recv<span class=\"token punctuation\">(<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>decode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u63a5\u6536\u5230\u7684\u6570\u636e\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>recv_data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 6.\u53d1\u9001\u6570\u636e<\/span><br \/>\nnew_socket<span class=\"token punctuation\">.<\/span>send<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4fe1\u606f\u5df2\u6536\u5230&#034;<\/span><span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 7.\u5173\u95ed\u5957\u63a5\u5b57&#xff08;\u53ea\u80fd\u63a5\u6536\u4e00\u6b21\u4fe1\u606f&#xff09;<\/span><br \/>\n<span class=\"token comment\"># \u4e0d\u80fd\u5173\u95edtcp_server_socket&#xff0c;\u5426\u5219\u65e0\u6cd5\u7ee7\u7eed\u63a5\u6536\u65b0\u8fde\u63a5<\/span><br \/>\nnew_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u76ee\u524d\u4e00\u6b21\u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u56e0\u4e3a\u662f\u5355\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># \u5982\u679c\u5e0c\u671b\u670d\u52a1\u5668\u53ef\u4ee5\u540c\u65f6\u548c\u591a\u4e2a\u5ba2\u6237\u7aef\u6536\u53d1\u6d88\u606f&#xff0c;\u9700\u8981\u591a\u8fdb\u7a0b&#xff08;\u591a\u4efb\u52a1\u7f16\u7a0b&#xff09;<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e8c\u6b65&#xff1a;\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/span><br \/>\nws <span class=\"token operator\">&#061;<\/span> WebServer<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e09\u6b65&#xff1a;\u8c03\u7528start\u65b9\u6cd5&#xff0c;\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><br \/>\nws<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h2>\u7aef\u53e3\u590d\u7528&#xff1a;<\/h2>\n<p>\u5728\u4e0a\u4e00\u6b21\u5173\u95ed\u670d\u52a1\u5668\u540e&#xff0c;\u7aef\u53e3\u4e0d\u4f1a\u7acb\u5373\u91ca\u653e&#xff0c;\u9700\u8981\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff0c;\u624d\u80fd\u7ee7\u7eed\u4f7f\u7528\u6b64\u7aef\u53e3<\/p>\n<p><span class=\"token keyword\">import<\/span> socket<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">WebServer<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># 3\u3001\u5b9a\u4e49\u4e00\u4e2a__init__\u65b9\u6cd5&#xff0c;\u521d\u59cb\u5316\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>tcp_server_socket <span class=\"token operator\">&#061;<\/span> socket<span class=\"token punctuation\">.<\/span>socket<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>AF_INET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SOCK_STREAM<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff08;\u5728\u4e0a\u4e00\u6b21\u5173\u95ed\u670d\u52a1\u5668\u540e&#xff0c;\u7aef\u53e3\u4e0d\u4f1a\u7acb\u5373\u91ca\u653e&#xff0c;\u9700\u8981\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff09;<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>setsockopt<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>SOL_SOCKET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SO_REUSEADDR<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u53c2\u65702&#xff1a;SOL_SOCKET\u8868\u793a\u5f53\u524d\u5957\u63a5\u5b57\u5bf9\u8c61&#xff0c;\u53c2\u65703&#xff1a;SO_REUSEADDR\u8868\u793a\u590d\u7528\u7684\u5730\u5740&#xff0c;\u53c2\u65704&#xff1a;True\u8868\u793a\u5f00\u542f\u7aef\u53e3\u590d\u7528&#xff08;\u9ed8\u8ba4\u662ffalse&#xff0c;\u8981\u7b49\u5f85\u5f88\u957f\u65f6\u95f4\u7aef\u53e3\u624d\u4f1a\u81ea\u52a8\u91ca\u653e&#xff09;<\/span><\/p>\n<p>        self<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>bind<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5982\u679c\u662f\u672c\u673a&#xff0c;\u53ef\u4ee5\u4e0d\u5199ip\u5730\u5740<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>listen<span class=\"token punctuation\">(<\/span><span class=\"token number\">128<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 128\u8868\u793a\u6700\u5927\u8fde\u63a5\u6570<\/span><\/p>\n<p>    <span class=\"token comment\"># 4\u3001\u5b9a\u4e49\u4e00\u4e2astart\u65b9\u6cd5&#xff0c;\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">start<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># \u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42<\/span><br \/>\n            new_socket<span class=\"token punctuation\">,<\/span> ip_port <span class=\"token operator\">&#061;<\/span> self<span class=\"token punctuation\">.<\/span>tcp_server_socket<span class=\"token punctuation\">.<\/span>accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>            <span class=\"token comment\"># \u8c03\u7528\u81ea\u8eab\u7684handle_request()\u65b9\u6cd5&#xff0c;\u7528\u4e8e\u63a5\u6536\u548c\u53d1\u9001\u6d88\u606f&#xff08;\u5c01\u88c5\u6027&#xff09;<\/span><br \/>\n            self<span class=\"token punctuation\">.<\/span>handle_request<span class=\"token punctuation\">(<\/span>new_socket<span class=\"token punctuation\">,<\/span> ip_port<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 5\u3001\u5b9a\u4e49\u4e00\u4e2ahandle_request\u65b9\u6cd5&#xff0c;\u7528\u4e8e\u63a5\u6536\u548c\u53d1\u9001\u6d88\u606f<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">handle_request<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> new_socket<span class=\"token punctuation\">,<\/span> ip_port<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u63a5\u6536\u67d0\u4e2a\u5ba2\u6237\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u6d88\u606f<\/span><br \/>\n        recv_data <span class=\"token operator\">&#061;<\/span> new_socket<span class=\"token punctuation\">.<\/span>recv<span class=\"token punctuation\">(<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>decode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9e\u9645\u5de5\u4f5c\u4e2d\u4e00\u6761\u6570\u636e\u5927\u5c0f\u57281~1.5k\u4e4b\u95f4<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u63a5\u6536\u5230\u7684\u6570\u636e\u4e3a&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>recv_data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u53d1\u9001\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef<\/span><br \/>\n        new_socket<span class=\"token punctuation\">.<\/span>send<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4fe1\u606f\u5df2\u6536\u5230&#034;<\/span><span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u5173\u95ed\u5957\u63a5\u5b57<\/span><br \/>\n        new_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u5b9a\u4e49\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u5165\u53e3<\/span><br \/>\n<span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># 1\u3001\u5b9e\u4f8b\u5316\u670d\u52a1\u5668\u5bf9\u8c61<\/span><br \/>\n    server <span class=\"token operator\">&#061;<\/span> WebServer<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 2\u3001\u542f\u52a8\u670d\u52a1\u5668<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h2>\u5f00\u53d1\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>1.\u5f53TCP\u5ba2\u6237\u7aef\u7a0b\u5e8f\u60f3\u8981\u548cTCP\u670d\u52a1\u7aef\u7a0b\u5e8f\u8fdb\u884c\u901a\u4fe1\u7684\u65f6\u5019\u5fc5\u987b\u8981\u5148\u5efa\u7acb\u8fde\u63a5 2.TCP\u5ba2\u6237\u7aef\u7a0b\u5e8f\u4e00\u822c\u4e0d\u9700\u8981\u7ed1\u5b9a\u7aef\u53e3\u53f7&#xff0c;\u56e0\u4e3a\u5ba2\u6237\u7aef\u662f\u4e3b\u52a8\u53d1\u8d77\u5efa\u7acb\u8fde\u63a5\u7684\u3002 3.TCP\u670d\u52a1\u7aef\u7a0b\u5e8f\u5fc5\u987b\u7ed1\u5b9a\u7aef\u53e3\u53f7&#xff0c;\u5426\u5219\u5ba2\u6237\u7aef\u627e\u4e0d\u5230\u8fd9\u4e2aTCP\u670d\u52a1\u7aef\u7a0b\u5e8f\u3002 4.listen\u540e\u7684\u5957\u63a5\u5b57\u662f\u88ab\u52a8\u5957\u63a5\u5b57&#xff0c;\u53ea\u8d1f\u8d23\u63a5\u6536\u65b0\u7684\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u4e0d\u80fd\u6536\u53d1\u6d88\u606f\u3002 5.\u5f53TCP\u5ba2\u6237\u7aef\u7a0b\u5e8f\u548cTCP\u670d\u52a1\u7aef\u7a0b\u5e8f\u8fde\u63a5\u6210\u529f\u540e&#xff0c;TCP\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57&#xff0c;\u6536\u53d1\u5ba2\u6237\u7aef\u6d88\u606f\u4f7f\u7528\u8be5\u5957\u63a5\u5b57\u3002 6.\u5173\u95edaccept\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u610f\u5473\u7740\u548c\u8fd9\u4e2a\u5ba2\u6237\u7aef\u5df2\u7ecf\u901a\u4fe1\u5b8c\u6bd5\u3002 7.\u5f53\u5ba2\u6237\u7aef\u7684\u5957\u63a5\u5b57\u8c03\u7528close\u540e&#xff0c;\u670d\u52a1\u5668\u7aef\u7684recv\u4f1a\u89e3\u963b\u585e&#xff0c;\u8fd4\u56de\u7684\u6570\u636e\u957f\u5ea6\u4e3aO&#xff0c;\u670d\u52a1\u7aef\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u6570\u636e\u7684\u957f\u5ea6\u6765\u5224\u65ad\u5ba2\u6237\u7aef\u662f\u5426 \u5df2\u7ecf\u4e0b\u7ebf&#xff0c;\u53cd\u4e4b\u670d\u52a1\u7aef\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u5ba2\u6237\u7aef\u7684recv\u4e5f\u4f1a\u89e3\u963b\u585e&#xff0c;\u8fd4\u56de\u7684\u6570\u636e\u957f\u5ea6\u4e5f\u4e3a0\u3002<\/p>\n<h2>UDP\u5ba2\u6237\u7aef<\/h2>\n<p><span class=\"token comment\"># \u5bfc\u5165socket\u6a21\u5757<\/span><br \/>\n<span class=\"token keyword\">import<\/span> socket<\/p>\n<p><span class=\"token comment\"># \u521b\u5efaUDP\u5957\u63a5\u5b57\u5bf9\u8c61<\/span><br \/>\nudp_socket <span class=\"token operator\">&#061;<\/span> socket<span class=\"token punctuation\">.<\/span>socket<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">.<\/span>AF_INET<span class=\"token punctuation\">,<\/span> socket<span class=\"token punctuation\">.<\/span>SOCK_DGRAM<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u8fde\u63a5\u670d\u52a1\u5668&#xff0c;\u53d1\u9001\u6570\u636e<\/span><br \/>\nudp_socket<span class=\"token punctuation\">.<\/span>sendto<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6d88\u606f&#034;<\/span><span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u5173\u95ed\u5957\u63a5\u5b57<\/span><br \/>\nudp_socket<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf12\u6b21\u3002CS\u6a21\u5f0f\uff1a\u5ba2\u6237\u7aef-\u670d\u52a1\u7aef\u6a21\u5f0fTCP\u5ba2\u6237\u7aef\u5f00\u53d1\u6d41\u7a0b\u4ecb\u7ecd\uff08\u4e94\u6b65\uff09\uff08C\u7aef\uff091.\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u548c\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5efa\u7acb\u8fde\u63a53.\u53d1\u9001\u6570\u636e4.\u63a5\u6536\u6570\u636e5.\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57TCP\u670d\u52a1\u7aef\u5f00\u53d1\u6d41\u7a0b\uff08\u4e03\u6b65\uff09\uff08S\u7aef\uff091.\u521b\u5efa\u670d\u52a1\u7aef\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u7ed1\u5b9a\u7aef\u53e3\u53f73.\u8bbe\u7f6e\u76d1\u542c4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c425.\u63a5\u6536\u6570\u636e6.\u53d1\u9001\u6570\u636e7.\u5173\u95ed\u5957\u63a5\u5b57\u3002_python \u521b\u5efatcp\u5ba2\u6237\u7aef<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,205,43,78],"topic":[],"class_list":["post-19810","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-tcp-ip","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \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\/19810.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf12\u6b21\u3002CS\u6a21\u5f0f\uff1a\u5ba2\u6237\u7aef-\u670d\u52a1\u7aef\u6a21\u5f0fTCP\u5ba2\u6237\u7aef\u5f00\u53d1\u6d41\u7a0b\u4ecb\u7ecd\uff08\u4e94\u6b65\uff09\uff08C\u7aef\uff091.\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u548c\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5efa\u7acb\u8fde\u63a53.\u53d1\u9001\u6570\u636e4.\u63a5\u6536\u6570\u636e5.\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57TCP\u670d\u52a1\u7aef\u5f00\u53d1\u6d41\u7a0b\uff08\u4e03\u6b65\uff09\uff08S\u7aef\uff091.\u521b\u5efa\u670d\u52a1\u7aef\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u7ed1\u5b9a\u7aef\u53e3\u53f73.\u8bbe\u7f6e\u76d1\u542c4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c425.\u63a5\u6536\u6570\u636e6.\u53d1\u9001\u6570\u636e7.\u5173\u95ed\u5957\u63a5\u5b57\u3002_python \u521b\u5efatcp\u5ba2\u6237\u7aef\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/19810.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T18:22:37+00:00\" \/>\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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/19810.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/19810.html\",\"name\":\"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T18:22:37+00:00\",\"dateModified\":\"2025-04-18T18:22:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/19810.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/19810.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/19810.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b\"}]},{\"@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":"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \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\/19810.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf12\u6b21\u3002CS\u6a21\u5f0f\uff1a\u5ba2\u6237\u7aef-\u670d\u52a1\u7aef\u6a21\u5f0fTCP\u5ba2\u6237\u7aef\u5f00\u53d1\u6d41\u7a0b\u4ecb\u7ecd\uff08\u4e94\u6b65\uff09\uff08C\u7aef\uff091.\u521b\u5efa\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u548c\u670d\u52a1\u7aef\u5957\u63a5\u5b57\u5efa\u7acb\u8fde\u63a53.\u53d1\u9001\u6570\u636e4.\u63a5\u6536\u6570\u636e5.\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57TCP\u670d\u52a1\u7aef\u5f00\u53d1\u6d41\u7a0b\uff08\u4e03\u6b65\uff09\uff08S\u7aef\uff091.\u521b\u5efa\u670d\u52a1\u7aef\u7aef\u5957\u63a5\u5b57\u5bf9\u8c612.\u7ed1\u5b9a\u7aef\u53e3\u53f73.\u8bbe\u7f6e\u76d1\u542c4.\u7b49\u5f85\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c425.\u63a5\u6536\u6570\u636e6.\u53d1\u9001\u6570\u636e7.\u5173\u95ed\u5957\u63a5\u5b57\u3002_python \u521b\u5efatcp\u5ba2\u6237\u7aef","og_url":"https:\/\/www.wsisp.com\/helps\/19810.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T18:22:37+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/19810.html","url":"https:\/\/www.wsisp.com\/helps\/19810.html","name":"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T18:22:37+00:00","dateModified":"2025-04-18T18:22:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/19810.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/19810.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/19810.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010python\u9ad8\u7ea7\u3011342-TCP\u670d\u52a1\u5668\u5f00\u53d1\u6d41\u7a0b"}]},{"@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\/19810","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=19810"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/19810\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=19810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=19810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=19810"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=19810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}