[Chicago] How do you troubleshoot cgi errors?

Ian Bicking ianb at colorstudy.com
Mon Jul 23 16:29:35 CEST 2007

David Rock wrote:
> This one is driving me nuts.  I have a simple cgi interface that I
> occasionally get a "CGI Error - Bad content header" message from.   It's
> particularly annoying because the problem is not easily or consistently
> repeatable.  I know that what's actually happening is the html being
> sent is angering the html gods, but the browser contents tell me nothing
> about what the actual text attempting to be processed looks like. 
> I am already trying to use cgitb to catch what's happening, to no avail.
> That _does_ work if a real traceback error crops up, but this appears to
> be something different.  It almost looks like the script itself is not
> running properly from time to time, creating a partial page.
> Does anyone have any ideas about how to monitor what is actually being
> generated and sent to the browser?  I'm almost to the point where I am
> considering creating duplicate print statements to an external log for
> everything I am printing, but that seems crazy to have to go that far.
> Isn't there any way to debug/monitor cgi output?

You can do something like this:

class ReplStdOut(object):
     def __init__(self, *files):
         self.files = files
     def __getattr__(self, attr):
         def repl_method(*args, **kw):
             for f in self.files:
                 value = getattr(f, attr)(*args, **kw)
             return value
         return repl_method

sys.stdout = ReplStdout(sys.stdout, open('output.log', 'a'))

You might not want to append it all to the same file, but something like 
that will work.

Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org
             : Write code, do good : http://topp.openplans.org/careers

More information about the Chicago mailing list