This gist is deprecated and will not be edited in the future. Consider visit ninedraft/python-udp repo. It will not be deleted, however.
Works for python 3.7 and 2.7 for Mac OS and Linux(kernel>=3.9) hosts. If you'ra using linux(kernel<3.9), then use socket.O_REUSEADDR
instead of socket.SO_REUSEPORT
to share (host, port)
between multiple clients and servers.
Tricks and traps:
- Socket portability issues: How do SO_REUSEADDR and SO_REUSEPORT differ?;
- Awesome "Socket Programming HOWTO";
Traceback (most recent call last): File "brd_serv.py", line 150, in <module> clt_brd() File "brd_serv.py", line 23, in clt_brd client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP AttributeError: type object 'socket' has no attribute 'socket'
I'm getting this error. I actually put client.py and server.py in a same file and called client(). Then this is the output. What might be the issue?
`def ser_brd_name():
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
nam='BEN'
num=9999
#message = b'%s: %s' % (nam.encode('utf-8'), ip.encode('utf-8')
message = str.encode(nam)
while True:
server.sendto(message,('192.168.43.255',12345))
print("Broadcast")
time.sleep(1)
def clt_brd():
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)`
`