5454_FATAL_ERROR_IGNORE = (BrokenPipeError ,
5555 ConnectionResetError , ConnectionAbortedError )
5656
57+ _HAS_IPv6 = hasattr (socket , 'AF_INET6' )
58+
5759
5860def _format_handle (handle ):
5961 cb = handle ._callback
@@ -136,7 +138,7 @@ def _ipaddr_info(host, port, family, type, proto):
136138
137139 if family == socket .AF_UNSPEC :
138140 afs = [socket .AF_INET ]
139- if hasattr ( socket , 'AF_INET6' ) :
141+ if _HAS_IPv6 :
140142 afs .append (socket .AF_INET6 )
141143 else :
142144 afs = [family ]
@@ -152,7 +154,10 @@ def _ipaddr_info(host, port, family, type, proto):
152154 try :
153155 socket .inet_pton (af , host )
154156 # The host has already been resolved.
155- return af , type , proto , '' , (host , port )
157+ if _HAS_IPv6 and af == socket .AF_INET6 :
158+ return af , type , proto , '' , (host , port , 0 , 0 )
159+ else :
160+ return af , type , proto , '' , (host , port )
156161 except OSError :
157162 pass
158163
@@ -997,7 +1002,6 @@ def create_server(self, protocol_factory, host=None, port=None,
9971002 raise ValueError (
9981003 'host/port and sock can not be specified at the same time' )
9991004
1000- AF_INET6 = getattr (socket , 'AF_INET6' , 0 )
10011005 if reuse_address is None :
10021006 reuse_address = os .name == 'posix' and sys .platform != 'cygwin'
10031007 sockets = []
@@ -1037,7 +1041,9 @@ def create_server(self, protocol_factory, host=None, port=None,
10371041 # Disable IPv4/IPv6 dual stack support (enabled by
10381042 # default on Linux) which makes a single socket
10391043 # listen on both address families.
1040- if af == AF_INET6 and hasattr (socket , 'IPPROTO_IPV6' ):
1044+ if (_HAS_IPv6 and
1045+ af == socket .AF_INET6 and
1046+ hasattr (socket , 'IPPROTO_IPV6' )):
10411047 sock .setsockopt (socket .IPPROTO_IPV6 ,
10421048 socket .IPV6_V6ONLY ,
10431049 True )
0 commit comments