and server (web server) are just different processes running at the same time on the
same computer.
Though not meant for enterprise-level work, this turns out to be a great way to test
CGI scripts—you can develop them on the same machine without having to transfer
code back to a remote server machine after each change. Simply run this script from
the directory that contains both your HTML files and a cgi-bin subdirectory for scripts
and then use http://localhost/... in your browser to access your HTML and script files.
Here is the trace output the web server script produces in a Windows console window
that is running on the same machine as the web browser and launched from the direc-
tory where the HTML files reside:
...\PP4E\Preview> python webserver.py
mark-VAIO - - [28/Jan/2010 18:34:01] "GET /cgi101.html HTTP/1.1" 200 -
mark-VAIO - - [28/Jan/2010 18:34:12] "POST /cgi-bin/cgi101.py HTTP/1.1" 200 -
mark-VAIO - - [28/Jan/2010 18:34:12] command: C:\Python31\python.exe -u C:\Users
\mark\Stuff\Books\4E\PP4E\dev\Examples\PP4E\Preview\cgi-bin\cgi101.py ""
mark-VAIO - - [28/Jan/2010 18:34:13] CGI script exited OK
mark-VAIO - - [28/Jan/2010 18:35:25] "GET /cgi-bin/cgi101.py?user=Sue+Smith HTTP
/1.1" 200 -
mark-VAIO - - [28/Jan/2010 18:35:25] command: C:\Python31\python.exe -u C:\Users
\mark\Stuff\Books\4E\PP4E\dev\Examples\PP4E\Preview\cgi-bin\cgi101.py
mark-VAIO - - [28/Jan/2010 18:35:26] CGI script exited OK
One pragmatic note here: you may need administrator privileges in order to run a server
on the script’s default port 80 on some platforms: either find out how to run this way
or try running on a different port. To run this server on a different port, change the port
number in the script and name it explicitly in the URL (e.g., http://localhost:8888/)..)
We’ll learn more about this convention later in this book.
And to run this server on a remote computer, upload the HTML files and CGI scripts
subdirectory to the remote computer, launch the server script on that machine, and
replace “localhost” in the URLs with the domain name or IP address of your server
machine (e.g., http://www.myserver.com/)..) When running the server remotely, all the
interaction will be as shown here, but inputs and replies will be automatically shipped
across network connections, not routed between programs running on the same
computer.
To delve further into the server classes our web server script employs, see their imple-
mentation in Python’s standard library (C:\Python31\Lib for Python 3.1); one of the
major advantages of open source system like Python is that we can always look under
the hood this way. In Chapter 15, we’ll expand Example 1-32 to allow the directory
name and port numbers to be passed in on the command line.
Using Query Strings and urllib
In the basic CGI example shown earlier, we ran the Python script by filling out and
submitting a form that contained the name of the script. Really, server-side CGI scripts
can be invoked in a variety of ways—either by submitting an input form as shown so
Step 6: Adding a Web Interface | 57