{"id":1065250,"date":"2024-12-31T16:15:57","date_gmt":"2024-12-31T08:15:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065250.html"},"modified":"2024-12-31T16:15:59","modified_gmt":"2024-12-31T08:15:59","slug":"python%e7%9a%84socket%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e6%ba%90%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065250.html","title":{"rendered":"python\u7684socket\u5982\u4f55\u4fee\u6539\u6e90\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2dfb5610-f810-4d00-b228-363eefe82e41.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u7684socket\u5982\u4f55\u4fee\u6539\u6e90\u5730\u5740\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fee\u6539socket\u7684\u6e90\u5730\u5740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528bind\u65b9\u6cd5\u7ed1\u5b9a\u7279\u5b9a\u7684IP\u5730\u5740\u3001\u4f7f\u7528SO_BINDTODEVICE\u5c06socket\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3\u3001\u4f7f\u7528IP_HDRINCL\u8bbe\u7f6e\u81ea\u5b9a\u4e49IP\u5934<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528bind\u65b9\u6cd5\u7ed1\u5b9a\u7279\u5b9a\u7684IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    # \u7ed1\u5b9a\u6e90IP\u5730\u5740<\/p>\n<p>    sock.bind((source_ip, 0))<\/p>\n<p>    # \u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>    sock.connect((destination_ip, destination_port))<\/p>\n<p>    return sock<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>source_ip = &#39;192.168.1.100&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>destination_port = 80<\/p>\n<p>sock = create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port)<\/p>\n<p>sock.send(b&#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;)<\/p>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode())<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTCP socket\uff0c\u5e76\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u5c06\u6e90IP\u5730\u5740\u7ed1\u5b9a\u4e3a<code>192.168.1.100<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740<code>192.168.1.200<\/code>\u768480\u7aef\u53e3\uff0c\u5e76\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\u3002\u6700\u540e\uff0c\u6211\u4eec\u63a5\u6536\u5e76\u6253\u5370\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528bind\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u7ed1\u5b9a\u6e90IP\u5730\u5740<\/h3>\n<\/p>\n<p><p>\u5728Python\u7684<code>socket<\/code>\u6a21\u5757\u4e2d\uff0c<code>bind<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2asocket\u7ed1\u5b9a\u5230\u4e00\u4e2a\u7279\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u901a\u8fc7\u5c06\u7aef\u53e3\u8bbe\u7f6e\u4e3a0\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5206\u914d\u4e00\u4e2a\u53ef\u7528\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    # \u7ed1\u5b9a\u6e90IP\u5730\u5740<\/p>\n<p>    sock.bind((source_ip, 0))<\/p>\n<p>    # \u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>    sock.connect((destination_ip, destination_port))<\/p>\n<p>    return sock<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>source_ip = &#39;192.168.1.100&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>destination_port = 80<\/p>\n<p>sock = create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port)<\/p>\n<p>sock.send(b&#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;)<\/p>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode())<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTCP socket\uff0c\u5e76\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u5c06\u6e90IP\u5730\u5740\u7ed1\u5b9a\u4e3a<code>192.168.1.100<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740<code>192.168.1.200<\/code>\u768480\u7aef\u53e3\uff0c\u5e76\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\u3002\u6700\u540e\uff0c\u6211\u4eec\u63a5\u6536\u5e76\u6253\u5370\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>2. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6e90IP\u5730\u5740\u5728\u672c\u5730\u7f51\u7edc\u63a5\u53e3\u4e2d\u662f\u6709\u6548\u7684\u3002\u5982\u679c\u6e90IP\u5730\u5740\u65e0\u6548\uff0c<code>bind<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>OSError<\/code>\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u624d\u80fd\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684IP\u5730\u5740\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528SO_BINDTODEVICE<\/h2>\n<\/p>\n<p><h3>1. \u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06socket\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3\u3002\u53ef\u4ee5\u4f7f\u7528<code>SO_BINDTODEVICE<\/code>\u5957\u63a5\u5b57\u9009\u9879\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u8be5\u9009\u9879\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u4e00\u4e2a\u7f51\u7edc\u63a5\u53e3\u540d\u79f0\uff0c\u5982<code>eth0<\/code>\u6216<code>wlan0<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>import fcntl<\/p>\n<p>import struct<\/p>\n<p>def create_socket_bound_to_interface(interface_name, destination_ip, destination_port):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    # \u5c06socket\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3<\/p>\n<p>    ifname = interface_name.encode(&#39;utf-8&#39;)<\/p>\n<p>    fcntl.ioctl(sock.fileno(), 0x8933, struct.pack(&#39;256s&#39;, ifname[:15]))<\/p>\n<p>    # \u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>    sock.connect((destination_ip, destination_port))<\/p>\n<p>    return sock<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>interface_name = &#39;eth0&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>destination_port = 80<\/p>\n<p>sock = create_socket_bound_to_interface(interface_name, destination_ip, destination_port)<\/p>\n<p>sock.send(b&#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;)<\/p>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode())<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTCP socket\uff0c\u5e76\u4f7f\u7528<code>SO_BINDTODEVICE<\/code>\u5c06socket\u7ed1\u5b9a\u5230\u7f51\u7edc\u63a5\u53e3<code>eth0<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740<code>192.168.1.200<\/code>\u768480\u7aef\u53e3\uff0c\u5e76\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\u3002\u6700\u540e\uff0c\u6211\u4eec\u63a5\u6536\u5e76\u6253\u5370\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>2. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>SO_BINDTODEVICE<\/code>\u9009\u9879\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6307\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3\u5728\u7cfb\u7edf\u4e2d\u662f\u6709\u6548\u7684\u3002\u5982\u679c\u7f51\u7edc\u63a5\u53e3\u65e0\u6548\u6216\u5f53\u524d\u7528\u6237\u6ca1\u6709\u8db3\u591f\u7684\u6743\u9650\uff0c<code>fcntl.ioctl<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>OSError<\/code>\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u4e0d\u652f\u6301\u8be5\u9009\u9879\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528IP_HDRINCL\u8bbe\u7f6e\u81ea\u5b9a\u4e49IP\u5934<\/h2>\n<\/p>\n<p><h3>1. \u8bbe\u7f6e\u81ea\u5b9a\u4e49IP\u5934<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49IP\u5934\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>IP_HDRINCL<\/code>\u9009\u9879\u6765\u5b9e\u73b0\u3002\u8be5\u9009\u9879\u5141\u8bb8\u6211\u4eec\u5728\u53d1\u9001\u6570\u636e\u5305\u65f6\u5305\u542b\u81ea\u5b9a\u4e49\u7684IP\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>import struct<\/p>\n<p>def checksum(data):<\/p>\n<p>    s = 0<\/p>\n<p>    for i in range(0, len(data), 2):<\/p>\n<p>        w = (data[i] &lt;&lt; 8) + (data[i+1] if i+1 &lt; len(data) else 0)<\/p>\n<p>        s = (s + w) &amp; 0xffff<\/p>\n<p>    return ~s &amp; 0xffff<\/p>\n<p>def create_raw_socket_with_custom_ip_header(source_ip, destination_ip):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u539f\u59cbsocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)<\/p>\n<p>    # \u8bbe\u7f6eIP_HDRINCL\u9009\u9879<\/p>\n<p>    sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)<\/p>\n<p>    # \u521b\u5efaIP\u5934<\/p>\n<p>    ip_header = struct.pack(<\/p>\n<p>        &#39;!BBHHHBBH4s4s&#39;,<\/p>\n<p>        0x45, 0, 0, 0, 0, 255, socket.IPPROTO_TCP, 0,<\/p>\n<p>        socket.inet_aton(source_ip),<\/p>\n<p>        socket.inet_aton(destination_ip)<\/p>\n<p>    )<\/p>\n<p>    # \u8ba1\u7b97IP\u5934\u6821\u9a8c\u548c<\/p>\n<p>    ip_header = ip_header[:10] + struct.pack(&#39;H&#39;, checksum(ip_header)) + ip_header[12:]<\/p>\n<p>    return sock, ip_header<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>source_ip = &#39;192.168.1.100&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>sock, ip_header = create_raw_socket_with_custom_ip_header(source_ip, destination_ip)<\/p>\n<p>sock.sendto(ip_header + b&#39;Hello, World!&#39;, (destination_ip, 0))<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u539f\u59cbsocket\uff0c\u5e76\u8bbe\u7f6e<code>IP_HDRINCL<\/code>\u9009\u9879\u3002\u63a5\u7740\uff0c\u6211\u4eec\u6784\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684IP\u5934\uff0c\u5e76\u8ba1\u7b97\u4e86\u6821\u9a8c\u548c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06IP\u5934\u548c\u6570\u636e\u4e00\u8d77\u53d1\u9001\u5230\u76ee\u6807IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>2. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u539f\u59cbsocket\u548c\u81ea\u5b9a\u4e49IP\u5934\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6570\u636e\u5305\u7684\u683c\u5f0f\u548c\u6821\u9a8c\u548c\u662f\u6b63\u786e\u7684\u3002\u5426\u5219\uff0c\u6570\u636e\u5305\u53ef\u80fd\u4f1a\u88ab\u4e22\u5f03\u6216\u89e3\u6790\u9519\u8bef\u3002\u6b64\u5916\uff0c\u521b\u5efa\u539f\u59cbsocket\u901a\u5e38\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4fee\u6539socket\u7684\u6e90\u5730\u5740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528bind\u65b9\u6cd5\u7ed1\u5b9a\u7279\u5b9a\u7684IP\u5730\u5740<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528SO_BINDTODEVICE\u5c06socket\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7ed1\u5b9a\u5230\u7279\u5b9a\u7f51\u7edc\u63a5\u53e3\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528IP_HDRINCL\u8bbe\u7f6e\u81ea\u5b9a\u4e49IP\u5934<\/strong>\uff1a\u9002\u7528\u4e8e\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\uff0c\u9700\u8981\u81ea\u5b9a\u4e49IP\u5934\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5e94\u7528\u573a\u666f\u3001\u7cfb\u7edf\u6743\u9650\u548c\u7f51\u7edc\u73af\u5883\u7b49\u56e0\u7d20\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9socket\u6e90\u5730\u5740\u7684\u7cbe\u786e\u63a7\u5236\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><h3>\u793a\u4f8b1\uff1a\u7ed1\u5b9a\u6e90IP\u5730\u5740<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    # \u7ed1\u5b9a\u6e90IP\u5730\u5740<\/p>\n<p>    sock.bind((source_ip, 0))<\/p>\n<p>    # \u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>    sock.connect((destination_ip, destination_port))<\/p>\n<p>    return sock<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>source_ip = &#39;192.168.1.100&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>destination_port = 80<\/p>\n<p>sock = create_socket_with_specific_source_ip(source_ip, destination_ip, destination_port)<\/p>\n<p>sock.send(b&#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;)<\/p>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode())<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b2\uff1a\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>import fcntl<\/p>\n<p>import struct<\/p>\n<p>def create_socket_bound_to_interface(interface_name, destination_ip, destination_port):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    # \u5c06socket\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3<\/p>\n<p>    ifname = interface_name.encode(&#39;utf-8&#39;)<\/p>\n<p>    fcntl.ioctl(sock.fileno(), 0x8933, struct.pack(&#39;256s&#39;, ifname[:15]))<\/p>\n<p>    # \u8fde\u63a5\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>    sock.connect((destination_ip, destination_port))<\/p>\n<p>    return sock<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>interface_name = &#39;eth0&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>destination_port = 80<\/p>\n<p>sock = create_socket_bound_to_interface(interface_name, destination_ip, destination_port)<\/p>\n<p>sock.send(b&#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;)<\/p>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode())<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b3\uff1a\u8bbe\u7f6e\u81ea\u5b9a\u4e49IP\u5934<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>import struct<\/p>\n<p>def checksum(data):<\/p>\n<p>    s = 0<\/p>\n<p>    for i in range(0, len(data), 2):<\/p>\n<p>        w = (data[i] &lt;&lt; 8) + (data[i+1] if i+1 &lt; len(data) else 0)<\/p>\n<p>        s = (s + w) &amp; 0xffff<\/p>\n<p>    return ~s &amp; 0xffff<\/p>\n<p>def create_raw_socket_with_custom_ip_header(source_ip, destination_ip):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u539f\u59cbsocket\u5bf9\u8c61<\/p>\n<p>    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)<\/p>\n<p>    # \u8bbe\u7f6eIP_HDRINCL\u9009\u9879<\/p>\n<p>    sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)<\/p>\n<p>    # \u521b\u5efaIP\u5934<\/p>\n<p>    ip_header = struct.pack(<\/p>\n<p>        &#39;!BBHHHBBH4s4s&#39;,<\/p>\n<p>        0x45, 0, 0, 0, 0, 255, socket.IPPROTO_TCP, 0,<\/p>\n<p>        socket.inet_aton(source_ip),<\/p>\n<p>        socket.inet_aton(destination_ip)<\/p>\n<p>    )<\/p>\n<p>    # \u8ba1\u7b97IP\u5934\u6821\u9a8c\u548c<\/p>\n<p>    ip_header = ip_header[:10] + struct.pack(&#39;H&#39;, checksum(ip_header)) + ip_header[12:]<\/p>\n<p>    return sock, ip_header<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>source_ip = &#39;192.168.1.100&#39;<\/p>\n<p>destination_ip = &#39;192.168.1.200&#39;<\/p>\n<p>sock, ip_header = create_raw_socket_with_custom_ip_header(source_ip, destination_ip)<\/p>\n<p>sock.sendto(ip_header + b&#39;Hello, World!&#39;, (destination_ip, 0))<\/p>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u4fee\u6539socket\u7684\u6e90\u5730\u5740\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u786e\u4fdd\u6e90IP\u5730\u5740\u548c\u7f51\u7edc\u63a5\u53e3\u7684\u6709\u6548\u6027\uff0c\u4ee5\u53ca\u6570\u636e\u5305\u7684\u6b63\u786e\u6027\uff0c\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684socket\u7f16\u7a0b\u4e2d\u8bbe\u7f6e\u6e90\u5730\u5740\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>socket.bind()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6e90\u5730\u5740\u3002\u5728\u521b\u5efasocket\u5bf9\u8c61\u540e\uff0c\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\u5c06\u5176\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\u4f8b\u5982\uff0c<code>sock.bind((&#39;192.168.1.2&#39;, 12345))<\/code>\u5c06\u6e90\u5730\u5740\u8bbe\u7f6e\u4e3a192.168.1.2\u548c\u7aef\u53e312345\u3002\u786e\u4fdd\u4f7f\u7528\u7684IP\u5730\u5740\u662f\u672c\u673a\u7684\u6709\u6548\u5730\u5740\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528socket\u65f6\uff0c\u6e90\u5730\u5740\u7684\u9009\u62e9\u4f1a\u5f71\u54cd\u6027\u80fd\u5417\uff1f<\/strong><br \/>\u6e90\u5730\u5740\u7684\u9009\u62e9\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u7f51\u7edc\u7684\u5ef6\u8fdf\u548c\u5e26\u5bbd\u5229\u7528\u7387\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u9700\u8981\u5728\u591a\u4e2a\u7f51\u7edc\u63a5\u53e3\u4e0a\u8fdb\u884c\u901a\u4fe1\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6e90\u5730\u5740\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u6570\u636e\u6d41\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u9760\u8fd1\u76ee\u6807\u670d\u52a1\u5668\u7684\u672c\u5730IP\u5730\u5740\u53ef\u80fd\u4f1a\u51cf\u5c11\u5ef6\u8fdf\u3002\u56e0\u6b64\uff0c\u4e86\u89e3\u7f51\u7edc\u62d3\u6251\u7ed3\u6784\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5728socket\u4e2d\u4f7f\u7528\u591a\u4e2a\u6e90\u5730\u5740\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u5728Python\u7684socket\u5e93\u4e2d\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2asocket\u53ea\u80fd\u7ed1\u5b9a\u5230\u4e00\u4e2a\u6e90\u5730\u5740\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u6e90\u5730\u5740\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u4e2asocket\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u7ed1\u5b9a\u5230\u4e0d\u540c\u7684\u6e90\u5730\u5740\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u540c\u4e00\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u591a\u8def\u590d\u7528\uff0c\u540c\u65f6\u4e0e\u4e0d\u540c\u7684\u76ee\u6807\u8fdb\u884c\u901a\u4fe1\u3002\u786e\u4fdd\u4e3a\u6bcf\u4e2asocket\u9009\u62e9\u5408\u9002\u7684\u6e90\u5730\u5740\uff0c\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fee\u6539socket\u7684\u6e90\u5730\u5740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528bind\u65b9\u6cd5\u7ed1\u5b9a\u7279\u5b9a\u7684IP\u5730\u5740\u3001\u4f7f\u7528S [&hellip;]","protected":false},"author":3,"featured_media":1065256,"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\/1065250"}],"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=1065250"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065250\/revisions"}],"predecessor-version":[{"id":1065260,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065250\/revisions\/1065260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065256"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}