debugging CGI scripts
Adam Vandenberg
adamv at nwlink.com
Mon Aug 21 00:08:48 EDT 2000
lynx <a at b.c> wrote in message
news:pJ%n5.43052$QD5.426611 at news.corecomm.net...
> any good, simple work-even-remotely-alike for perl's CGI::Carp out there,
Here is something I use for debugging Python CGI scripts.
It's based on various other similar debugging scripts I found and glommed
together.
--- debug.py ---
#!/usr/bin/python
import os, sys, string
import cgi
filename = os.environ["PATH_TRANSLATED"]
# Grab the real stdout from the script we are trying to debug
import StringIO
real_stdout = sys.stdout
sys.stdout = StringIO.StringIO()
try:
execfile(filename)
real_stdout.write( sys.stdout.getvalue() )
except:
real_stdout.write ("Content-type: text/html\n\n")
real_stdout.write ("debugging: " + filename + "<br><br>")
import traceback
IOBuffer = StringIO.StringIO()
traceback.print_exc(file=IOBuffer)
HTML = IOBuffer.getvalue()
HTML = string.replace(HTML,"&","amp;")
HTML = string.replace(HTML,"<","<")
HTML = string.replace(HTML,">",">")
HTML = string.replace(HTML,"\n","<br>")
HTML = string.replace(HTML,"\t"," ")
real_stdout.write("<tt>")
real_stdout.write(HTML)
real_stdout.write("</tt>")
sys.exit(0)
I place this in my web's root folder and include it in the path.
Debug, send tracebacks to browser: http://adamv.com/debug.py/toy/test.py
Hope this helps,
Adam Vandenberg, Pythoneer 3rd class.
More information about the Python-list
mailing list