{"id":2824,"date":"2025-03-16T12:16:38","date_gmt":"2025-03-16T04:16:38","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2824"},"modified":"2025-03-16T12:16:38","modified_gmt":"2025-03-16T04:16:38","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b5-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%bd%91%e7%bb%9c%e6%95%b0%e6%8d%ae%e5%8c%85%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b5-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%bd%91%e7%bb%9c%e6%95%b0%e6%8d%ae%e5%8c%85%e5%88%86%e6%9e%90\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b5. \u81ea\u5b9a\u4e49\u670d\u52a1\u5668\u4e0e\u7f51\u7edc\u6570\u636e\u5305\u5206\u6790"},"content":{"rendered":"\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>socketserver<\/code><\/strong>\uff1a\u5feb\u901f\u521b\u5efa TCP\/UDP \u670d\u52a1\u5668<\/li>\n\n\n\n<li><strong><code>scapy<\/code><\/strong>\uff1a\u7f51\u7edc\u6570\u636e\u5305\u5206\u6790\u548c\u6293\u5305<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5.1 \u4f7f\u7528 <code>socketserver<\/code> \u521b\u5efa\u81ea\u5b9a\u4e49\u670d\u52a1\u5668<\/strong><\/h2>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86 <code>socketserver<\/code> \u6a21\u5757\uff0c\u5141\u8bb8\u6211\u4eec\u5feb\u901f\u521b\u5efa TCP \u6216 UDP \u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1.1 TCP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socketserver\n\nclass MyTCPHandler(socketserver.BaseRequestHandler):\n    def handle(self):\n        data = self.request.recv(1024).strip()\n        print(f\"\u6536\u5230\u6765\u81ea {self.client_address} \u7684\u6570\u636e: {data.decode()}\")\n        self.request.sendall(b\"Hello from TCP Server\")\n\nserver = socketserver.TCPServer((\"0.0.0.0\", 9999), MyTCPHandler)\nprint(\"TCP \u670d\u52a1\u5668\u8fd0\u884c\u5728\u7aef\u53e3 9999...\")\nserver.serve_forever()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1.2 TCP \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nclient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nclient.connect((\"127.0.0.1\", 9999))\n\nclient.sendall(b\"Hello Server\")\nresponse = client.recv(1024)\nprint(f\"\u6536\u5230\u670d\u52a1\u5668\u54cd\u5e94: {response.decode()}\")\n\nclient.close()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1.3 UDP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socketserver\n\nclass MyUDPHandler(socketserver.BaseRequestHandler):\n    def handle(self):\n        data, socket = self.request\n        print(f\"\u6536\u5230\u6765\u81ea {self.client_address} \u7684\u6570\u636e: {data.decode()}\")\n        socket.sendto(b\"Hello from UDP Server\", self.client_address)\n\nserver = socketserver.UDPServer((\"0.0.0.0\", 9999), MyUDPHandler)\nprint(\"UDP \u670d\u52a1\u5668\u8fd0\u884c\u5728\u7aef\u53e3 9999...\")\nserver.serve_forever()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1.4 UDP \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nclient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nclient.sendto(b\"Hello UDP Server\", (\"127.0.0.1\", 9999))\n\nresponse, addr = client.recvfrom(1024)\nprint(f\"\u6536\u5230\u670d\u52a1\u5668\u54cd\u5e94: {response.decode()}\")\n\nclient.close()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5.2 \u7f51\u7edc\u6570\u636e\u5305\u5206\u6790\uff08<code>scapy<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p><code>scapy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python \u7f51\u7edc\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u6293\u5305\u3001\u5206\u6790\u6570\u636e\u5305\u3001\u6784\u9020\u81ea\u5b9a\u4e49\u6570\u636e\u5305\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2.1 \u5b89\u88c5 <code>scapy<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install scapy<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2.2 \u6293\u53d6\u7f51\u7edc\u6570\u636e\u5305<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from scapy.all import sniff\n\ndef packet_callback(packet):\n    print(packet.summary())\n\nsniff(prn=packet_callback, count=10)  # \u76d1\u542c 10 \u4e2a\u6570\u636e\u5305<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2.3 \u53d1\u9001\u81ea\u5b9a\u4e49\u6570\u636e\u5305<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from scapy.all import IP, ICMP, send\n\npacket = IP(dst=\"8.8.8.8\") \/ ICMP()\nsend(packet)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2.4 \u6784\u9020 TCP SYN \u626b\u63cf<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from scapy.all import IP, TCP, send\n\npacket = IP(dst=\"192.168.1.1\") \/ TCP(dport=80, flags=\"S\")\nsend(packet)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u4ecb\u7ecd <code>asyncio<\/code> \u5f02\u6b65\u7f51\u7edc\u7f16\u7a0b\uff0c\u6253\u9020\u9ad8\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\uff1a 5.1 \u4f7f\u7528 socketserver \u521b\u5efa\u81ea\u5b9a\u4e49\u670d\u52a1\u5668 Python \u63d0\u4f9b\u4e86 so [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2825,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2824"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2824\/revisions"}],"predecessor-version":[{"id":2826,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2824\/revisions\/2826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2825"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}