servethis: simple http server

March 18th, 2008 Leave a reply »

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

5 comments

  1. james lorenzen says:

    That’s pretty cool.
    Might come in handy. Thanks!

  2. Kit Plummer says:

    How ’bout this in Ruby:


    #!/usr/bin/env ruby
    require 'webrick'
    include WEBrick
    ### It is possible to include specific file extensions, within the ""
    s = HTTPServer.new( :P ort => 3000, :D ocumentRoot => File.join(Dir.pwd, ""))
    trap("INT") { s.shutdown }
    s.start

    Obviously, I didn’t take the Port as an arg…with a script like this there doesn’t seem to be any advantage. Just edit the script.

    James, where’s the Groovy version? ;)

  3. sam says:

    Well, as long as we’re doing simple versions, here’s the original one-liner I found. Just run it in your shell:

    python -c “import SimpleHTTPServer;SimpleHTTPServer.test()”
    :-D

  4. no name says:

    hahahhahahaha you dropped chicken crumbs *snicker* *points and laughs*

  5. wife says:

    How about this in Rudy?

    ^$/loser/gin/envelope prostitute
    acquire ‘yourmom’
    disclude NETdick
    AAA It is not probable to wear all hair extensions, outside the **
    q = FTPTENNISBALL.old( :P ortARTHUR => 666, :JimBobSonOf => Fish.glub(UR.pwoned, :) :)
    rap(“StopCollaborateListen”) { get.jiggywitit }
    ice.baby

Leave a Reply