Fix listen_on with IPv6 address

This commit is contained in:
Kovid Goyal 2024-08-27 18:42:51 +05:30
parent fdc3c3d7c1
commit d363513884
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 5 deletions

View file

@ -173,10 +173,9 @@ def listen_on(spec: str) -> tuple[int, str]:
atexit.register(remove_socket_file, s, socket_path)
s.bind(address)
s.listen()
if isinstance(address, tuple):
h, resolved_port = s.getsockname()
sfamily, host, port = spec.split(':', 2)
spec = f'{sfamily}:{host}:{resolved_port}'
if isinstance(address, tuple): # tcp socket
h, resolved_port = s.getsockname()[:2]
spec = spec.rpartition(':')[0] + f':{resolved_port}'
return s.fileno(), spec

View file

@ -492,7 +492,14 @@ def parse_address_spec(spec: str) -> tuple[AddressFamily, Union[tuple[str, int],
socket_path = address
elif protocol in ('tcp', 'tcp6'):
family = socket.AF_INET if protocol == 'tcp' else socket.AF_INET6
host, port = rest.rsplit(':', 1)
if rest.startswith('['): # ]
host = rest[1:]
host, sep, leftover = host.rpartition(']')
_, port = leftover.rsplit(':', 1)
if ':' in host and protocol == 'tcp':
family = socket.AF_INET6
else:
host, port = rest.rsplit(':', 1)
address = host, int(port)
else:
raise ValueError(f'Unknown protocol in listen-on value: {spec}')