How to Send UDP Multicast in Python?

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 = '224.1.1.1'
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))
UDP Multicast Send and Receive (Overview)

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 = '224.1.1.1'
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!

Programmer Humor

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.

πŸ‘©πŸ§”β€β™‚οΈπŸ‘±β€β™€οΈ

Sources