framework itself. With HTMLgen and similar packages, for instance, it’s possible to
deal in Python objects, not HTML syntax, though you must learn this system’s API as
well.
Running Server-Side Examples
Like GUIs, web-based systems are highly interactive, and the best way to get a feel for
some of these examples is to test-drive them live. Before we get into some code, let’s
get set up to run the examples we’re going to see.
Running CGI-based programs requires three pieces of software:
- The client, to submit requests: a browser or script
- The web server that receives the request
- The CGI script, which is run by the server to process the request
We’ll be writing CGI scripts as we move along, and any web browser can be used as a
client (e.g., Firefox, Safari, Chrome, or Internet Explorer). As we’ll see later, Python’s
urllib.request module can also serve as a web client in scripts we write. The only
missing piece here is the intermediate web server.
Web Server Options
There are a variety of approaches to running web servers. For example, the open source
Apache system provides a complete, production-grade web server, and its mod_python
extension discussed later runs Python scripts quickly. Provided you are willing to install
and configure it, it is a complete solution, which you can run on a machine of your
own. Apache usage is beyond our present scope here, though.
If you have access to an account on a web server machine that runs Python 3.X, you
can also install the HTML and script files we’ll see there. For the second edition of this
book, for instance, all the web examples were uploaded to an account I had on the
“starship” Python server, and were accessed with URLs of this form:
http://starship.python.net/~lutz/PyInternetDemos.html
If you go this route, replace starship.python.net/~lutz with the names of your own
server and account directory path. The downside of using a remote server account is
that changing code is more involved—you will have to either work on the server ma-
chine itself or transfer code back and forth on changes. Moreover, you need access to
such a server in the first place, and server configuration details can vary widely. On the
starship machine, for example, Python CGI scripts were required to have a .cgi filename
extension, executable permission, and the Unix #! line at the top to point the shell to
Python.
Finding a server that supports Python 3.X used by this book’s examples might prove a
stumbling block for some time to come as well; neither of my own ISPs had it installed
1130 | Chapter 15: Server-Side Scripting