From a44e7c29fd2d994bfed35981ea0f3338ecb5ec92 Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Wed, 31 Jul 2024 21:47:54 +0000 Subject: [PATCH] Fallback to IPv4 when IPv6 is unsupported when using the default address (::1) --- server.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 2c07451..8c4dc00 100755 --- a/server.py +++ b/server.py @@ -603,13 +603,21 @@ async def connectServer(hostname: str, port: int): async def runServer(address: str, port: int): global G - if ":" in address: - INET = socket.AF_INET6 - else: - INET = socket.AF_INET - sock = socket.socket(INET, socket.SOCK_STREAM) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket - sock.bind((address, port)) + sock = None + try: + if ":" in address: + INET = socket.AF_INET6 + else: + INET = socket.AF_INET + sock = socket.socket(INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket + sock.bind((address, port)) + except OSError as E: + if E.errno != 97 or hostname != '::1': + raise E + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Reuse socket + sock.bind(('0.0.0.0', port)) server = await asyncio.start_server(handleClient, sock=sock) log(f"Listening on port {port}...") G.msgs.append(log("Server startup"))