Friday, May 25, 2012

Quick Python web server on localhost

Python's SimpleHTTPServer is great for serving files from a directory without the hassle of setting us a proper web server.

$ python -m SimpleHTTPServer

But by default it listens on all network interfaces. If you only want to listen on localhost then you'll need to write a short script.

import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()

Adapted from an article at Linux Journal.

No comments:

Post a Comment