Fallback to IPv4 when IPv6 is unsupported when using the default address (::1)

This commit is contained in:
Firepup Sixfifty 2024-07-31 21:47:54 +00:00
parent aaeb313181
commit a44e7c29fd
Signed by: Firepup650
SSH key fingerprint: SHA256:U0Zp8EhEe3CMqFSrC79CqatzaEiL4sjta80/RSX2XrY

View file

@ -603,6 +603,8 @@ async def connectServer(hostname: str, port: int):
async def runServer(address: str, port: int):
global G
sock = None
try:
if ":" in address:
INET = socket.AF_INET6
else:
@ -610,6 +612,12 @@ async def runServer(address: str, port: int):
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"))