Do SO_REUSEADDR

This commit is contained in:
Juhani Haverinen 2016-08-03 01:19:08 +03:00
parent 11754b56e3
commit 3adb47efde
1 changed files with 5 additions and 1 deletions

View File

@ -62,12 +62,17 @@ def bind(port, backlog = 1):
s = socket.socket(af, socktype, proto)
except OSError:
continue
# Make IPv6 socket only bind on IPv6 address, otherwise may clash with IPv4 and not get enabled
if af == socket.AF_INET6:
try:
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
except OSError:
pass
# Set SO_REUSEADDR for less painful server restarting
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(sa)
s.listen(backlog)
@ -79,7 +84,6 @@ def bind(port, backlog = 1):
return sockets
# drop_privileges()
# Drops set[ug]id, die()s if unsuccesful
def drop_privileges():