From d4f05c7dd5ede0364c1f69e8e854b9b4ef80f1a7 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Thu, 10 Oct 2024 22:55:22 +0100 Subject: [PATCH] Added support for ipv6 in sync client (Fixes #39) --- src/simple_websocket/ws.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/simple_websocket/ws.py b/src/simple_websocket/ws.py index 25d36f9..c78a585 100644 --- a/src/simple_websocket/ws.py +++ b/src/simple_websocket/ws.py @@ -397,13 +397,21 @@ def __init__(self, url, subprotocols=None, headers=None, elif isinstance(headers, list): self.extra_headeers = headers - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + connection_args = None + for conn in socket.getaddrinfo(self.host, self.port): # pragma: no branch + if conn[1] == socket.SOCK_STREAM: # pragma: no branch + connection_args = conn + break + if connection_args is None: # pragma: no cover + raise ConnectionError() + sock = socket.socket(connection_args[0], connection_args[1], + connection_args[2]) if is_secure: # pragma: no cover if ssl_context is None: ssl_context = ssl.create_default_context( purpose=ssl.Purpose.SERVER_AUTH) sock = ssl_context.wrap_socket(sock, server_hostname=self.host) - sock.connect((self.host, self.port)) + sock.connect(connection_args[4]) super().__init__(sock, connection_type=ConnectionType.CLIENT, receive_bytes=receive_bytes, ping_interval=ping_interval,