-
-
Save ipolo0000i/fce47f1ede570e2638bb0e3e4ba3e75d to your computer and use it in GitHub Desktop.
Python udp broadcast client server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | |
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
client.bind(("", 37020)) | |
while True: | |
data, addr = client.recvfrom(1024) | |
print("received message: %s"%data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
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) | |
server.bind(("", 44444)) | |
message = b"your very important message" | |
while True: | |
server.sendto(message, ('<broadcast>', 37020)) | |
print("message sent!") | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment