简体   繁体   English

通过python发送UDP包没有得到响应

[英]Send UDP package via python not getting response

I have a security camera that receive any data via UDP port 37810 and respond with a JSON.我有一个安全摄像头,它通过 UDP 端口 37810 接收任何数据并以 JSON 响应。 The camera ip is 192.168.1.100摄像头ip为192.168.1.100

echo 'a' |nc -u  192.168.1.100 37810 

response:回复:

DHIP??{"mac":"18:0d:2c:d1:a2:29","method":"client.notifyDevInfo","params":{"deviceInfo":{"AlarmInputChannels":0,"AlarmOutputChannels":0,"DeviceClass":"MHDX","DeviceType":"MHDX 3116","Find":"BD","HttpPort":88,"IPv4Address":{"DefaultGateway":"192.168.1.1","DhcpEnable":false,"IPAddress":"192.168.1.100","SubnetMask":"255.255.255.0"},"IPv6Address":{"DefaultGateway":"","DhcpEnable":true,"IPAddress":"\/64","LinkLocalAddress":"fe80::1a0d:2cff:fed1:a229\/64"},"Init":3238,"MachineName":"MHDX","Manufacturer":"Intelbras","Port":57777,"RemoteVideoInputChannels":0,"SerialNo":"EKHH2502545HM","Vendor":"Intelbras","Version":"4.000.00IB002.17","VideoInputChannels":16,"VideoOutputChannels":0}}}

I can confirm with tcpdump that the message was sent and the camera responded:我可以通过 tcpdump 确认消息已发送并且相机响应:

IP 192.168.0.105.56613 > 192.168.1.100.37810: UDP, length 2
IP 192.168.1.100.37810 > 192.168.0.105.53256: UDP, length 712

Then I wrote a simple python3 program to do the same:然后我写了一个简单的python3程序来做同样的事情:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,socket.IPPROTO_UDP)
s.sendto("a".encode(),("192.168.1.100",37810))

tcpdump show the that the "message" was sent, but the camera never responded. tcpdump 显示“消息”已发送,但相机从未响应。

IP 192.168.0.105.53792 > 192.168.1.100.37810: UDP, length 1

I know that this python script is not listening for incoming data, but the camera should have answered and tcpdump should have shown What am I missing?我知道这个 python 脚本没有监听传入的数据,但是相机应该已经回答并且 tcpdump 应该已经显示了我错过了什么?

echo adds a newline to the output by default. echo默认情况下会在输出中添加一个换行符。 I guess the camera is waiting for the newline to indicate the end of the request, so you need to send that in Python as well.我猜相机正在等待换行符来指示请求的结束,所以你也需要在 Python 中发送它。

s.sendto("a\n".encode(),("192.168.1.100",37810))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM