Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows中使用python将原始套接字绑定到接口_Python_Windows_Sockets - Fatal编程技术网

在Windows中使用python将原始套接字绑定到接口

在Windows中使用python将原始套接字绑定到接口,python,windows,sockets,Python,Windows,Sockets,从绑定到Windows下指定接口的套接字读取原始数据包时,还会显示在其他接口上接收的数据包。这是Windows下的限制吗 import socket as s HOST = '192.168.1.101' # s.gethostbyname(s.gethostname()) socket = s.socket(s.AF_INET, s.SOCK_RAW) socket.bind((HOST, 0)) while True: print(str(socket.recv(1000)))

从绑定到Windows下指定接口的套接字读取原始数据包时,还会显示在其他接口上接收的数据包。这是Windows下的限制吗

import socket as s
HOST = '192.168.1.101' # s.gethostbyname(s.gethostname())
socket = s.socket(s.AF_INET, s.SOCK_RAW)
socket.bind((HOST, 0))

while True:
    print(str(socket.recv(1000)))
代码遵循和中给出的示例。如果可能,如何实现预期结果?

我添加到您的代码中:

socket.ioctl(s.SIO_RCVALL, s.RCVALL_ON)
然后在使用Python 3.5的Windows 7上运行:

import socket as s

HOSTS = ('169.254.222.66', '10.50.220.132')
for HOST in HOSTS:
    print('---', HOST)
    socket = s.socket(s.AF_INET, s.SOCK_RAW)
    socket.bind((HOST, 0))
    socket.ioctl(s.SIO_RCVALL, s.RCVALL_ON)
    for i in range(10):
        x = socket.recvfrom(65535)
        print(x[1], ' '.join(['%02x' % b for b in x[0]][:20]))
    socket.ioctl(s.SIO_RCVALL, s.RCVALL_OFF)
作为输出,我得到:

--- 169.254.222.66
('169.254.222.66', 0) 45 00 00 8d 6e d4 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 07 2d 6e d5 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 09 ad 6e d7 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 0a 3d 6e d9 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 04 3d 6e db 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 00 dd 6e dc 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 07 bd 6e dd 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 08 5d 6e df 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 06 4d 6e e1 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
('169.254.222.66', 0) 45 00 06 cd 6e e3 40 00 80 06 00 00 a9 fe de 42 a9 fe cc 5f
--- 10.50.220.132
('10.50.220.158', 0) 45 00 00 a1 64 18 00 00 01 11 7e 69 0a 32 dc 9e ef ff ff fa
('10.50.220.132', 0) 45 00 00 28 7d 4e 40 00 80 06 e3 cc 0a 32 dc 84 ac d9 06 25
('10.50.220.247', 0) 45 00 00 4e 02 9e 00 00 80 11 66 a6 0a 32 dc f7 0a 32 df ff
('10.50.221.3', 0) 45 00 00 4e 35 57 00 00 80 11 33 e1 0a 32 dd 03 0a 32 df ff
('10.50.220.149', 0) 45 00 00 f1 37 5f 40 00 04 11 67 db 0a 32 dc 95 ef ff ff fa
('10.50.220.149', 0) 45 00 00 f1 b3 04 40 00 40 11 a0 30 0a 32 dc 95 ff ff ff ff
('10.50.220.247', 0) 45 00 00 4e 02 9f 00 00 80 11 66 a5 0a 32 dc f7 0a 32 df ff
('10.50.221.3', 0) 45 00 00 4e 35 58 00 00 80 11 33 e0 0a 32 dd 03 0a 32 df ff
('10.50.220.176', 0) 45 00 00 4e 19 14 00 00 80 11 50 77 0a 32 dc b0 0a 32 df ff
('10.50.221.3', 0) 45 00 00 4e 35 59 00 00 80 11 33 df 0a 32 dd 03 0a 32 df ff

所以在我的测试中,没有混合接口

这个问题可能是由于数据包没有使用您期望的接口。您可以在不希望用于路由的接口上增加接口度量。这将使您希望使用的接口成为首选

在Windows 7中:

  • 转到控制面板
  • 选择网络和共享
  • 选择“更改适配器设置”
  • 选择非首选的路由接口
  • 从列表中选择Internet协议版本4(TCP/IPv4)
  • 选择高级
  • 取消选择自动度量
  • 添加一个足够大的接口度量,以允许首选任何其他接口

  • 我试着运行你的代码,但得到了不同的结果。当绑定到
    169.254.222.66(255.255.255.0)
    时,它显示来自两个接口的数据包。绑定到
    10.50.220.132(255.255.252.0)
    仅显示来自预期接口的数据包。我也运行Windows7,但运行Python 3.6。我将检查Python3.5这个问题在Python3.5中也存在。有时我必须运行它多次,然后才能观察它。感谢您的帮助查看
    route print
    ipconfig
    tracert 192.168.1.100
    显示有时连接通过第二个接口上的标准网关路由,这可能会很有帮助。这方面的另一个指标是,与标准网关路由20相比,预期路由的
    route print
    显示的高度量为266。因此,问题与Python无关,而是与Windows切换路由有关。我仍在寻找一种方法来修复这条路线。或者有没有一种方法不是通过IP而是通过套接字应该绑定到的接口?再次感谢Stephen,你表现出了极大的耐心!我也试过了。首先是中等价值,然后是难以置信的高价值。想要的路由比任何其他路由都低10到1000点,但tracert仍然显示数据包是通过两个接口发送的。这些接口在同一子网上吗?因为我成功地使用了上述技术。隐马尔可夫模型。。。祝你好运。不,他们有不同的子网