Skip to content

Instantly share code, notes, and snippets.

@knotman90
Created January 26, 2022 10:22
Show Gist options
  • Save knotman90/3a970484b97bb6e42ad7db606b881178 to your computer and use it in GitHub Desktop.
Save knotman90/3a970484b97bb6e42ad7db606b881178 to your computer and use it in GitHub Desktop.
import socket
import struct
import sys
import binascii
assert len(sys.argv) ==3
IP = sys.argv[1]
PORT = sys.argv[2]
print "Connecting to",IP, "on port", PORT
MCAST_GRP = IP
MCAST_PORT = int(PORT)
IS_ALL_GROUPS = True
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if IS_ALL_GROUPS:
# on this port, receives ALL multicast groups
sock.bind(('', MCAST_PORT))
else:
# on this port, listen ONLY to MCAST_GRP
sock.bind((MCAST_GRP, MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while True:
# For Python 3, change next line to "print(sock.recv(10240))"
print sock.recv(10240)
@knotman90
Copy link
Author

Usage:

dump_udp_multicast.py MULTICAST_IP_ADDRESS PORT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment