11.2.8 Debugging CGI scripts
Python Library Reference
Previous: 11.2.7 Testing your CGI
Up: 11.2 cgi
Next: 11.2.9 Common problems and
11.2.8 Debugging CGI scripts
First of all, check for trivial installation errors -- reading the
section above on installing your CGI script carefully can save you a
lot of time. If you wonder whether you have understood the
installation procedure correctly, try installing a copy of this module
file (cgi.py) as a CGI script. When invoked as a script, the file
will dump its environment and the contents of the form in HTML form.
Give it the right mode etc, and send it a request. If it's installed
in the standard cgi-bin directory, it should be possible to send it a
request by entering a URL into your browser of the form:
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
If this gives an error of type 404, the server cannot find the script
- perhaps you need to install it in a different directory. If it
gives another error (e.g. 500), there's an installation problem that
you should fix before trying to go any further. If you get a nicely
formatted listing of the environment and form content (in this
example, the fields should be listed as ``addr'' with value ``At Home''
and ``name'' with value ``Joe Blow''), the cgi.py script has been
installed correctly. If you follow the same procedure for your own
script, you should now be able to debug it.
The next step could be to call the cgi module's
test() function from your script: replace its main code
with the single statement
cgi.test()
This should produce the same results as those gotten from installing
the cgi.py file itself.
When an ordinary Python script raises an unhandled exception
(e.g. because of a typo in a module name, a file that can't be opened,
etc.), the Python interpreter prints a nice traceback and exits.
While the Python interpreter will still do this when your CGI script
raises an exception, most likely the traceback will end up in one of
the HTTP server's log file, or be discarded altogether.
Fortunately, once you have managed to get your script to execute
some code, it is easy to catch exceptions and cause a traceback
to be printed. The test() function below in this module is
an example. Here are the rules:
Import the traceback module before entering the try
... except statement
Assign sys.stderr to be sys.stdout
Make sure you finish printing the headers and the blank line
early
Wrap all remaining code in a try ... except
statement
In the except clause, call traceback.print_exc()
For example:
import sys
import traceback
print "Content-Type: text/html"
print
sys.stderr = sys.stdout
try:
...your code here...
except:
print "\n\n<PRE>"
traceback.print_exc()
Notes: The assignment to sys.stderr is needed because the
traceback prints to sys.stderr.
The print "\n\n<PRE>" statement is necessary to
disable the word wrapping in HTML.
If you suspect that there may be a problem in importing the traceback
module, you can use an even more robust approach (which only uses
built-in modules):
import sys
sys.stderr = sys.stdout
print "Content-Type: text/plain"
print
...your code here...
This relies on the Python interpreter to print the traceback. The
content type of the output is set to plain text, which disables all
HTML processing. If your script works, the raw HTML will be displayed
by your client. If it raises an exception, most likely after the
first two lines have been printed, a traceback will be displayed.
Because no HTML interpretation is going on, the traceback will
readable.
Python Library Reference
Previous: 11.2.7 Testing your CGI
Up: 11.2 cgi
Next: 11.2.9 Common problems and
See About this document... for information on suggesting changes.
Wyszukiwarka
Podobne podstrony:
node245 T2YQJZD6LDQ6LP5OYUC3OHQJMT5QYEUMQNSNPTInode245node243node242 7L22GLKYOY7WCD2ENRGH2MBHLMAUTJL2GTKKYNYnode246 YIF4WCYAY6SJS2OLKTPEMD6TGPZD4BLGOW43G3Anode24 PDHWHKXXAJCE53SGAZZQAGL4RLNJ7ZXWLGOY4QYnode24node24 3ZDS5WHDJGYZQJTUOOHV6YJOOXTC2GUWSJC775Inode24node244node243 YS2T6SICTFF6CFMWRO7X5VPFRR7EJ4VCN3TKXFYnode24 2PNVCNM2PFRUZA3ENONWT4NSN4DPLHTMPZ3FH7Ynode241node244 D3H4Q5QNKWCUAFHHJWADTRR7OS7G5JGYHSPAIMQnode249 7JYBNHKY3DEZ42ROO3VGHKZUJVB2IRUD4MHHCUYnode247node24 1node248 C33U2NRXWJ5BBSGXFIODC6MPK44O246Y7WN2G7Ywięcej podobnych podstron