The standard library modules SimpleHTTPServer and CGIHTTPServer are extremely useful, but the documentation hides their virtues. I hope to improve the situation with this recipe.
1 2 3 4 5
$ python -m SimpleHTTPServer # requires Python 2.4 or $ python -c "from SimpleHTTPServer import test; test()" # older Python
If you want to serve files from the current directory, just give the previous command on the shell prompt and open a browser at http://localhost:8000.
If you have a cgi script located in /cgi-bin/myscript.py, you can test it by giving
$ cd $ python -m CGIHTTPServer
and going at http://localhost:8000/myscript.py
Notice that this will work for non-Python CGI scripts too!
The advantage of using CGIHTTPServer is that you will get the logs and the error messages in a shell window and not in a log file. This is of invaluable help during debugging.
You can use a port different from port 8000, just give
$ python -m