To learn CGI (Common Gateway Interface) programming in Python, you need access to a web server configured for handling Python CGI requests. For learning purposes or debugging you may run the web servers that come with Python.
If you want to just start up the most basic CGI web server, open a console window and change to the directory from which you want to run the server. You will need to have a sub-directory called cgi-bin within this directory. To run a simple test, copy the file hello.py into that directory. Then start Python as follows:
python -m CGIHTTPServerwhere the -m option (to run a library module as a script) is new in version 2.4. This should result in the following message:
C:\classes\python>python -m CGIHTTPServer Serving HTTP on 0.0.0.0 port 8000 ...Note that the console window is now running the server and is not available for further interaction.
Next activate the following link: http://localhost:8000/cgi-bin/hello.py Your browser window should look something like this:
The server console should have logged messages such as:
localhost - - [29/Jan/2008 20:13:54] "GET /hello.py HTTP/1.1" 404 - localhost - - [29/Jan/2008 20:14:17] "GET /cgi-bin/hello.py HTTP/1.1" 200 - localhost - - [29/Jan/2008 20:14:17] command: C:\Python25\python.exe -u C:\class es\python\cgi-bin\hello.py "" localhost - - [29/Jan/2008 20:14:17] CGI script exited OK
To exit the server, type a few control-C characters in the server console and perhaps send another request to the server so that the abort key messages are processed. Generally this will not be a graceful exit, but you should end up with a command prompt.
Maintained by John Loomis, last updated 29 January 2008