Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Send UDP Multicast in Python?

#1
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)https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 1024px) 100vw, 1024px" />

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!

Sources

The post How to Send UDP Multicast in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 36 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 42 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 47 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 36 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 56 02-01-2021, 03:25 AM
Last Post: xSicKxBot
  [Tut] Python help() xSicKxBot 0 41 01-22-2021, 01:02 AM
Last Post: xSicKxBot
  [Tut] Python globals() xSicKxBot 0 46 01-19-2021, 04:16 AM
Last Post: xSicKxBot
  [Tut] Python getattr() xSicKxBot 0 54 12-28-2020, 05:11 AM
Last Post: xSicKxBot
  [Tut] Python delattr() xSicKxBot 0 55 12-26-2020, 12:06 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) Gloria Victis, Crackerjack ...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 1
AppleInsider - iPhone 11 reunited with o...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 1
News - Video: Beautiful Ghibli-Esque Pla...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 1
News - Fortnite: Ant-Man Will Be The Nex...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 1
Haven | A tale of two lover who gave up ...
Last Post: SickProdigy
Today 12:32 AM
» Replies: 0
» Views: 6
News - Hate Zips? Then This Magnet-Power...
Last Post: xSicKxBot
Yesterday 08:17 PM
» Replies: 0
» Views: 3
News - Fortnite: Ant-Man Will Be The Nex...
Last Post: xSicKxBot
Yesterday 08:17 PM
» Replies: 0
» Views: 3
News - The Case of the Missing Rewards
Last Post: xSicKxBot
Yesterday 05:31 PM
» Replies: 0
» Views: 2
Xbox Wire - Minecraft Dungeons Flames of...
Last Post: xSicKxBot
Yesterday 05:30 PM
» Replies: 0
» Views: 3
News - Don’t Miss: A 2018 economic deep ...
Last Post: xSicKxBot
Yesterday 05:30 PM
» Replies: 0
» Views: 5

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020