Python不会接收UDP数据包(非广播)

时间:2016-06-29 17:22:10

标签: python sockets networking udp

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

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

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

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

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

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

创建套接字没有问题:

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

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

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

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

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

接收,我的脚本只是等待recvfrom()方法调用。这对我来说意味着它不接收数据包。即使我通过Wireshark验证了它。

我真的把头靠在墙上,任何帮助都会让人感到沮丧......

贝娄是完整的状态"从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  ..............~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

1 个答案:

答案 0 :(得分:0)

原来,我的"服务器"发送的地方不是IP Kosher。所以他们在网络和传输层遭到拒绝。解决方法是不使用python套接字类,而是使用introduced in Java 1.0和ctypes

直接与OSI-L2通信