Only attempt to set IPV6_V6ONLY for IPv6 sockets

This commit is contained in:
Juhani Haverinen 2016-07-12 17:16:21 +03:00
parent 720f69e888
commit 4b35a63562
1 changed files with 6 additions and 4 deletions

View File

@ -36,10 +36,12 @@ def bind(port, backlog = 1):
s = socket.socket(af, socktype, proto)
except OSError:
continue
try:
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
except OSError:
pass
# 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
try:
s.bind(sa)
s.listen(backlog)