Here’s a nugget of info that makes writing to and capturing data from the broadcast address easy.
Notes for configuration of udp data logging/monitoring with python.
# To send data on the braoccast address:
##############################################################
#!/usr/bin/env python
import socket
import re
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
localipaddress = socket.gethostbyname(socket.gethostname())
udpbcstaddress = re.sub('.\d+$','.255',localipaddress)
s.sendto( message, ( udpbcstaddress, port)
# To receive data on the broadcast address:
##############################################################
#!/usr/bin/env python
import socket, traceback
s=socket.socket( socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = '' # binds to all interfaces, otherwise give ip address
port = 33333
s.bind( (host, port) )
while 1:
try:
message, address = s.recvfrom(1024)
print address
print message
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()
As a side note, I wanted to do this with Python’s logging package, however I could not find a straight-forward way to set the options on the sockets that allow use of the broadcast address, and the default for the logging package seems to be to pickle the strings before sending them which means I'd have to have a python routine to capture them on the other end to unpickle. Not what I want.