Created
April 16, 2025 06:08
-
-
Save sefgit/6eed95f75522ceba5b25b65401df7470 to your computer and use it in GitHub Desktop.
Simple python web server
This file contains hidden or 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
from http.server import HTTPServer, SimpleHTTPRequestHandler | |
import ssl | |
import logging | |
from os import path | |
from urllib.parse import urlparse | |
SECURE = False | |
PORT = 8000 | |
class Handler(SimpleHTTPRequestHandler): | |
# Define the custom MIME types | |
custom_mime_types = { | |
".wgsl": "text/javascript", | |
".crx": "application/x-chrome-extension", | |
# Add more custom MIME types here as needed | |
} | |
def guess_type(self, path): | |
url = urlparse(path) | |
file_ext = url.path | |
pos = file_ext.rfind('.') | |
if pos != -1: | |
file_ext = file_ext[pos:] | |
else: | |
file_ext = "" | |
# Check if the file extension has a custom MIME type | |
if file_ext in self.custom_mime_types: | |
return self.custom_mime_types[file_ext] | |
return super().guess_type(path) | |
def end_headers(self) -> None: | |
#self.send_header('dummy-header', 'dummy-header-value') | |
self.send_header('Access-Control-Allow-Origin', '*') | |
super().end_headers() | |
def do_GET(self): | |
logging.error(self.headers) | |
super().do_GET() | |
def do_POST(self): | |
logging.error(self.headers) | |
super().do_GET() | |
def do_OPTIONS(self): | |
logging.error(self.headers) | |
self.send_response(200) | |
self.end_headers() | |
if SECURE: | |
folder = path.dirname(__file__) | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
# https://github.com/FiloSottile/mkcert | |
context.load_cert_chain( | |
certfile=f"{folder}/localhost+2.pem", | |
keyfile=f"{folder}/localhost+2-key.pem" | |
) | |
context.check_hostname = False | |
with HTTPServer(("", PORT), Handler) as httpd: | |
httpd.socket = context.wrap_socket(httpd.socket, server_side=True) | |
httpd.serve_forever() | |
else: | |
with HTTPServer(("", PORT), Handler) as httpd: | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment