From 67a51c93167a47313ca8c3d5073e90706ceed693 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 15 Apr 2024 16:07:30 +0200 Subject: [PATCH] [Web] Fix serve.py utility on Windows IPv6 dual stack is disabled by default, and Windows resolves wildcard addresses to an IPv6 by default, so connecting through the local IPv4 address would not work. This enables IPv6 dual stacking for the HTTP server by default like done in upstream python when launching the module from CLI. --- platform/web/serve.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/platform/web/serve.py b/platform/web/serve.py index 6a3efcc4637f..89dff63ca308 100755 --- a/platform/web/serve.py +++ b/platform/web/serve.py @@ -5,9 +5,20 @@ import os import sys import argparse +import contextlib +import socket import subprocess +# See cpython GH-17851 and GH-17864. +class DualStackServer(HTTPServer): + def server_bind(self): + # Suppress exception when protocol is IPv4. + with contextlib.suppress(Exception): + self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) + return super().server_bind() + + class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header("Cross-Origin-Opener-Policy", "same-origin") @@ -32,7 +43,7 @@ def serve(root, port, run_browser): print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).") shell_open(f"http://127.0.0.1:{port}") - test(CORSRequestHandler, HTTPServer, port=port) + test(CORSRequestHandler, DualStackServer, port=port) if __name__ == "__main__":