Skip to content

Instantly share code, notes, and snippets.

@carlochess
Created November 13, 2015 18:53
Show Gist options
  • Save carlochess/53e444c9db6b9deec3a2 to your computer and use it in GitHub Desktop.
Save carlochess/53e444c9db6b9deec3a2 to your computer and use it in GitHub Desktop.
Proof of concept: UDP broadcasting over a local network
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.')
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