faqts : Computers : Programming : Languages : Python : Common Problems : Debugging

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes

Entry

Is there anyway I can get the traceback on to the web page so I know what's happening?

Jul 2nd, 2000 21:58
Chuck Esterbrook, Dan Gindikin, unknown unknown, http://webware.sourceforge.net


The easiest way is to split your CGI module in two parts; use the 
following script as a wrapper, and place the program logic in a 
separate 
script ("my-script.main()" in this case):
#!/usr/bin/env python
import cgi, StringIO, sys, traceback
try:
    import myscript
    myscript.main()
except:
    print "Content-Type:", "text/html"
    print
    file = StringIO.StringIO()
    traceback.print_exc(file=file)
    print "<pre>"
    print cgi.escape(file.getvalue())
    print "</pre>"
If there is an error while running the program it will be displayed.
If the traceback does not appear in the result, then chances are you
have a syntax error in your code.  Set a debug mode that allows you to
run the script locally without calling cgi.FieldStorage() and related
stuff, and this should help you debug these errors.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You can grab a rather comprehensive CGI wrapper from
http://webware.sourceforge.net/Webware/CGIWrapper/Documentation/CGIWrapp
er.html which features additional support such as e-mailing errors to 
the site administrator and saving the error information to a log file.