#!/usr/bin/env python3 """ Simple authenticated web server for LangMem documentation """ import http.server import socketserver import base64 import os from urllib.parse import parse_qs # Authentication credentials USERNAME = "langmem" PASSWORD = "langmem2025" class AuthHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): """HTTP handler with basic authentication""" def __init__(self, *args, **kwargs): super().__init__(*args, directory="docs", **kwargs) def do_GET(self): """Handle GET requests with authentication""" if self.authenticate(): super().do_GET() else: self.send_auth_request() def authenticate(self): """Check if request has valid authentication""" auth_header = self.headers.get('Authorization') if not auth_header: return False try: auth_type, credentials = auth_header.split(' ', 1) if auth_type.lower() != 'basic': return False decoded = base64.b64decode(credentials).decode('utf-8') username, password = decoded.split(':', 1) return username == USERNAME and password == PASSWORD except Exception: return False def send_auth_request(self): """Send authentication request to client""" self.send_response(401) self.send_header('WWW-Authenticate', 'Basic realm="LangMem Documentation"') self.send_header('Content-type', 'text/html') self.end_headers() html = """ Authentication Required

šŸ” LangMem Documentation

Authentication required to access documentation.

Username: langmem
Password: langmem2025

Please refresh the page and enter your credentials when prompted.

""" self.wfile.write(html.encode()) def start_server(port=8080): """Start the authenticated documentation server""" try: with socketserver.TCPServer(("", port), AuthHTTPRequestHandler) as httpd: print(f"🌐 LangMem Documentation Server started!") print(f"šŸ“ URL: http://localhost:{port}") print(f"šŸ”‘ Username: {USERNAME}") print(f"šŸ”‘ Password: {PASSWORD}") print(f"šŸ›‘ Press Ctrl+C to stop the server") print("=" * 50) httpd.serve_forever() except KeyboardInterrupt: print("\nšŸ›‘ Server stopped by user") except Exception as e: print(f"āŒ Error starting server: {e}") if __name__ == "__main__": import sys port = 8080 if len(sys.argv) > 1: try: port = int(sys.argv[1]) except ValueError: print("Invalid port number. Using default port 8080.") start_server(port)