Thursday, July 25, 2013

Python Simple HTTP Server with SVG image type

Although you can run Python Simple HTTP Server by just typing:

python -m SimpleHTTPServer 8000


It does not serve svg images with correct mime type. To add your own mime type for svg files, run it like this:

#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
import mimetypes

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

Handler.extensions_map['.svg']='image/svg+xml'

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()



Make sure to use correct the location of python (#!/usr/bin/python).