I have a device connected to a windows 10 machine and this device is sending out various multicast UDP packages. I can see them on the windows machine using wireshark
:
Now to get them in python (3.10.10) I am using the following script:
import struct
import socket
import select
HOST = "192.168.200.5"
MCAST_GRP = "239.0.0.12"
PORT = 45013
def initSocket(group, port, host):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("0.0.0.0", port))
# struct.pack converts non-bytes values to bytes. To be used as the last parameter of
# setsockopt which needs to be a bytes-like object representing a buffer.
# "4sl" is a format specifier for a 4-bytes string and a long int.
mreq = struct.pack("4sl", socket.inet_aton(group), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
sock = initSocket(MCAST_GRP, PORT, HOST)
while True:
ready = select.select([sock], [], [], 1)
if ready[0]:
data = sock.recv(4096)
print(f"received message: {data}")
else:
print("No data received")
But no message seems to be received! I only get No data received
.
I am running this script as admin, and no other process seems to be using port 45013 (according to netstat -ano -p udp
). I also allowed the "python" app through the firewall.
The device itself is connected to an ethernet adaptor (with IP address 192.168.200.5
) to the computer via USB. And the computer DOES receive the UDP messages (see wireshark).
netsh int ip show join
shows
Interface 25: Ethernet
Scope References Last Address
---------- ---------- ---- ---------------------------------
0 0 Yes 224.0.0.1
0 4 Yes 224.0.0.251
0 1 Yes 224.0.0.252
0 3 Yes 239.255.255.250
So what else can I try? How to make it work? What is the problem?
Maybe there is a workaround to use wireshark inside python to receive the UDP data?
I have been using a C code to receive the UDP data on a Linux VM running on windows, and after some configuring and re-configuring settings and IP addresses the C code is able to receive the data. But not the python code. The python code now chooses to create a new error (on Linux VM only):
Traceback (most recent call last):
File "udp_receiver.py", line 54, in <module>
sock = initSocket(MCAST_GRP, PORT, HOST)
File "udp_receiver.py", line 32, in initSocket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
OSError: [Errno 19] No such device