Skip to content

Instantly share code, notes, and snippets.

@ninedraft
Last active July 6, 2024 05:58
Show Gist options
  • Save ninedraft/7c47282f8b53ac015c1e326fffb664b5 to your computer and use it in GitHub Desktop.
Save ninedraft/7c47282f8b53ac015c1e326fffb664b5 to your computer and use it in GitHub Desktop.
Python udp broadcast client server example.

Python udp broadcast client-server example

⚠️ ❗ ATTENTION ❗ ⚠️

This gist is deprecated and will not be edited in the future. Consider visit ninedraft/python-udp repo. It will not be deleted, however.

⚠️ ❗ ATTENTION ❗ ⚠️

Works for python 3.7 and 2.7 for Mac OS and Linux(kernel>=3.9) hosts. If you'ra using linux(kernel<3.9), then use socket.O_REUSEADDR instead of socket.SO_REUSEPORT to share (host, port) between multiple clients and servers.

Tricks and traps:

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
# Thanks @seym45 for a fix
data, addr = client.recvfrom(1024)
print("received message: %s"%data)
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Set a timeout so the socket does not block
# indefinitely when trying to receive data.
server.settimeout(0.2)
message = b"your very important message"
while True:
server.sendto(message, ('<broadcast>', 37020))
print("message sent!")
time.sleep(1)
@weihohoho
Copy link

Thank you for offer the code. I was runing the server and client codes on a group of remote machines. I wrote a powershell script to SSH into the server and client machines to start the python codes.
It seems the python codes freezes/hang the SSH every time. May I ask what is causing that and if there is a way around it? Thanks!

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