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()
  • Digg
  • del.icio.us
  • blogmarks
  • Fark
  • Reddit
  • Slashdot
  • Technorati
  • Propeller
  • StumbleUpon

5 Responses to 'servethis: simple http server'

Subscribe to comments with RSS or TrackBack to 'servethis: simple http server'.

  1. james lorenzen said,

    on March 18th, 2008 at 11:19 am

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

  2. Kit Plummer said,

    on March 18th, 2008 at 12:18 pm

    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( :Port => 3000, :DocumentRoot => 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 said,

    on March 18th, 2008 at 12:25 pm

    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 said,

    on March 26th, 2008 at 9:30 pm

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

  5. wife said,

    on April 3rd, 2008 at 7:55 pm

    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( :PortARTHUR => 666, :JimBobSonOf => Fish.glub(UR.pwoned, :) :)
    rap(”StopCollaborateListen”) { get.jiggywitit }
    ice.baby

Post a comment