Last active
October 26, 2017 21:47
-
-
Save whymarrh/1df7def6d41782894bfb to your computer and use it in GitHub Desktop.
UDP broadcast quick playground
This file contains hidden or 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
FROM alpine:3.6 | |
RUN apk --update add python3 | |
CMD ["python3"] |
This file contains hidden or 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
#!/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)) |
This file contains hidden or 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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build & run: