It’s possible to open some of the examples by clicking on their HTML file directly in
your system’s file explorer GUI. However, the CGI scripts ultimately invoked by some
of the example links must be run by a web server. If you click to browse such pages
directly, your browser will likely display the scripts’ source code, instead of running it.
To run scripts, too, be sure to open the HTML pages by typing their “localhost” URL
address into your browser’s address field.
Eventually, you probably will want to start using a more powerful web server, so we
will study additional CGI installation details later in this chapter. You may also wish
to review our prior exploration of custom server options in Chapter 12 (Apache and
mod_python are a popular option). Such details can be safely skipped or skimmed if
you will not be installing on another server right away. For now, we’ll run locally.
Viewing Server-Side Examples and Output
The source code of examples in this part of the book is listed in the text and included
in the book’s examples distribution package. In all cases, if you wish to view the source
code of an HTML file, or the HTML generated by a Python CGI script, you can also
simply select your browser’s View Source menu option while the corresponding web
page is displayed.
Keep in mind, though, that your browser’s View Source option lets you see the out-
put of a server-side script after it has run, but not the source code of the script itself.
There is no automatic way to view the Python source code of the CGI scripts themselves,
short of finding them in this book or in its examples distribution.
To address this issue, later in this chapter we’ll also write a CGI-based program
called getfile, which allows the source code of any file on this book’s website (HTML,
CGI script, and so on) to be downloaded and viewed. Simply type the desired file’s
name into a web page form referenced by the getfile.html link on the Internet demos
launcher page of Figure 15-1, or add it to the end of an explicitly typed URL as a
parameter like the following; replace tutor5.py at the end with the name of the script
whose code you wish to view, and omit the cgi-bin component at the end to view HTML
files instead:
http://localhost/cgi-bin/getfile.py?filename=cgi-bin\tutor5.py
In response, the server will ship back the text of the named file to your browser. This
process requires explicit interface steps, though, and much more knowledge of URLs
than we’ve gained thus far; to learn how and why this magic line works, let’s move on
to the next section.
1134 | Chapter 15: Server-Side Scripting