servethis: simple http server

Posted on March 18th, 2008 in Uncategorized by sam

This post is actually mainly a test for the google-syntax-highlighter WordPress plugin, but it also gives me a chance to share this small script.

EDIT: {This looks bland now, because I got rid of the aforementioned plugin. Looked great, but I don’t like that it’s all JavaScript (ends up showing the code unmodified for 2-3 seconds while the page loads). It needs to run when the page is generated server-side.}

Found a python one-liner a while back to serve the current directory, but needed the ability to specify what port to use (the one-liner used 8100). I added that, and came up with this. Just call it from any directory to serve the contents up in a quick-and-dirty HTTP server. It defaults to port 8100, or you can specify the port by doing servethis [PORT]

#!/usr/bin/python

import sys
import SimpleHTTPServer
import SocketServer

# minimal web server.  serves files relative to the
# current directory.

PORT = 8100

if len(sys.argv) == 2:
        PORT = int(sys.argv[1])

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

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

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