14.12.2020 python
Python - simple httpd with mime types
Most of the time, when doing webdev, the really simple http server from Python is enough. However, there are times when you need mime types to be recognized correctly.
This is where this gist comes in handy. Modify the dict with the extensions and mimes you need. Should be straight forward.
# -*- coding: utf-8 -*-
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from sys import argv
port = 80
if len(argv) == 2:
port = int(argv[1])
handler = SimpleHTTPRequestHandler
handler.extensions_map = {
'.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',
'': 'application/octet-stream'
}
try:
httpd = TCPServer(("dev.localhost", port), handler)
print(f"serving at port {port}")
httpd.serve_forever()
except KeyboardInterrupt:
print("Server stopped, good bye!")
comments
Characters: 0/1000
Come join the discussion and write something nice. You will have to confirm your comment by mail, so make sure it is legit and not a throwaway. Only the name part of it will be displayed, so don't worry about spam. If it does not show up after confirming it, it may be considered spam, but I curate them manually, so don't worry. Please read the privacy statement for more.