Skip to content Skip to sidebar Skip to footer

Debugging Cgi Python

Is there any easy way to debug cgi python programs apart from looking at the log file each time the browser generates an error?

Solution 1:

You can use the cgitb module. It's as simple as

import cgitb
cgitb.enable()

It doesn't always work (e.g. it won't help you for permission errors, or certain other kinds of errors), but when it does it's quite helpful!

Solution 2:

You could capture (or form by hand) the data and env variables which the CGI script receives, then plainly run the script under your favorite debugger and feed the data to it.

In order to capture the incoming data you can just dump it from the script in CGI mode to some log file, then re-use under debugger in standalone mode.

Solution 3:

If you want to have an interactive debugging session where you can set breakpoints, examine variables, etc. set the environment variable QUERY_STRING to the desired value before calling cgi.FieldStorage(), and then execute the file as an ordinary script using your favorite debugger.

This shows the general idea:

>>> import cgi
>>> import os
>>> os.environ['QUERY_STRING'] = 'name=Bob&city=Toronto&favorite=chocolate'>>> args = cgi.FieldStorage()
>>> args.keys()
['name', 'city', 'favorite']

Post a Comment for "Debugging Cgi Python"