display the exception’s name and value in the response page; exception details are
available in the built-in sys module, from sys.exc_info(). In addition, Python’s
traceback module can be used to manually generate stack traces on your reply page
for errors; tracebacks show source-code lines active when an exception occurred.
We’ll use this later in the error page in PyMailCGI (Chapter 16).
Add debugging prints
You can always insert tracing print statements in your code, just as in normal
Python programs. Be sure you print the content-type header line first, though, or
your prints may not show up on the reply page. In the worst case, you can also
generate debugging and trace messages by opening and writing to a local text file
on the server; provided you access that file later, this avoids having to format the
trace messages according to HTML reply stream conventions.
Run it live
Of course, once your script is at least half working, your best bet is likely to start
running it live on the server, with real inputs coming from a browser. Running a
server locally on your machine, as we’re doing in this chapter, can help by making
changes go faster as you test.
Adding Common Input Devices
So far, we’ve been typing inputs into text fields. HTML forms support a handful of
input controls (what we’d call widgets in the traditional GUI world) for collecting user
inputs. Let’s look at a CGI program that shows all the common input controls at once.
As usual, we define both an HTML file to lay out the form page and a Python CGI script
to process its inputs and generate a response. The HTML file is presented in
Example 15-11.
Example 15-11. PP4E\Internet\Web\tutor5a.html