httpd anywhere – A simple python script to quickly run a web server using any directory as the root
I develop a lot of little sites for fun. Sometimes I’m trying out an idea or I just want to test some CSS or Javascript, etc. In any case, I find it useful in testing to have a web server that can fire off from any directory on my machine.
Python just happens to have a basic HTTP server as part of the base language (see SimpleHTTPServer). There is also an extension of the module that runs CGI (see CGIHTTPServer). So, a quick script and you have an instant web server that:
- runs on port 8000 (by default)
- uses the current directory as the root htdocs
- can run CGI scripts
[Note: I'm not the first to think of using this built-in python function. I mean, that's what it's for, right? I'm just passing this along as a tip.]
If you place said script on your path (as I have), you can fire up a mini web server in an instant from anywhere.
Here is the script:
#!/usr/bin/env python # If necessary, replace above with your path to python import CGIHTTPServer CGIHTTPServer.test()
I called my script cgiserver for ease of use. Also, if you are new to shell scripting, be sure to make your shiny, new script executable:
chmod +x [script_name]
Below is a clip from my terminal window where I fired up the server and browsed http://localhost:8000. To close the server, just hit control-c to kill the script.
jgmbpro:~/Documents/Work/FGTP/site jamiegrove$ cgiserver Serving HTTP on 0.0.0.0 port 8000 ... localhost - - [15/Sep/2007 09:21:02] "GET / HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /styles/stylemain.css HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /styles/scal.css HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/prototype.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/scriptaculous.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/builder.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/effects.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/dragdrop.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/controls.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/slider.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:02] "GET /javascripts/sound.js HTTP/1.1" 200 - localhost - - [15/Sep/2007 09:21:03] "GET /javascripts/scal.js HTTP/1.1" 200 -










