[Tutor] debugging CGI hints

Sheila King sheila@thinkspot.net
Wed, 06 Mar 2002 15:43:38 -0800


On Wed, 6 Mar 2002 17:28:48 -0600 (CST), Tim Wilson <wilson@isis.visi.com>
wrote about [Tutor] debugging CGI hints:

> Hi everyone,
> 
> I've got apache configured to run Python-based CGI scripts on my home
> directory now. Thanks for the tips. Now that I'm working on some simple
> scripts, I'm finding the debugging to be difficult. Any problems with
> the scripts generate an "Internal Server Error" in the browser and I
> don't get any specific error messages.
> 
> Any tips for debugging CGI scripts?
> 
> -Tim

Here are a few. Mix and match to suit your preferences:

(1) Do you have access to the error logs? This should contain the error and
it is most likely a syntax error.

(2) Before running the script as a cgi, try running it from the command
line. The only difficulty with that, is that if your script requires
certain environment variables to be set, they will not be automatically set
when running from the command line, and you would have to take care to set
them. (I'm willing to go into more detail on this, if you like.) Anyhow,
running it from the command line definitely let's you see all the error
messages, and is usually much quicker than running it as a cgi and then
looking in the error logs.

(3) Try resetting stderr so that it outputs to stdout and then the error
messages will appear in your browser window. It is explained how to do
this, here:
http://www.python.org/doc/current/lib/node302.html

and I quote:

import sys
sys.stderr = sys.stdout
print "Content-Type: text/plain"
print
...your code here...


So, basically, you can add those four lines to the top of whatever code you
currently have, and the output should go to your browser window.

(4) If you are running Python 2.2, you could try the new cgitb module.
http://www.python.org/doc/current/lib/module-cgitb.html

I haven't tried it yet, but looks interesting.

(5) Write all the errors to an error log. (This is something that I do
frequently.) Basically I have a function like this near the top of my
script:

def logfileentry(exc, val, tb):
    errortime = str(asctime(localtime(time())))
    logfile = open(logfile, 'a')
    logfile.write('\n*** %s ***\n' % errortime)
    traceback.print_exception(exc, val, tb, file=logfile)
    logfile.write('\n')
    logfile.close()

( you will need to import asctime, localtime and time from the time module,
and import the traceback module )

Then, I have try/except blocks in my code:

try:
    <my code here>
except TypeOfError, errmsg:
    exc, val, tb = sys.exc_info()
    # append to the logfile
    logfileentry(exc, val, tb)
    # just to be sure there is no reference to the traceback
    del tb


So, this should give you several different approaches to try.

HTH,

-- 
Sheila King
http://www.thinkspot.net/sheila/

"When introducing your puppy to an adult cat,
restrain the puppy, not the cat." -- Gwen Bailey,
_The Perfect Puppy: How to Raise a Well-behaved Dog_