Last active
November 20, 2024 23:23
-
-
Save Alexufo/2303bff77f0a16ba83568f0260b8cf47 to your computer and use it in GitHub Desktop.
Simple Python https server example py 3.10+ ( multithread, wasm, gzip )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
import http.cookiejar | |
import io | |
import socket | |
from http import HTTPStatus | |
import ssl | |
import os | |
import zlib | |
server_address = ('0.0.0.0', 4443) | |
hostname = socket.gethostname() | |
local_ip = socket.gethostbyname(hostname) | |
print("Open https://localhost:4443/samples/idengine_sample_wasm/sample.html") | |
print(f'Open https://{local_ip}:4443/samples/idengine_sample_wasm/sample.html') | |
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 do_GET(self): | |
"""Serve a GET request.""" | |
if f := self.send_head(): | |
try: | |
if hasattr(f, "read"): | |
self.copyfile(f, self.wfile) | |
else: | |
for data in f: | |
self.wfile.write(data) | |
finally: | |
f.close() | |
def send_head(self): | |
path = self.translate_path(self.path) | |
f = None | |
try: | |
f = open(path, 'rb') | |
except OSError: | |
self.send_error(HTTPStatus.NOT_FOUND, "File not found") | |
return None | |
ctype = self.guess_type(path) | |
try: | |
fs = os.fstat(f.fileno()) | |
content_length = fs[6] | |
self.send_response(HTTPStatus.OK) | |
self.send_header("Content-type", ctype) | |
self.send_header("Content-Length", str(content_length)) | |
self.end_headers() | |
return f | |
except: | |
f.close() | |
raise | |
def end_headers(self): | |
# Include additional response headers here. CORS for example: | |
self.send_header('Access-Control-Allow-Origin', '*') | |
http.server.SimpleHTTPRequestHandler.end_headers(self) | |
httpd = http.server.ThreadingHTTPServer(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
This one a well as prev gist lacks ssl handshake timeouts, thus httpd.timeout = 123 will not work
to fix:
you need to subclass HTTPServer: