[Tutor] debugging CGI hints

Paul Sidorsky paulsid@shaw.ca
Wed, 06 Mar 2002 17:12:08 -0700


Tim Wilson wrote:

> 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?

CGI debugging is touchy - check the section on the cgi module in the
library reference if you haven't already, there's some tips in there.

Off the top of my head I can suggest these:

- Check the path to python on the #! line at the start.  If you're
uploading the scripts (esp. from a Windows system) make sure you do it
in text mode, otherwise the path might have an extra CR on the end which
will cause it to fail.

- Put the initial print "Content-Type..." lines ASAP, ideally right at
the start before anything else including imports.    Sometimes this will
let you get a traceback displayed in the browser, which can be immensely
helpful.  

- Also make sure there's a blank line printed after the
"Content-Type..." line.  I think Netscape will choke if you don't have
this.

- If possible, run the script from the command line (i.e. non-CGI)
first.

- Use cgi.test() to make sure your server is setup right.  (Or use
another known-to-work CGI script.)

Hope that helps - all of these have helped me fix CGI problems in the
past.  I haven't done all that much CGI work, though, so I'm sure other
people will have better ideas.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/