Problem Formulation: How to send and receive UDP multicast messages in Python?
Background: Multicast is a distributed systems concept for group communication over a network (one-to-many or many-to-many). The choice of the network “transport layer” which the Multicast uses determines its type—for example, IP Multicast is sending a multicast over the IP layer and UDP Multicast is sending it over the User Datagram Protocol (UDP) layer that doesn’t provide any deliverability guarantees, so it’s unreliable—massages may be lost or delivered out of order. But this makes UDP Multicast also fast, lightweight, and the protocol of choice for many streaming scenarios such as Netflix sending UDP Multicast to all watchers of a specific show.
We differentiate between sending and receiving UDP Multicast messages in Python. For the code presented in this tutorial, you don’t need to custom install any library as we only need the
socket library which is already in Python’s standard library. ?
Sending UDP Multicast
To send a UDP message to a multicast group listening on a given IP address and PORT, overwrite the IP address, PORT, and MESSAGE and run the following code:
# SENDER import socket group = '188.8.131.52' port = 5004 # 2-hop restriction in network ttl = 2 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) sock.sendto(b"hello world", (group, port))
If you’ve sent a message nobody is listening to, does it even exist? 😉
Receiving UDP Multicast
Here’s how you can create a receiver listening to the IP address and PORT and printing all received messages to the screen:
# RECEIVER import socket import struct MCAST_GRP = '184.108.40.206' MCAST_PORT = 5004 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 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: print(sock.recv(10240))
You can customize the processing of the messages as you desire. If you want to have multiple receivers, just use this exact code to start multiple Python scripts that receive the same message listening to the same port and receiving the same UDP packets.
Distributed systems engineers must be masters of at least one programming language. I know from personal experience as a distributed system doctoral researcher. If you want to boost your Python skills, consider joining my free email academy that teaches you everything you need to become a skilled Python pro! It’s free!
There are only 10 kinds of people in this world: those who know binary and those who don’t.
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.