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
That last bit should have been
why os.environ can not be passed to child ? A python CGI program can run on linux but not on windows. By looking at CGIHTTPServer.py, we found out the reason: on windows, the parent cannot pass os.environ to child demonstrated as below:
it will print failure
main.py is adopted from CGIHTTPServer.py ?!