具有多个接口的 Python UDP 套接字 [英] Python UDP Sockets with Multiple Interfaces

查看:63
本文介绍了具有多个接口的 Python UDP 套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 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 是所需卡的地址

然后我将超时设置为 5s

self.sock.settimeout(5)

然后我尝试将消息发送到我可以证明存在且有效的服务器.然后等待回应.

self.destination = ('10.42.40.34', 62434)# 发送消息self.sock.sendto(味精,self.destination)# 接收数据回复,地址 = self.sock.recvfrom(1024)

然而,总是抛出 socket.timeout.所以我打开wireshark查看出了什么问题,结果我的初始消息从未在所需的界面上发送.

我看到的是不同接口上的 arp 广播 (10.10.10.12) 从我的机器询问谁连接到我想要的目标 IP:

1 0.000000 IntelCor_8c:6d:97 广播 ARP 42 谁有 10.42.40.34?告诉 10.10.10.12

当然没有响应广播,因为 10.42.40.34 地址/机器无法从 10.10.10.12 接口访问

如何告诉 Python 在 '10.31.9.0' 上发送 ARP 广播?我做错了什么?

附加信息>我使用的接口的网络是 B 类(网络掩码为 255.255.0.0)

接口IP为:10.31.9.0目标 IP 为:10.42.40.34.

我想知道问题是否是由于我的目标位于单独的子网上所致.但是,如此处的相关问题所述.有从服务器到我的流量... =/

更新:

route PRINT 10*"的结果

活动路线:网络目标网络掩码网关接口指标10.0.0.0 255.0.0.0 10.10.10.12 10.10.10.12 1010.10.10.12 255.255.255.255 127.0.0.1 127.0.0.1 1010.31.0.0 255.255.0.0 10.31.9.0 10.31.9.0 1010.31.9.0 255.255.255.255 127.0.0.1 127.0.0.1 1010.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 1010.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 10默认网关:153.4.84.1============================================================================持久路由:没有任何

更新 #2全路线打印

活动路线:网络目标网络掩码网关接口指标0.0.0.0 0.0.0.0 153.4.84.1 153.4.85.81 1010.10.0.0 255.255.0.0 10.10.10.12 10.10.10.12 1010.10.10.12 255.255.255.255 127.0.0.1 127.0.0.1 1010.31.0.0 255.255.0.0 10.31.9.0 10.31.9.0 1010.31.9.0 255.255.255.255 127.0.0.1 127.0.0.1 1010.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 1010.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 10127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1153.4.84.0 255.255.252.0 153.4.85.81 153.4.85.81 10153.4.85.81 255.255.255.255 127.0.0.1 127.0.0.1 10153.4.255.255 255.255.255.255 153.4.85.81 153.4.85.81 10192.168.56.0 255.255.255.0 192.168.56.1 192.168.56.1 20192.168.56.1 255.255.255.255 127.0.0.1 127.0.0.1 20192.168.56.255 255.255.255.255 192.168.56.1 192.168.56.1 20224.0.0.0 240.0.0.0 10.10.10.12 10.10.10.12 10224.0.0.0 240.0.0.0 10.31.9.0 10.31.9.0 10224.0.0.0 240.0.0.0 153.4.85.81 153.4.85.81 10224.0.0.0 240.0.0.0 192.168.56.1 192.168.56.1 20255.255.255.255 255.255.255.255 10.10.10.12 10.10.10.12 1255.255.255.255 255.255.255.255 10.31.9.0 10.31.9.0 1255.255.255.255 255.255.255.255 153.4.85.81 153.4.85.81 1255.255.255.255 255.255.255.255 192.168.56.1 192.168.56.1 1255.255.255.255 255.255.255.255 192.168.56.1 5 1默认网关:153.4.84.1============================================================================持久路由:没有任何

解决方案

事实证明,我的服务器"发送的数据包不是 IP kosher 的.所以他们在网络和传输层被拒绝.解决方案是不使用 python 套接字类,而是使用 winpcap 和 ctypes

I'm Writing a script in python2.7 on a windows XP machine. The machine is connected to multiple networks using different network cards.

I'm running into an issue where I've bound a UDP Socket to a specific interface(I understand that you can accomplish this in windows by just providing the network cards existing IP address)

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

I then set the timeout to 5s

self.sock.settimeout(5)

Then I try to send a message out to a server that I can prove exists and works. then wait for a response.

self.destintation = ('10.42.40.34', 62434)

# Send the msg
self.sock.sendto(msg, self.destintation)

# receive data
reply, addr = self.sock.recvfrom(1024)

However a socket.timeout is always thrown. so I open up wire shark to see what is going wrong, and it turns out that my initial message never gets sent on the desired interface.

What I do see is an arp broadcast on a different interface(10.10.10.12 ) from my machine asking who is attached to my desired destination IP:

1   0.000000    IntelCor_8c:6d:97   Broadcast   ARP 42      Who has 10.42.40.34?  Tell 10.10.10.12

Of course there is no response to the broadcast because the 10.42.40.34 Address/machine is not reachable from the 10.10.10.12 interface

How do I tell Python to send the ARP broadcast out on '10.31.9.0'? What have I done Wrong?

EDIT:

Additional Information> The network for the interface I am using is a Class B (netmask is 255.255.0.0)

The interface IP is : 10.31.9.0 

The target IP is: 10.42.40.34. 

I am wondering if the issue is a result of my target sitting on a separate subnet. However, as described in a related issue here. there is traffic from the server to me... =/

UPDATE:

Results of "route 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

UPDATE #2 Full route PRINT

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

解决方案

Turns out, the Packets that my "server" was sending where not IP kosher. so they where getting rejected at the network and transport layers. Solution was to not use python socket class, but instead communicate directly to OSI-L2 using winpcap and ctypes

这篇关于具有多个接口的 Python UDP 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆