Skip to content

Instantly share code, notes, and snippets.

@whymarrh
Last active October 26, 2017 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whymarrh/1df7def6d41782894bfb to your computer and use it in GitHub Desktop.
Save whymarrh/1df7def6d41782894bfb to your computer and use it in GitHub Desktop.
UDP broadcast quick playground
FROM alpine:3.6
RUN apk --update add python3
CMD ["python3"]
#!/usr/bin/env python3
import socket
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', PORT))
print('Bound to {}'.format(PORT))
while True:
data, addr = s.recvfrom(1024)
int_val = int.from_bytes(data, byteorder='big')
print('Recv {} from {}'.format(int_val, addr))
#!/usr/bin/env python3
import socket
import time
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', PORT))
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print('Bound to {}'.format(PORT))
for i in range(256):
data = bytes([i])
s.sendto(data, ('<broadcast>', PORT))
print('Sent value: {}'.format(i))
time.sleep(2)
@whymarrh
Copy link
Author

whymarrh commented Oct 26, 2017

Build & run:

docker build -t test .
docker run -it -v $PWD:/opt -w /opt test ./recv

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