Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fbeneventi/566275479cb844dd5b8b362b7df09c02 to your computer and use it in GitHub Desktop.
Save fbeneventi/566275479cb844dd5b8b362b7df09c02 to your computer and use it in GitHub Desktop.
Simple Python https server example py 3.10+
import http.server
import socket
import ssl
import os
server_address = ('0.0.0.0', 9000)
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
print("Open https://localhost:9000")
print('Open https://' + local_ip + ':9000')
#os.chdir("./dist/")
class CORSHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
extensions_map = {
'': 'application/octet-stream',
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'.wasm': 'application/wasm',
'.json': 'application/json',
'.xml': 'application/xml',
}
def end_headers(self):
# Include additional response headers here. CORS for example:
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
http.server.SimpleHTTPRequestHandler.end_headers(self)
httpd = http.server.HTTPServer(server_address, CORSHTTPRequestHandler)
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.check_hostname = False
ctx.load_cert_chain(certfile='localhost.pem') # with key inside
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment