Python 不会接收 UDP 数据包(非广播) [英] Python won't Receive UDP Packets(Non Broadcast)

查看:37
本文介绍了Python 不会接收 UDP 数据包(非广播)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows XP 机器上用 python2.7 编写脚本.本机使用不同的网卡连接到多个网络.我已经发布了一个与此问题相关的问题,但没有直接解决它此处

我有一台服务器以约 1 秒的间隔通过 UDP 发送状态数据"

服务器 Ip:10.42.40.34我的机器 IP:10.31.9.0

我使用的卡:Intel(R) 千兆 CT 桌面适配器

我可以观察发送到我的机器"的服务器"数据包(通过wireshark).

2995 19.592120 10.42.40.34 10.31.9.0 UDP 455 000000007e4e9ca7000000000404040003010000000 port0:Dest54515453012 19.688119 10.42.40.34 10.31.9.0 UDP 455 000000007e4e9ca7000000000404040003010000000001004 端口... 1 源端口:D56est5262

创建套接字没有问题:

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

但是,当我将套接字绑定到接口时:

self.sock.bind(('10.31.9.0', 62415))

然后使用(没有设置超时):

回复,addr = self.sock.recvfrom(65535)

接收,我的脚本只是在 recvfrom() 方法调用处等待.这对我来说意味着它没有接收数据包.即使我确认它是通过 Wireshark 进行的.

我真的把头撞在墙上,任何帮助都会非常感激......

Bellow 是从wireshark 中提取的完整状态"数据包,我想可能某些标头信息可能是错误的,这就是为什么 python 忽略它... IDK

0000 03 00 00 00 81 b5 02 00 00 2a 28 20 08 00 45 00 .........*( ..E.0010 01 b8 1b 2b 40 00 01 11 17 a0 0a 2a 28 22 0a 1f ...+@......*("..0020 09 00 f3 cf f3 cf 01 a4 00 00 00 00 00 00 7e 4e ...............~N0030 9c a7 00 00 00 00 04 04 04 00 03 01 00 00 00 00 .....................0040 01 00 3e 99 99 9a 01 01 00 00 00 00 00 00 00 00 .....0050 00 00 00 00 00 00 00 00 00 00 3e 8c e7 04 3e 9c ......>...>.0060 43 2d bf 17 5f 6e bf 17 5f 6e 00 00 00 00 00 00 C-.._n.._n......0070 00 00 03 01 00 00 03 00 00 00 04 04 00 00 04 04 .....................0080 00 00 04 04 00 00 04 04 00 00 00 00 00 01 00 00 .....................0090 00 00 00 00 00 00 03 00 00 00 00 00 00 40 00 00 ...............@..00a0 00 00 40 90 00 00 40 86 4c cb 40 90 00 00 40 86 ..@...@.L.@...@.00b0 4c cb bf 09 09 21 40 6a 57 4e 40 6a 57 4e 3f dc L ....!@jWN@jWN?.00c0 b1 4e 3f dc b1 4e bf f7 fd 4e bf f7 fd 4e 3e 80 .N?..N...N...N>.00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................00e0 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 .....................00f0 00 0f 01 00 00 00 00 00 00 00 00 ee ee 00 00 00 .....................0100 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................0190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................01a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................01b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .....................01c0 00 00 79 c7 42 05 56 ..y.B.V

事实证明,我的服务器"发送的数据包不是 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 have already posted a question that relates to this issue but does not directly address it here

I have a server that is sending "status data" via UDP at ~1 sec intervals

The Servers Ip: 10.42.40.34 My Machines IP: 10.31.9.0

The Card I'm using: Intel(R) Gigabit CT Desktop Adapter

I can observer the "Servers" packets (via wireshark) being sent to "My Machine".

2995    19.592120   10.42.40.34 10.31.9.0   UDP 455 000000007e4e9ca700000000040404000301000000000100... Source port: 62415  Destination port: 62415
3012    19.688119   10.42.40.34 10.31.9.0   UDP 455 000000007e4e9ca700000000040404000301000000000100... Source port: 62415  Destination port: 62415

The socket is created with no problems:

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

However when I bind a socket to the interface:

self.sock.bind(('10.31.9.0', 62415))

Then use(with no timeout set):

reply, addr = self.sock.recvfrom(65535)

to receive, my script just waits at the recvfrom() method call. which implies to me that it isn't receiving packets. Even though I verified that it is via Wireshark.

I'm really banging my head against the wall, any help would be much apreciated...

Bellow is the full "status" packet pulled from wireshark, I'm thinking maybe some of the header info might be bad and that's why python is ignoring it... IDK

0000   03 00 00 00 81 b5 02 00 00 2a 28 20 08 00 45 00  .........*( ..E.
0010   01 b8 1b 2b 40 00 01 11 17 a0 0a 2a 28 22 0a 1f  ...+@......*("..
0020   09 00 f3 cf f3 cf 01 a4 00 00 00 00 00 00 7e 4e  ..............~N
0030   9c a7 00 00 00 00 04 04 04 00 03 01 00 00 00 00  ................
0040   01 00 3e 99 99 9a 01 01 00 00 00 00 00 00 00 00  ..>.............
0050   00 00 00 00 00 00 00 00 00 00 3e 8c e7 04 3e 9c  ..........>...>.
0060   43 2d bf 17 5f 6e bf 17 5f 6e 00 00 00 00 00 00  C-.._n.._n......
0070   00 00 03 01 00 00 03 00 00 00 04 04 00 00 04 04  ................
0080   00 00 04 04 00 00 04 04 00 00 00 00 00 01 00 00  ................
0090   00 00 00 00 00 00 03 00 00 00 00 00 00 40 00 00  .............@..
00a0   00 00 40 90 00 00 40 86 4c cb 40 90 00 00 40 86  ..@...@.L.@...@.
00b0   4c cb bf 09 09 21 40 6a 57 4e 40 6a 57 4e 3f dc  L....!@jWN@jWN?.
00c0   b1 4e 3f dc b1 4e bf f7 fd 4e bf f7 fd 4e 3e 80  .N?..N...N...N>.
00d0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00e0   00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00  ................
00f0   00 0f 01 00 00 00 00 00 00 00 00 ee ee 00 00 00  ................
0100   00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0110   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0120   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0130   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0140   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0150   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0160   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0170   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0180   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0190   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
01a0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
01b0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
01c0   00 00 79 c7 42 05 56                             ..y.B.V

解决方案

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天全站免登陆