#!/usr/bin/env python3 """Serve the built site (public/) on all interfaces so other machines on the same LAN can reach it. Usage: python3 serve_lan.py [port] (default 8090) The site must be built first: make build index Then start this: python3 serve_lan.py Address to open from ANOTHER machine on the same network: http://:8090/ where the LAN IP is printed at startup (and is also hostname -I | head -1). """ import functools import http.server import socket import subprocess import sys PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8090 # Serve the built site (public/), NOT the repo root. handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory="public") server = http.server.ThreadingHTTPServer(("0.0.0.0", PORT), handler) def lan_ip() -> str: try: return subprocess.check_output(["hostname", "-I"], text=True).split()[0] except Exception: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(("8.8.8.8", 80)) return s.getsockname()[0] finally: s.close() ip = lan_ip() print(f"Serving public/ -> http://0.0.0.0:{PORT}/", flush=True) print(f"LAN address -> http://{ip}:{PORT}/ (open this from another machine)", flush=True) print("Press Ctrl+C to stop.", flush=True) try: server.serve_forever() except KeyboardInterrupt: pass finally: server.server_close()