Skip to content

Instantly share code, notes, and snippets.

@quietcricket
Last active January 6, 2025 03:34
Show Gist options
  • Save quietcricket/2cd91d15c8cb3d2d11fb640302d3c4b3 to your computer and use it in GitHub Desktop.
Save quietcricket/2cd91d15c8cb3d2d11fb640302d3c4b3 to your computer and use it in GitHub Desktop.
Work around with the default python -m http.server limitations: 1. some file types are not sent (e.g. excel, word) 2. Caching is not needed
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
import socketserver
PORT = 8080
class MyHandler(SimpleHTTPRequestHandler):
def __init__(self, request, client_address, server, *, directory=None):
super().__init__(request, client_address, server, directory=directory)
self.extensions_map = {
".html": "text/html",
".png": "image/png",
".jpg": "image/jpg",
".svg": "image/svg+xml",
".css": "text/css",
".js": "application/x-javascript",
"": "application/octet-stream", # Default
}
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
SimpleHTTPRequestHandler.end_headers(self)
httpd = socketserver.TCPServer(("", PORT), MyHandler)
print("serving at port", PORT)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment