Skip to content

Instantly share code, notes, and snippets.

@razor-x
Last active June 18, 2024 14:27
Show Gist options
  • Save razor-x/9542707 to your computer and use it in GitHub Desktop.
Save razor-x/9542707 to your computer and use it in GitHub Desktop.
Python http.server that sets Access-Control-Allow-Origin header.
# Python http.server that sets Access-Control-Allow-Origin header.
# https://gist.github.com/razor-x/9542707
import os
import sys
import http.server
import socketserver
PORT = 8000
class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
http.server.SimpleHTTPRequestHandler.end_headers(self)
def server(port):
httpd = socketserver.TCPServer(('', port), HTTPRequestHandler)
return httpd
if __name__ == "__main__":
port = PORT
httpd = server(port)
try:
os.chdir('build')
print("\nserving from build/ at localhost:" + str(port))
httpd.serve_forever()
except KeyboardInterrupt:
print("\n...shutting down http server")
httpd.shutdown()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment