1#!/usr/bin/env python3 2 3""" 4An extension of Python's simple HTTP server that sets the Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy 5HTTP headers to same-origin and require-corp respectively. 6 7Listens on localhost:8080 by default but you can override this by passing a command line arg, e.g.: 8 ./webserver.py localhost:9001 9""" 10 11from http.server import HTTPServer, SimpleHTTPRequestHandler 12import sys 13 14 15class Handler(SimpleHTTPRequestHandler): 16 def __init__(self, *args, **kwargs): 17 super().__init__(*args, **kwargs)
Overridden to set headers
27def run(host, port): 28 httpd = HTTPServer((host, port), Handler) 29 httpd.serve_forever() 30 31 32def main(): 33 server_address = "localhost:8080" 34 if len(sys.argv) > 1: 35 server_address = sys.argv[1] 36 37 (host, port) = server_address.split(":") 38 39 print(f"Starting webserver listening on http://{host}:{port}/") 40 41 run(host, int(port)) 42 43 44if __name__ == "__main__": 45 main()