Skip to content

Instantly share code, notes, and snippets.

@HaiyangXu
Created May 18, 2014 14:00
Show Gist options
  • Save HaiyangXu/ec88cbdce3cdbac7b8d5 to your computer and use it in GitHub Desktop.
Save HaiyangXu/ec88cbdce3cdbac7b8d5 to your computer and use it in GitHub Desktop.
A simper python http server can handle mime type properly
# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver
PORT = 8080
Handler = http.server.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',
'': 'application/octet-stream', # Default
}
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
@HaiyangXu
Copy link
Author

Python 3 simple HTTP server with MIME type supported

@fbroussais
Copy link

Added
'.json': 'application/json',
'.xml': 'application/xml'
and runs perfectly for prototyping and mocking, thanks !

@myrdd
Copy link

myrdd commented Jun 8, 2018

Thanks a lot!

Note that the code should work with any version of python >= 3.0.

@EliasHasle
Copy link

EliasHasle commented Jul 24, 2019

I tried av variant of this approach, but it didn't work. I think I blame the python library, though. According to Chrome, the server serves imported ES6 modules (.js/.mjs) as "text/html" (causing the browser to refuse them) after updating the extensions_map (no need to replace the whole map) with entries for .js/.mjs.

@shellbit32
Copy link

saved my life

@kinka
Copy link

kinka commented Jan 21, 2020

wasm support: add '.wasm': 'application/wasm',

@asynts
Copy link

asynts commented Apr 29, 2020

There is a very subtle bug in this code:

Handler = http.server.SimpleHTTPRequestHandler

This creates a shallow copy, this means that it actually modifies http.server.SimpleHTTPRequestHandler, here a demonstration:

import http.server

Handler = http.server.SimpleHTTPRequestHandler
Handler.foo = 42

print(http.server.SimpleHTTPRequestHandler.foo) # 42

The correct way to do this is to inherit from http.server.SimpleHTTPRequestHandler:

import http.server

class Handler(http.server.SimpleHTTPRequestHandler):
    pass

Handler.foo = 42

print(http.server.SimpleHTTPRequestHandler.foo) # AttributeError

The modified script with some additional tweaks:

import http.server
import socketserver

PORT = 8080

class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    extensions_map = {
        '': 'application/octet-stream',
        '.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',
        '.json': 'application/json',
        '.xml': 'application/xml',
    }

httpd = socketserver.TCPServer(("localhost", PORT), HttpRequestHandler)

try:
    print(f"serving at http://localhost:{PORT}")
    httpd.serve_forever()
except KeyboardInterrupt:
    pass

@scottj
Copy link

scottj commented Sep 1, 2020

There is a very subtle bug in this code:

Nice updates. Thanks @asynts!

@jan25
Copy link

jan25 commented Jan 3, 2021

Thanks! Also, for cleanup of server i used context manager around httpd.

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

@3826
Copy link

3826 commented May 18, 2022

wasm support: add '.wasm': 'application/wasm',

yessss

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