Created
November 13, 2015 18:53
-
-
Save carlochess/53e444c9db6b9deec3a2 to your computer and use it in GitHub Desktop.
Proof of concept: UDP broadcasting over a local network
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
from socket import * | |
myip = gethostbyname(gethostname()) | |
broadip = inet_ntoa( inet_aton(myip)[:3] + b'\xff' ) | |
addr = (broadip, 34353) | |
UDPSock = socket(AF_INET, SOCK_DGRAM) | |
data = u'holii' | |
UDPSock.sendto(data.encode('utf-8'), addr) | |
print("Sending message '%s'..." % data) | |
UDPSock.close() | |
print('Client stopped.') |
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 | |
addr = ('', 34353) | |
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
UDPSock.bind(addr) | |
while True: | |
data, addr = UDPSock.recvfrom(1024) | |
if data == 'stop': | |
print('Client wants me to stop.') | |
break | |
else: | |
print("From addr: '%s', msg: '%s'" % (addr[0], data)) | |
UDPSock.close() | |
print ('Server stopped.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment