Skip to content

Instantly share code, notes, and snippets.

@scottj
Forked from HaiyangXu/Server.py
Last active February 9, 2023 14:25
Show Gist options
  • Save scottj/a510d6bd96941901fb99554566ee226d to your computer and use it in GitHub Desktop.
Save scottj/a510d6bd96941901fb99554566ee226d to your computer and use it in GitHub Desktop.
A simple Python HTTP server that can handle mime types properly.
#!/usr/bin/env python3
import http.server
import socketserver
HOST = "localhost"
PORT = 8000
class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
extensions_map = {".manifest": "text/cache-manifest"}
def version_string(self):
return "Apache/1.3.0 (Win32)"
try:
with socketserver.TCPServer((HOST, PORT), HttpRequestHandler) as httpd:
print(f"serving at http://{HOST}:{PORT}")
httpd.serve_forever()
except KeyboardInterrupt:
pass
@scottj
Copy link
Author

scottj commented May 18, 2022

Updated with suggestion from @jan25 here.

@scottj
Copy link
Author

scottj commented Feb 9, 2023

Since Python 3.9, the extensions_map parameter is used solely to override a vast list of default values with custom MIME types, mostly rendering this script superfluous. Redundant MIME types have been removed.

Additionally, I added an override for the Server header in this version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment