Page 284 -
P. 284
track down cgi errors
Enable CGI tracking to help with errors
The CGI standard dictates that any output generated by a server-side
program (your CGI script) should be captured by the web server and sent
to the waiting web browser. Specifically, anything sent to STDOUT (standard
output) is captured.
When your CGI script raises an exception, Python arranges for the error
message to display on STDERR (standard error). The CGI mechanism is
programmed to ignore this output because all it wants is the CGI script’s
standard output.
When your CGI works, I’ll fill
your STDOUT with lovely HTML.
When your CGI fails, it’s a case
of—POOF!—gone for good. Sorry,
but that’s the way the CGI cookie
crumbles...
Web
Server
This behavior is fine when the webapp is deployed, but not when it’s being
developed. Wouldn’t it be useful to see the details of the exception in the
browser window, as opposed to constantly having to jump to the web server’s
logging screen?
Well…guess what? Python’s standard library comes with a CGI tracking
module (called cgitb) that, when enabled, arranges for detailed error
messages to appear in your web browser. These messages can help you work
out where your CGI has gone wrong. When you’ve fixed the error and your
CGI is working well, simply switch off CGI tracking:
Add these two lines near the
start of your CGI scripts to
import cgitb enable Python’s CGI tracking
cgitb.enable() technology.
248 Chapter 7