From f01628ca6b1bde6d54d1b7c202d12c3545c18b5f Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 12 Dec 2024 09:44:53 +0100 Subject: [PATCH] fix localhost network tests on systems with IPv6 Thanks to themill from Debian for this suggestion: > https://docs.python.org/3/library/socket.html#socket.socket makes me > think socket.socket has done an ipv4 only but urilib3 will do > whatever localhost resolves to. I suspect the test code should be > using socket.create_server - there's an example at > https://docs.python.org/3/library/socket.html#socket.create_server --- tests/test_net.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_net.py b/tests/test_net.py index b8c311d3..5084474d 100755 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -37,9 +37,13 @@ class RetryServer: self.stop_event.set() def run_fake_server(self): - server_sock = socket.socket() - server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - server_sock.bind(('localhost', self.port)) + addr = ('localhost', self.port) + if socket.has_dualstack_ipv6(): + server_sock = socket.create_server( + addr, family=socket.AF_INET6, dualstack_ipv6=True + ) + else: + server_sock = socket.create_server(addr) server_sock.listen(5) server_sock.settimeout(5) time.sleep(0.001) # wait for it to start