{"id":1705,"date":"2014-12-22T13:07:05","date_gmt":"2014-12-22T13:07:05","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1705"},"modified":"2014-12-22T13:07:05","modified_gmt":"2014-12-22T13:07:05","slug":"%e5%9c%a8python%e4%b8%ad%e7%94%a8socket%e6%8d%95%e8%8e%b7%e6%8c%87%e5%ae%9a%e7%bd%91%e5%8d%a1%e7%9a%84%e6%b5%81%e9%87%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1705.html","title":{"rendered":"\u5728Python\u4e2d\u7528socket\u6355\u83b7\u6307\u5b9a\u7f51\u5361\u7684\u6d41\u91cf"},"content":{"rendered":"<p>\u641c\u7d22\uff08<strong>Python<\/strong> <strong>socket<\/strong> <strong>bind<\/strong> <strong>interface<\/strong>\uff09\uff1a<a href=\"http:\/\/search.aol.com\/aol\/search?q=python+socket+bind+interface\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+socket+bind+interface<\/a><\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/335607\/how-do-i-make-an-outgoing-socket-to-a-specific-network-interface\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/335607\/how-do-i-make-an-outgoing-socket-to-a-specific-network-interface<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8437726\/can-python-select-what-network-adapter-when-opening-a-socket\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/8437726\/can-python-select-what-network-adapter-when-opening-a-socket<\/a><\/li>\n<\/ul>\n<p>\u6307\u5b9a\u8fde\u63a5\u5230Internet\u7684\u7f51\u5361\uff1a<\/p>\n<p><a href=\"http:\/\/www.quora.com\/Which-Python-library-allows-us-to-specify-network-interface-to-connect-to-the-Internet\" target=\"_blank\">http:\/\/www.quora.com\/Which-Python-library-allows-us-to-specify-network-interface-to-connect-to-the-Internet<\/a><\/p>\n<ol>\n<li>Get the IP address for the interface that you want.<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">import socket\nimport fcntl\nimport struct\ndef get_ip_address(ifname):\n    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n    return socket.inet_ntoa(fcntl.ioctl(\n        s.fileno(),\n        0x8915,\n        struct.pack('256s', ifname[:15])\n    )[20:24])\n\nYou can use it like this:\n&gt;&gt;&gt; get_ip_address('lo')\n'127.0.0.1'\n&gt;&gt;&gt; get_ip_address('eth0')\n'38.113.228.130'<\/pre>\n<p>Source: <a href=\"http:\/\/code.activestate.com\/recipes\/439094-get-the-ip-address-associated-with-a-network-inter\/\" target=\"_blank\">http:\/\/code.activestate.com\/recipes\/439094-get-the-ip-address-associated-with-a-network-inter\/<\/a><\/p>\n<ol start=\"2\">\n<li>Create a new socket bind it to your interface.<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">import socket\ns = socket.socket()\ns.bind((get_ip_address('eth0'), 0))<\/pre>\n<ol start=\"3\">\n<li>And then:<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">s.connect(('http:\/\/www.google.com\/', 80))<\/pre>\n<ul>\n<li><a href=\"http:\/\/www.pythonforpentesting.com\/2014\/03\/python-raw-sockets.html\" target=\"_blank\">http:\/\/www.pythonforpentesting.com\/2014\/03\/python-raw-sockets.html<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/offensive-python\/Sniffy\/blob\/master\/Sniffy.py\" target=\"_blank\">https:\/\/github.com\/offensive-python\/Sniffy\/blob\/master\/Sniffy.py<\/a><\/li>\n<\/ul>\n<p>\u6bd4\u8f83\u597d\u7684\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/www.pythonforpentesting.com\/2014\/09\/packet-injection-capturing-response.html\" target=\"_blank\">http:\/\/www.pythonforpentesting.com\/2014\/09\/packet-injection-capturing-response.html<\/a><\/li>\n<li><a href=\"http:\/\/www.pythonforpentesting.com\/2014\/08\/tcp-packet-injection-with-python.html\" target=\"_blank\">http:\/\/www.pythonforpentesting.com\/2014\/08\/tcp-packet-injection-with-python.html<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/OffensivePython\" target=\"_blank\">https:\/\/github.com\/OffensivePython<\/a><\/li>\n<li><a href=\"http:\/\/www.binarytides.com\/category\/sockets\/python-sockets-sockets\/\" target=\"_blank\">http:\/\/www.binarytides.com\/category\/sockets\/python-sockets-sockets\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/socket.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/socket.html<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u5728Windows\u548cLinux\u7cfb\u7edf\u4e0a\u9762socket\u6a21\u5757\u91cc\u9762\u6709\u7684\u5185\u5bb9\u6709\u4e00\u4e9b\u90fd\u4e0d\u4e00\u6837o(\u256f\u25a1\u2570)o\uff0c\u5728Windows\u4e0aOK\uff1a<\/p>\n<pre class=\"lang:default decode:true\">s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)# Enable promiscuous mode\nIn [3]: socket.SIO_RCVALL\nOut[3]: 2550136833L\n\nIn [4]: socket.RCVALL_ON\nOut[4]: 1<\/pre>\n<p>\u5728Linux\u4e0a\u4f1a\u51fa\u73b0\u9519\u8bef\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [47]: socket.RCVALL_ON\n...\nAttributeError: 'module' object has no attribute 'RCVALL_ON'\n...\nAttributeError: 'module' object has no attribute 'SIO_RCVALL'<\/pre>\n<p>\u6240\u4ee5\u6587\u7ae0\uff1a<a href=\"http:\/\/www.pythonforpentesting.com\/2014\/03\/python-raw-sockets.html\" target=\"_blank\">http:\/\/www.pythonforpentesting.com\/2014\/03\/python-raw-sockets.html<\/a> \u4e2d\u4ecb\u7ecd\u7684\u65e0\u6cd5\u7528\u4e8eLinux\u7cfb\u7edf\u4e0a\u3002<\/p>\n<p>\u4e0d\u8fc7\u5728\u6587\u7ae0\uff1a<\/p>\n<p><a href=\"http:\/\/www.pythonforpentesting.com\/2014\/09\/packet-injection-capturing-response.html\" target=\"_blank\">http:\/\/www.pythonforpentesting.com\/2014\/09\/packet-injection-capturing-response.html<\/a>\uff0c\u4e2d\u63d0\u5230\u7684\u7ed1\u5b9a\u6307\u5b9a\u7f51\u5361\uff0c\u53ef\u4ee5\u6709\uff1a<\/p>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/socket_bind.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1706\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/socket_bind.png\" alt=\"socket_bind\" width=\"628\" height=\"254\" \/><\/a><\/p>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/socket_bind2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1707\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/socket_bind2.png\" alt=\"socket_bind2\" width=\"707\" height=\"88\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\uff08Python socket bind interface\uff09\uff1ahttp:\/\/search.aol.com\/ [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[417,8,173],"class_list":["post-1705","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-bind","tag-python","tag-socket"],"views":5467,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1705"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1705\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}