6161_FATAL_ERROR_IGNORE = (BrokenPipeError ,
6262 ConnectionResetError , ConnectionAbortedError )
6363
64+ _HAS_IPv6 = hasattr (socket , 'AF_INET6' )
65+
6466
6567def _format_handle (handle ):
6668 cb = handle ._callback
@@ -123,7 +125,7 @@ def _ipaddr_info(host, port, family, type, proto):
123125
124126 if family == socket .AF_UNSPEC :
125127 afs = [socket .AF_INET ]
126- if hasattr ( socket , 'AF_INET6' ) :
128+ if _HAS_IPv6 :
127129 afs .append (socket .AF_INET6 )
128130 else :
129131 afs = [family ]
@@ -139,7 +141,10 @@ def _ipaddr_info(host, port, family, type, proto):
139141 try :
140142 socket .inet_pton (af , host )
141143 # The host has already been resolved.
142- return af , type , proto , '' , (host , port )
144+ if _HAS_IPv6 and af == socket .AF_INET6 :
145+ return af , type , proto , '' , (host , port , 0 , 0 )
146+ else :
147+ return af , type , proto , '' , (host , port )
143148 except OSError :
144149 pass
145150
@@ -1309,7 +1314,6 @@ async def create_server(
13091314 raise ValueError (
13101315 'host/port and sock can not be specified at the same time' )
13111316
1312- AF_INET6 = getattr (socket , 'AF_INET6' , 0 )
13131317 if reuse_address is None :
13141318 reuse_address = os .name == 'posix' and sys .platform != 'cygwin'
13151319 sockets = []
@@ -1349,7 +1353,9 @@ async def create_server(
13491353 # Disable IPv4/IPv6 dual stack support (enabled by
13501354 # default on Linux) which makes a single socket
13511355 # listen on both address families.
1352- if af == AF_INET6 and hasattr (socket , 'IPPROTO_IPV6' ):
1356+ if (_HAS_IPv6 and
1357+ af == socket .AF_INET6 and
1358+ hasattr (socket , 'IPPROTO_IPV6' )):
13531359 sock .setsockopt (socket .IPPROTO_IPV6 ,
13541360 socket .IPV6_V6ONLY ,
13551361 True )
0 commit comments