{"id":1012325,"date":"2024-12-27T11:37:54","date_gmt":"2024-12-27T03:37:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012325.html"},"modified":"2024-12-27T11:37:58","modified_gmt":"2024-12-27T03:37:58","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e5%b0%81%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012325.html","title":{"rendered":"python\u5982\u4f55\u5199\u5c01\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090138\/6ad10219-6bb6-40f4-841e-7b738b3d6287.webp\" alt=\"python\u5982\u4f55\u5199\u5c01\u5305\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5199\u5c01\u5305\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528socket\u7f16\u7a0b\u3001struct\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521b\u5efasocket\u8fde\u63a5\u3001\u4f7f\u7528struct\u6a21\u5757\u6253\u5305\u6570\u636e\u3001\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002<\/strong>\u5728\u5176\u4e2d\uff0cstruct\u6a21\u5757\u7684\u4f7f\u7528\u5c24\u4e3a\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5c06Python\u7684\u6570\u636e\u7c7b\u578b\u6253\u5305\u6210\u5b57\u8282\u6d41\uff0c\u5e76\u5728\u63a5\u6536\u65f6\u5c06\u5b57\u8282\u6d41\u89e3\u5305\u4e3aPython\u7684\u6570\u636e\u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528SOCKET\u7f16\u7a0b\u5b9e\u73b0\u5c01\u5305<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efaSocket\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>Socket\u7f16\u7a0b\u662f\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u7840\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>socket<\/code>\u6a21\u5757\u6765\u521b\u5efaTCP\u6216UDP\u5957\u63a5\u5b57\u3002TCP\u5957\u63a5\u5b57\u63d0\u4f9b\u4e86\u53ef\u9760\u7684\u3001\u9762\u5411\u8fde\u63a5\u7684\u901a\u4fe1\uff0c\u800cUDP\u5957\u63a5\u5b57\u63d0\u4f9b\u4e86\u4e0d\u53ef\u9760\u7684\u3001\u65e0\u8fde\u63a5\u7684\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efaTCP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/strong><\/h2>\n<p>tcp_socket.connect((&#39;localhost&#39;, 8080))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTCP\u5957\u63a5\u5b57\uff0c\u5e76\u8fde\u63a5\u5230\u672c\u5730\u4e3b\u673a\u76848080\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><h4>2. \u53d1\u9001\u548c\u63a5\u6536\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86Socket\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>send<\/code>\u548c<code>recv<\/code>\u65b9\u6cd5\u6765\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d1\u9001\u6570\u636e<\/p>\n<p>tcp_socket.sendall(b&#39;Hello, server!&#39;)<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>data = tcp_socket.recv(1024)<\/p>\n<p>print(&#39;Received&#39;, repr(data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>sendall<\/code>\u65b9\u6cd5\u53d1\u9001\u6570\u636e\uff0c\u5b83\u4f1a\u786e\u4fdd\u6570\u636e\u88ab\u5b8c\u5168\u53d1\u9001\u3002\u63a5\u6536\u6570\u636e\u65f6\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u6700\u5927\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a1024\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528STRUCT\u6a21\u5757\u8fdb\u884c\u6570\u636e\u6253\u5305<\/h3>\n<\/p>\n<p><h4>1. \u6253\u5305\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06Python\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6253\u5305\u6210\u5b57\u8282\u6d41\uff0c\u8fd9\u5728\u7f51\u7edc\u4f20\u8f93\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u6253\u5305\u6574\u6570\u6570\u636e<\/strong><\/h2>\n<p>packed_data = struct.pack(&#39;i&#39;, 42)<\/p>\n<p>print(&#39;Packed Data:&#39;, packed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6574\u657042\u6253\u5305\u6210\u5b57\u8282\u6d41\u3002<code>&#39;i&#39;<\/code>\u8868\u793a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2. \u89e3\u5305\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u7684<code>unpack<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u6d41\u89e3\u5305\u4e3aPython\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u5305\u6570\u636e<\/p>\n<p>unpacked_data = struct.unpack(&#39;i&#39;, packed_data)<\/p>\n<p>print(&#39;Unpacked Data:&#39;, unpacked_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b57\u8282\u6d41\u89e3\u5305\u4e3a\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u5c01\u5305\u548c\u89e3\u5305\u64cd\u4f5c\uff0c\u6bd4\u5982<code>scapy<\/code>\u548c<code>pyshark<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1. Scapy\u5e93<\/h4>\n<\/p>\n<p><p><code>Scapy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f51\u7edc\u5c01\u5305\u5de5\u5177\u5305\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u548c\u89e3\u6790\u7f51\u7edc\u5c01\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scapy.all import *<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aIP\u5c01\u5305<\/strong><\/h2>\n<p>packet = IP(dst=&quot;www.google.com&quot;)\/ICMP()<\/p>\n<p>packet.show()<\/p>\n<h2><strong>\u53d1\u9001\u5c01\u5305<\/strong><\/h2>\n<p>send(packet)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aIP\u5c01\u5305\uff0c\u5e76\u5c06\u5176\u53d1\u9001\u5230\u76ee\u6807\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>2. PyShark\u5e93<\/h4>\n<\/p>\n<p><p><code>PyShark<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8eWireshark\u7684Python\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6355\u83b7\u548c\u5206\u6790\u7f51\u7edc\u6d41\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyshark<\/p>\n<h2><strong>\u6355\u83b7\u5c01\u5305<\/strong><\/h2>\n<p>capture = pyshark.LiveCapture(interface=&#39;eth0&#39;)<\/p>\n<p>capture.sniff(timeout=10)<\/p>\n<p>for packet in capture.sniff_continuously(packet_count=5):<\/p>\n<p>    print(packet)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>pyshark<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\u5e76\u5206\u6790\u5c01\u5305\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1. \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>Python\u5c01\u5305\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u4f7f\u7528\uff0c\u5982\u7f51\u7edc\u901a\u4fe1\u3001\u6570\u636e\u4f20\u8f93\u3001\u534f\u8bae\u5b9e\u73b0\u548c\u7f51\u7edc\u5b89\u5168\u7b49\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u6a21\u578b\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u7269\u8054\u7f51\u8bbe\u5907\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h4>2. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5c01\u5305\u548c\u89e3\u5305\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b57\u8282\u987a\u5e8f<\/strong>\uff1a\u5728\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u65f6\uff0c\u6ce8\u610f\u5b57\u8282\u987a\u5e8f\uff08\u5927\u7aef\u6216\u5c0f\u7aef\uff09\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u786e\u4fdd\u6253\u5305\u548c\u89e3\u5305\u65f6\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u683c\u5f0f\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u5728\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u786e\u4fdd\u6570\u636e\u7684\u52a0\u5bc6\u548c\u5b8c\u6574\u6027\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62\u6570\u636e\u7be1\u6539\u548c\u7a83\u542c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5b9e\u73b0\u5c01\u5305\u548c\u89e3\u5305\uff0c\u4e3a\u7f51\u7edc\u901a\u4fe1\u548c\u6570\u636e\u4f20\u8f93\u63d0\u4f9b\u53ef\u9760\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5c01\u5305\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5c01\u5305\u662f\u6307\u5c06\u591a\u4e2a\u6570\u636e\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4ee5\u4fbf\u4e8e\u4f20\u8f93\u6216\u5b58\u50a8\u3002\u5728Python\u4e2d\uff0c\u5c01\u5305\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u6216\u81ea\u5b9a\u4e49\u7c7b\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u3002\u5c01\u5305\u7684\u4e3b\u8981\u76ee\u7684\u662f\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u64cd\u4f5c\u548c\u4f7f\u7528\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u5c06\u7528\u6237\u4fe1\u606f\u5c01\u5305\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u59d3\u540d\u3001\u5e74\u9f84\u548c\u5730\u5740\u7b49\u5b57\u6bb5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6807\u51c6\u5e93\u6765\u5c01\u5305\u548c\u89e3\u5305\u6570\u636e\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5c01\u5305\u548c\u89e3\u5305\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u6765\u5e8f\u5217\u5316\u5bf9\u8c61\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u4ece\u800c\u65b9\u4fbf\u5730\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u3002\u4f7f\u7528<code>pickle<\/code>\u65f6\uff0c\u8c03\u7528<code>pickle.dump()<\/code>\u5c06\u6570\u636e\u5c01\u5305\uff0c\u4f7f\u7528<code>pickle.load()<\/code>\u89e3\u5305\u6570\u636e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>pickle<\/code>\u65f6\u8981\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u907f\u514d\u6267\u884c\u4e0d\u53ef\u4fe1\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c01\u5305\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff1f<\/strong><br \/>\u8981\u5c01\u5305\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u5e76\u5b9e\u73b0<code>__dict__<\/code>\u65b9\u6cd5\u6216\u4f7f\u7528<code>dataclasses<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u5c06\u5bf9\u8c61\u7684\u5c5e\u6027\u8f6c\u6362\u4e3a\u5b57\u5178\u683c\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5c01\u5305\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u7c7b\uff0c\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u5c5e\u6027\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4f7f\u7528<code>dataclasses<\/code>\u6a21\u5757\uff0c\u7b80\u5355\u7684\u5b9a\u4e49\u7c7b\u5e76\u4f7f\u7528<code>asdict()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\uff0c\u4fbf\u4e8e\u6570\u636e\u7684\u5b58\u50a8\u548c\u4f20\u8f93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5199\u5c01\u5305\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528socket\u7f16\u7a0b\u3001struct\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521b\u5efasock [&hellip;]","protected":false},"author":3,"featured_media":1012332,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012325"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1012325"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012325\/revisions"}],"predecessor-version":[{"id":1012333,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012325\/revisions\/1012333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012332"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}