Skip to content

Instantly share code, notes, and snippets.

@rcowsill
Forked from HaiyangXu/Server.py
Last active January 30, 2024 21:46
Show Gist options
  • Save rcowsill/2590c8dcc469fe12ea3f710d57b970cc to your computer and use it in GitHub Desktop.
Save rcowsill/2590c8dcc469fe12ea3f710d57b970cc to your computer and use it in GitHub Desktop.
Minimal python http server (works on Windows)
import http.server
import socketserver
HOST = "localhost"
PORT = 8080
class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
extensions_map = {
".js":"text/javascript",
}
httpd = socketserver.TCPServer((HOST, PORT), HttpRequestHandler)
try:
print(f"serving at http://{HOST}:{PORT}")
httpd.serve_forever()
except KeyboardInterrupt:
pass
@rcowsill
Copy link
Author

rcowsill commented Jan 30, 2024

Minimal server for local development that works on Windows.

Based on original gist, updated with suggestions from @asynts (here), @jan25 (here) and @scottj (here)

The override is needed to ensure the correct MIME type is used when serving javascript. The default mapping comes from the registry, which says .js files are text/plain. See python/cpython#88141.

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