python recvfrom在Windows上工作,但Linux不起作用

发布于 2025-01-18 13:35:53 字数 3819 浏览 0 评论 0原文

有一个Ti雷达设备使用静态以太网接口,我编写了Python脚本进行测试。当设备收到从此脚本发送的消息时,它会响应另一个消息,该消息在Windows和Linux上都显示在Wireshark上。但是,即使RECVFROM函数返回Windows上的数据,它也会在Linux上使用(我在Linux Mint笔记本电脑和Raspberry Pi上尝试了它)。我也尝试使用UFW添加各种规则,例如“ Sudo UFW允许从192.168.33.180允许”,但它仍然淘汰。这是代码:

import socket
import codecs

cmd = '0100'
CONFIG_HEADER = '5aa5'
CONFIG_FOOTER = 'aaee'
length='0000'
body=''
msg = codecs.decode(''.join((CONFIG_HEADER, str(cmd), length, body, CONFIG_FOOTER)), 'hex')
sock = socket.socket(socket.AF_INET,
                    socket.SOCK_DGRAM,
                    socket.IPPROTO_UDP)

sock.settimeout(10)
sock.bind(('192.168.33.30', 4096))
send = (msg, ('192.168.33.180', 4096))
sock.sendto(send[0], send[1])
print(send)
recv = sock.recvfrom(4096)
print(recv)

以下是在Windows上运行代码的结果:

PS C:\ti\AWR1243-Capture> python .\python\test.py
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 4096))
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 1024))

这是在Linux上运行代码的结果:

python python/test.py 
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 4096))
Traceback (most recent call last):
  File "python/test.py", line 19, in <module>
    recv = sock.recvfrom(4096)
socket.timeout: timed out

另外,Wireshark输出将导出到Windows上的.txt:

No.     Time           Source                Destination           Protocol Length Info
      1 0.000000       192.168.33.30         192.168.33.180        UDP      50     4096 → 4096 Len=8

Frame 1: 50 bytes on wire (400 bits), 50 bytes captured (400 bits) on interface \Device\NPF_{7D06A449-C87B-4FB9-98DE-A4257C349390}, id 0
Ethernet II, Src: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07), Dst: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c)
Internet Protocol Version 4, Src: 192.168.33.30, Dst: 192.168.33.180
User Datagram Protocol, Src Port: 4096, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

No.     Time           Source                Destination           Protocol Length Info
      2 0.000053       192.168.33.180        192.168.33.30         UDP      60     1024 → 4096 Len=8

Frame 2: 60 bytes on wire (480 bits), 60 bytes captured (480 bits) on interface \Device\NPF_{7D06A449-C87B-4FB9-98DE-A4257C349390}, id 0
Ethernet II, Src: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c), Dst: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07)
Internet Protocol Version 4, Src: 192.168.33.180, Dst: 192.168.33.30
User Datagram Protocol, Src Port: 1024, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

和Linux上:

No.     Time           Source                Destination           Protocol Length Info
     50 5.803244767    192.168.33.30         192.168.33.180        UDP      50     4096 → 4096 Len=8

Frame 50: 50 bytes on wire (400 bits), 50 bytes captured (400 bits) on interface enp53s0, id 0
Ethernet II, Src: d4:93:90:0b:da:18 (d4:93:90:0b:da:18), Dst: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c)
Internet Protocol Version 4, Src: 192.168.33.30, Dst: 192.168.33.180
User Datagram Protocol, Src Port: 4096, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

No.     Time           Source                Destination           Protocol Length Info
     51 5.803624342    192.168.33.180        192.168.33.30         UDP      60     1024 → 4096 Len=8

Frame 51: 60 bytes on wire (480 bits), 60 bytes captured (480 bits) on interface enp53s0, id 0
Ethernet II, Src: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c), Dst: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07)
Internet Protocol Version 4, Src: 192.168.33.180, Dst: 192.168.33.30
User Datagram Protocol, Src Port: 1024, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

There's a TI radar device that uses a static Ethernet interface that I wrote a Python script to test. When the device receives the message sent from this script, it responds with another message that shows up on Wireshark on both Windows and Linux. However, even though the recvfrom function returns with the data on Windows, it times out on Linux (I tried it on a Linux Mint laptop and a Raspberry Pi). I tried adding a variety of rules to firewall using ufw too, such as "sudo ufw allow from 192.168.33.180", but it still times out. Here's the code:

import socket
import codecs

cmd = '0100'
CONFIG_HEADER = '5aa5'
CONFIG_FOOTER = 'aaee'
length='0000'
body=''
msg = codecs.decode(''.join((CONFIG_HEADER, str(cmd), length, body, CONFIG_FOOTER)), 'hex')
sock = socket.socket(socket.AF_INET,
                    socket.SOCK_DGRAM,
                    socket.IPPROTO_UDP)

sock.settimeout(10)
sock.bind(('192.168.33.30', 4096))
send = (msg, ('192.168.33.180', 4096))
sock.sendto(send[0], send[1])
print(send)
recv = sock.recvfrom(4096)
print(recv)

Here's the result of running the code on Windows:

PS C:\ti\AWR1243-Capture> python .\python\test.py
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 4096))
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 1024))

Here's the result of running the code on Linux:

python python/test.py 
(b'Z\xa5\x01\x00\x00\x00\xaa\xee', ('192.168.33.180', 4096))
Traceback (most recent call last):
  File "python/test.py", line 19, in <module>
    recv = sock.recvfrom(4096)
socket.timeout: timed out

Also, Wireshark output exported to .txt on Windows:

No.     Time           Source                Destination           Protocol Length Info
      1 0.000000       192.168.33.30         192.168.33.180        UDP      50     4096 → 4096 Len=8

Frame 1: 50 bytes on wire (400 bits), 50 bytes captured (400 bits) on interface \Device\NPF_{7D06A449-C87B-4FB9-98DE-A4257C349390}, id 0
Ethernet II, Src: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07), Dst: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c)
Internet Protocol Version 4, Src: 192.168.33.30, Dst: 192.168.33.180
User Datagram Protocol, Src Port: 4096, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

No.     Time           Source                Destination           Protocol Length Info
      2 0.000053       192.168.33.180        192.168.33.30         UDP      60     1024 → 4096 Len=8

Frame 2: 60 bytes on wire (480 bits), 60 bytes captured (480 bits) on interface \Device\NPF_{7D06A449-C87B-4FB9-98DE-A4257C349390}, id 0
Ethernet II, Src: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c), Dst: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07)
Internet Protocol Version 4, Src: 192.168.33.180, Dst: 192.168.33.30
User Datagram Protocol, Src Port: 1024, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

And on Linux:

No.     Time           Source                Destination           Protocol Length Info
     50 5.803244767    192.168.33.30         192.168.33.180        UDP      50     4096 → 4096 Len=8

Frame 50: 50 bytes on wire (400 bits), 50 bytes captured (400 bits) on interface enp53s0, id 0
Ethernet II, Src: d4:93:90:0b:da:18 (d4:93:90:0b:da:18), Dst: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c)
Internet Protocol Version 4, Src: 192.168.33.30, Dst: 192.168.33.180
User Datagram Protocol, Src Port: 4096, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

No.     Time           Source                Destination           Protocol Length Info
     51 5.803624342    192.168.33.180        192.168.33.30         UDP      60     1024 → 4096 Len=8

Frame 51: 60 bytes on wire (480 bits), 60 bytes captured (480 bits) on interface enp53s0, id 0
Ethernet II, Src: 0c:22:38:4e:5a:0c (0c:22:38:4e:5a:0c), Dst: Micro-St_1a:f5:07 (30:9c:23:1a:f5:07)
Internet Protocol Version 4, Src: 192.168.33.180, Dst: 192.168.33.30
User Datagram Protocol, Src Port: 1024, Dst Port: 4096
Data (8 bytes)

0000  5a a5 01 00 00 00 aa ee                           Z.......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文