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).

7 comments:

OOOps said...

Also if you need IPv6 support this page is useful:
http://www.righto.com/2011/02/ipv6-web-serving-with-arc-or-python.html

Swapnil Singh said...

Thank you very much!

harikasri.blogspot.com said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
Code fan said...

I have two comments:
- For python3 and on linux, #!/usr/bin/python3 is the correct text to put on the top of the program;
- That solution did not work for me, and when I look at Handler.extensions_map, I have a dictionary with a lot of extensions and mimetypes including svg. Then I don't understand the problem.

(I'm French, and my English level is bad)

Next said...

I have a dictionary with a lot of extensions and mimetypes including svg. Then I don't understand the problem.
I Gratis APK
Baixar My APK
I have a dictionary with a lot of extensions and mimetypes including svg. Then I don't understand the problem.