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";
@stevenreddie thanks for your contribution!
I did a tiny bit of R&D and found this thread about socket tripwires, so I applied
SO_REUSEPORT
flag.SO_REUSEADDR
doesn't allow to use a shared port on *BSD systems (Mac OS including). Also, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port) because for linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID.