[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.
This commit is contained in:
Fabio Alessandrelli 2024-04-15 16:07:30 +02:00
parent c951421c99
commit 67a51c9316

View file

@ -5,9 +5,20 @@ from pathlib import Path
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__":