{"id":3806,"date":"2021-05-07T21:03:36","date_gmt":"2021-05-07T13:03:36","guid":{"rendered":"http:\/\/blog.ywdevops.cn\/?p=3806"},"modified":"2021-05-07T21:03:37","modified_gmt":"2021-05-07T13:03:37","slug":"python-16","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2021\/05\/07\/python-16\/","title":{"rendered":"python\u4e4b\u7f51\u7edc\u7f16\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5b9e\u73b0\u5728\u4e0d\u540c\u7684\u7535\u8111\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>TCP\/IP\u7b80\u4ecb\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e92\u8054\u7f51\u534f\u8bae\u5305\u542b\u4e86\u4e0a\u767e\u79cd\u534f\u8bae\u6807\u51c6\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u534f\u8bae\u5c31\u662fTCP\u548cIP\u534f\u8bae<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001IP\u534f\u8bae\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IP\u534f\u8bae\u8d1f\u8d23\u628a\u6570\u636e\u4ece\u4e00\u53f0\u8ba1\u7b97\u673a\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u5230\u53e6\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u7531\u4e8e\u4e92\u8054\u7f51\u94fe\u8def\u590d\u6742\uff0c\u4e24\u53f0\u8ba1\u7b97\u673a\u4e4b\u95f4\u7ecf\u5e38\u6709\u591a\u6761\u7ebf\u8def\uff0c\u56e0\u6b64\uff0c\u8def\u7531\u5668\u5c31\u8d1f\u8d23\u51b3\u5b9a\u5982\u4f55\u628a\u4e00\u4e2aIP\u5305\u8f6c\u53d1\u51fa\u53bb\uff0cIP\u5305\u7684\u7279\u70b9\u662f\u6309\u5757\u53d1\u9001\uff0c\u9014\u5f84\u591a\u4e2a\u8def\u7531\uff0c\u4f46\u662f\u4e0d\u4fdd\u8bc1\u90fd\u80fd\u5230\u8fbe\uff0c\u4e5f\u4e0d\u80fd\u4fdd\u8bc1\u987a\u5229\u5230\u8fbe<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001TCP\u534f\u8bae\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TCP\u534f\u8bae\u662f\u5efa\u7acb\u5728IP\u534f\u8bae\u4e4b\u4e0a\u7684\uff0cTCP\u534f\u8bae\u8d1f\u8d23\u5728\u4e24\u53f0\u8ba1\u7b97\u673a\u4e4b\u95f4\u5efa\u7acb\u8fde\u63a5\uff0c\u4fdd\u8bc1\u6570\u636e\u5305\u6309\u987a\u5e8f\u5230\u8fbe\uff0c\u901a\u8fc73\u6b21\u63e1\u624b\u5efa\u7acb\u53ef\u9760\u6027\u8fde\u63a5\uff0c\u7136\u540e\u9700\u8981\u5bf9\u6bcf\u4e2aIP\u5305\u8fdb\u884c\u7f16\u53f7\uff0c\u786e\u4fdd\u5bf9\u65b9\u6309\u987a\u5e8f\u6536\u5230\uff0c\u5982\u679c\u5305\u4e22\u6389\u4e86\u5c31\u81ea\u52a8\u91cd\u53d1\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>UDP\u7b80\u4ecb\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f8\u5bf9\u4e8eTCP\u534f\u8bae\uff0cUDP\u534f\u8bae\u5219\u662f\u9762\u5411\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u4f7f\u7528UDP\u534f\u8bae\u65f6\uff0c\u4e0d\u9700\u8981\u5efa\u7acb\u8fde\u63a5\uff0c\u53ea\u9700\u8981\u77e5\u9053\u5bf9\u65b9\u7684IP\u548c\u7aef\u53e3\u53f7\uff0c\u5c31\u53ef\u4ee5\u53d1\u9001\u6570\u636e\u5305\uff0c\u4f46\u662f\u6570\u636e\u65e0\u6cd5\u4fdd\u8bc1\u4e00\u5b9a\u5230\u8fbe\uff0c\u867d\u7136\u4f7f\u7528UDP\u4f20\u8f93\u6570\u636e\u4e0d\u53ef\u9760\uff0c\u4f46\u5b83\u7684\u4f18\u70b9\u662f\u4f20\u8f93\u901f\u5ea6\u5feb\uff0c\u5982\u679c\u4e0d\u8981\u6c42\u53ef\u9760\u6027\u5230\u8fbe\u7684\u6570\u636e\u800c\u8a00\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528UDP\u534f\u8bae<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Socket\u7b80\u4ecb\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u8ba9\u4e24\u4e2a\u7a0b\u5e8f\u4e4b\u95f4\u901a\u8fc7\u7f51\u8def\u8fdb\u884c\u901a\u4fe1\uff0c\u4e8c\u8005\u5747\u5fc5\u987b\u4f7f\u7528Socket\u5957\u63a5\u5b57\uff0c\u901a\u5e38\u4e5f\u79f0\u4e3a\u5957\u63a5\u5b57\uff0c\u7528\u4e8e\u63cf\u8ff0IP\u5730\u5740\u548c\u7aef\u53e3\uff0c\u5b83\u662f\u4e00\u4e2a\u901a\u4fe1\u94fe\u7684\u53e5\u67c4\uff0c\u7528\u4e8e\u5b9e\u73b0\u4e0d\u540c\u865a\u62df\u673a\u6216\u4e0d\u540c\u8ba1\u7b97\u673a\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Python\u4e2d\uff0c\u4f7f\u7528socket\u6a21\u5757\u7684socket()\u51fd\u6570\u5c31\u53ef\u4ee5\u5b8c\u6210\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">s  = socket.socket(AddressFamily,Type)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Address Family\uff1a\u53ef\u4ee5\u9009\u62e9AF_INET(\u7528\u4e8eInternet\u8fdb\u7a0b\u95f4\u901a\u4fe1)\uff0c\u6216\u8005AF_UNIX(\u7528\u4e8e\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u8fdb\u7a0b\u95f4\u901a\u4fe1)\uff0c\u5b9e\u9645\u5de5\u4f5c\u4e2d\u5e38\u7528AF_INET<\/li><li>Type\uff1a\u5957\u63a5\u5b57\u7c7b\u578b\uff0c\u53ef\u4ee5\u662fSOCK_STREAM(\u6d41\u5f0f\u5957\u63a5\u5b57\uff0c\u4e3b\u8981\u7528\u4e8eTCP\u534f\u8bae)\u6216\u8005SOCK_DGRAM(\u6570\u636e\u5305\u5957\u63a5\u5b57\uff0c\u4e3b\u8981\u7528\u4e8eUDP\u534f\u8bae)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u901a\u8fc7\u4e0b\u9762\u65b9\u5f0f\u521b\u5efaTCP\u6216\u8005UDP\u5957\u63a5\u5b57\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"206\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/32.png\" alt=\"\" class=\"wp-image-3807\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/32.png 622w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/32-300x99.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/32-600x199.png 600w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-luminous-vivid-amber-background-color wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u9762\u7684socket.socket()\u4e2d\u4e0d\u52a0\u4efb\u4f55\u53c2\u6570\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5c31\u662f\u521b\u5efaTCP\u5957\u63a5\u5b57<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u751f\u6210\u4e00\u4e2asocket\u5bf9\u8c61\uff0csocket\u5bf9\u8c61\u4e3b\u8981\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u51fd\u6570<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u63cf\u8ff0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.bind()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u7ed1\u5b9a\u5730\u5740(host,port)\u5230\u5957\u63a5\u5b57\uff0c\u5728AF_INET\u4e0b\uff0c\u4ee5\u5143\u7ec4(host,port)\u5f62\u5f0f\u8868\u793a\u5730\u5740<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.listen()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5f00\u542fTCP\u76d1\u542c,\u53c2\u6570\u4e3a\u5728\u62d2\u7edd\u8fde\u63a5\u4e4b\u524d\uff0c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u6302\u8d77\u7684\u6700\u5927\u8fde\u63a5\u6570\u91cf\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3a5<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.accept()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u88ab\u52a8\u63a5\u53d7TCP\u5ba2\u6237\u7aef\u8fde\u63a5(\u963b\u585e\u5f0f)\uff0c\u7b49\u5f85\u8fde\u63a5\u5230\u6765<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.connect()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u4e3b\u52a8\u521d\u59cb\u5316TCP\u8fde\u63a5\uff0c\u53c2\u6570\u7684\u683c\u5f0f\u4e00\u822c\u4e3a\u5143\u7ec4(hostname,port)\uff0c\u5982\u679c\u8fde\u63a5\u51fa\u9519\uff0c\u8fd4\u56desocket.error\u9519\u8bef<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.recv()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u63a5\u53d7TCP\u6570\u636e\uff0c\u6570\u636e\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\uff0c\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u6700\u5927\u6570\u636e\u91cf<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.send()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u53d1\u9001TCP\u6570\u636e\uff0c\u5c06string\u4e2d\u7684\u6570\u636e\u53d1\u9001\u5230\u8fde\u63a5\u7684\u5957\u63a5\u5b57\uff0c\u8fd4\u56de\u503c\u662f\u8981\u53d1\u9001\u7684\u5b57\u8282\u6570\u91cf<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.sendall()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5b8c\u6574\u53d1\u9001TCP\u6570\u636e\uff0c\u5c06string\u4e2d\u7684\u6570\u636e\u53d1\u9001\u5230\u8fde\u63a5\u7684\u5957\u63a5\u5b57\uff0c\u5728\u8fd4\u56de\u4e4b\u524d\u4f1a\u5c1d\u8bd5\u53d1\u9001\u6240\u6709\u6570\u636e\uff0c\u6210\u529f\u8fd4\u56deNone\uff0c\u5931\u8d25\u629b\u51fa\u5f02\u5e38<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.recvfrom()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u63a5\u53d7UDP\u6570\u636e\uff0c\u4e0erecv()\u7c7b\u4f3c\uff0c\u8fd4\u56de\u503c\u662f(data,address)\uff0c\u5176\u4e2ddata\u662f\u5305\u542b\u63a5\u53d7\u6570\u636e\u7684\u5b57\u7b26\u4e32\uff0caddress\u662f\u53d1\u9001\u6570\u636e\u7684\u5957\u63a5\u5b57\u5730\u5740<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.sendto()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u53d1\u9001UDP\u6570\u636e\uff0c\u5c06\u6570\u636e\u53d1\u9001\u5230\u5957\u63a5\u5b57\uff0caddress\u5f62\u5f0f\u4e3a(ipaddr,port)\u7684\u5143\u7ec4\uff0c\u6307\u5b9a\u8fdc\u7a0b\u5730\u5740\uff0c\u8fd4\u56de\u503c\u662f\u53d1\u9001\u7684\u5b57\u8282\u6570<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">s.close()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5173\u95ed\u5957\u63a5\u5b57<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\">TCP\u7f16\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u521b\u5efaTCP\u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8981\u5b8c\u6210\u4e00\u4e2aTCP\u670d\u52a1\u5668\u7684\u529f\u80fd\uff0c\u9700\u8981\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528socket()\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57<\/li><li>\u4f7f\u7528bind()\u7ed1\u5b9aIP\u548cport<\/li><li>\u4f7f\u7528listen()\u4f7f\u5957\u63a5\u5b57\u53d8\u4e3a\u53ef\u88ab\u52a8\u8fde\u63a5<\/li><li>\u4f7f\u7528accept()\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/li><li>\u4f7f\u7528recv\/send()\u63a5\u53d7\u548c\u53d1\u9001\u6570\u636e<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u4f7f\u7528socket\u6a21\u5757\uff0c\u901a\u8fc7\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u5411\u672c\u5730\u670d\u52a1\u5668(127.0.0.1)\u53d1\u8d77\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u63a5\u6536\u5230\u8bf7\u6c42\uff0c\u5411\u6d4f\u89c8\u5668\u53d1\u9001helloworld\uff0c\u521b\u5efa\u4e00\u4e2aserver.py\u6587\u4ef6\uff0c\u5982\u56fe \uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"401\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/33.png\" alt=\"\" class=\"wp-image-3809\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/33.png 751w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/33-300x160.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/33-600x320.png 600w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"181\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/34.png\" alt=\"\" class=\"wp-image-3810\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/34.png 551w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/34-300x99.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u521b\u5efaTCP\u5ba2\u6237\u7aef\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u662f\u7528\u6d4f\u89c8\u5668\u4f5c\u4e3a\u5ba2\u6237\u7aef\u6765\u63a5\u6536\u6570\u636e\uff0c\u4e0b\u9762\uff0c\u521b\u5efa\u4e00\u4e2aTCP\u5ba2\u6237\u7aef\uff0c\u901a\u8fc7\u8be5\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u548c\u63a5\u53d7\u6d88\u606f\uff0c\u521b\u5efa\u4e00\u4e2aclient.py\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"305\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/35.png\" alt=\"\" class=\"wp-image-3811\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/35.png 605w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/35-300x151.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/35-600x302.png 600w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u540c\u65f6\u542f\u52a8\u7b2c\u4e00\u6b65\u7684\u670d\u52a1\u7aefserver.py\u548c\u4e0a\u56fe\u4e2d\u7684\u5ba2\u6237\u7aefclient.py\uff0c\u7136\u540e\u5728\u5ba2\u6237\u7aef\u4e2d\u8f93\u5165\u6570\u636e\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u8fd4\u56de\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"189\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/36.png\" alt=\"\" class=\"wp-image-3812\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/36.png 552w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/05\/36-300x103.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u6267\u884cTCP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4f8b\u5b50\u5b9e\u73b0\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u7b80\u6613\u804a\u5929\uff0c\u5f53\u6709\u4e00\u65b9\u8f93\u5165byebye\u540e\u5c31\u9000\u51fa\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u670d\u52a1\u7aef\u7684\u4ee3\u7801\nimport socket\nhost = socket.gethostname()  #\u83b7\u53d6\u4e3b\u673a\u5730\u5740\nport = 12345\ns = socket.socket()          #\u521b\u5efaTCP\u5957\u63a5\u5b57\ns.bind((host,port))          #\u7ed1\u5b9a\u4e3b\u673a\u5730\u5740\u548c\u7aef\u53e3\ns.listen(1)                  #\u8bbe\u7f6e\u6700\u591a\u8fde\u63a5\u6570\u91cf\nsock,addr = s.accept()       #\u88ab\u52a8\u63a5\u53d7TCP\u5ba2\u6237\u7aef\u8fde\u63a5\ninfo = sock.recv(1024).decode() #decode()\u5c06byte\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\nwhile info != 'byebye':\n    if info:\n        print(\"\u63a5\u53d7\u5230\u7684\u5185\u5bb9\u4e3a:\",info)\n    send_data = input(\"\u8f93\u5165\u53d1\u9001\u5185\u5bb9\uff1a\")\n    sock.send(send_data.encode()) #\u53d1\u9001TCPshuju\n    if send_data == 'byebye':     #\u5982\u679c\u53d1\u9001byebye\u5c31\u63a8\u51fa\n        break\n    info = sock.recv(1024).decode() #\u63a5\u53d7\u5ba2\u6237\u7aef\u6570\u636e\nsock.close()                        #\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\ns.close()                           #\u5173\u95ed\u670d\u52a1\u5668\u5957\u63a5\u5b57<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5ba2\u6237\u7aef\u4ee3\u7801\nimport socket\ns = socket.socket()             #\u521b\u5efaTCP\u5957\u63a5\u5b57\nhost = socket.gethostname()\nport = 12345\ns.connect((host,port))\ninfo = ''\nwhile info != 'byebye':\n    send_data = input(\"\u8bf7\u8f93\u5165\u53d1\u9001\u5185\u5bb9:\")\n    s.send(send_data.encode())\n    if send_data == 'byebye':\n        break\n    info = s.recv(1024).decode()\n    print(\"\u63a5\u53d7\u5230\u7684\u5185\u5bb9\u662f:\",info)\ns.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u5b9e\u73b0\u5728\u4e0d\u540c\u7684\u7535\u8111\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u7f16\u7a0b\u3002 TCP\/IP\u7b80\u4ecb\uff1a \u4e92\u8054\u7f51\u534f\u8bae\u5305\u542b\u4e86\u4e0a\u767e\u79cd\u534f\u8bae\u6807\u51c6\uff0c\u5176\u4e2d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,29],"tags":[],"class_list":["post-3806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-yunwei"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/3806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3806"}],"version-history":[{"count":2,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/3806\/revisions"}],"predecessor-version":[{"id":3813,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/3806\/revisions\/3813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/3814"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}