我正在 Windows XP 机器上用 python2.7 编写脚本。本机使用不同的网卡连接到多个网络。
我遇到了一个问题,我已将 UDP 套接字绑定到特定接口(我知道您可以通过仅提供网卡现有的 IP 地址来在 Windows 中完成此操作)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind(('10.31.9.0', 6466)) #<<< 10.31.9.0 is address of desired card
然后我将超时设置为5秒
self.sock.settimeout(5)
然后我尝试向服务器发送一条消息,我可以证明该消息存在且有效。然后等待回复。
self.destintation = ('10.42.40.34', 62434)
# Send the msg
self.sock.sendto(msg, self.destintation)
# receive data
reply, addr = self.sock.recvfrom(1024)
然而一个socket.timeout
总是被抛出。所以我打开wire shark看看出了什么问题,结果发现我的初始消息从未发送到所需的接口上。
我看到的是不同接口上的 arp 广播(10.10.10.12
)从我的机器询问谁连接到我想要的目标IP:
1 0.000000 IntelCor_8c:6d:97 Broadcast ARP 42 Who has 10.42.40.34? Tell 10.10.10.12
当然,广播没有响应,因为从 10.10.10.12 接口无法访问 10.42.40.34 地址/机器
我如何告诉Python发送ARP广播'10.31.9.0'
?我做错了什么?
EDIT:
附加信息>
我正在使用的接口的网络是 B 类
(网络掩码为 255.255.0.0)
The interface IP is : 10.31.9.0
The target IP is: 10.42.40.34.
我想知道这个问题是否是由于我的目标位于单独的子网上而导致的。但是,正如相关问题中所述here https://stackoverflow.com/questions/38106302/python-wont-receive-udp-packetsnon-broadcast。有从服务器到我的流量...=/
UPDATE:
“路线 PRINT 10*”的结果
Active Routes:
Network Destination Netmask Gateway Interface Metric
10.0.0.0 255.0.0.0 10.10.10.12 10.10.10.12 10
10.10.10.12 255.255.255.255 127.0.0.1 127.0.0.1 10
10.31.0.0 255.255.0.0 10.31.9.0 10.31.9.0 10
10.31.9.0 255.255.255.255 127.0.0.1 127.0.0.1 10
10.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 10
10.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 10
Default Gateway: 153.4.84.1
===========================================================================
Persistent Routes:
None
更新#2全路线打印
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 153.4.84.1 153.4.85.81 10
10.10.0.0 255.255.0.0 10.10.10.12 10.10.10.12 10
10.10.10.12 255.255.255.255 127.0.0.1 127.0.0.1 10
10.31.0.0 255.255.0.0 10.31.9.0 10.31.9.0 10
10.31.9.0 255.255.255.255 127.0.0.1 127.0.0.1 10
10.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 10
10.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 10
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
153.4.84.0 255.255.252.0 153.4.85.81 153.4.85.81 10
153.4.85.81 255.255.255.255 127.0.0.1 127.0.0.1 10
153.4.255.255 255.255.255.255 153.4.85.81 153.4.85.81 10
192.168.56.0 255.255.255.0 192.168.56.1 192.168.56.1 20
192.168.56.1 255.255.255.255 127.0.0.1 127.0.0.1 20
192.168.56.255 255.255.255.255 192.168.56.1 192.168.56.1 20
224.0.0.0 240.0.0.0 10.10.10.12 10.10.10.12 10
224.0.0.0 240.0.0.0 10.31.9.0 10.31.9.0 10
224.0.0.0 240.0.0.0 153.4.85.81 153.4.85.81 10
224.0.0.0 240.0.0.0 192.168.56.1 192.168.56.1 20
255.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 1
255.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 1
255.255.255.255 255.255.255.255 153.4.85.81 153.4.85.81 1
255.255.255.255 255.255.255.255 192.168.56.1 192.168.56.1 1
255.255.255.255 255.255.255.255 192.168.56.1 5 1
Default Gateway: 153.4.84.1
===========================================================================
Persistent Routes:
None