home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Debugging CGI scripts -- Python library reference</TITLE>
- Next: <A HREF="../c/common_problems_and_solutions" TYPE="Next">Common problems and solutions</A>
- Prev: <A HREF="../t/testing_your_cgi_script" TYPE="Prev">Testing your CGI script</A>
- Up: <A HREF="../c/cgi" TYPE="Up">cgi</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>10.1.8. Debugging CGI scripts</H2>
- 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 (<CODE>cgi.py</CODE>) 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 <CODE>cgi-bin</CODE> directory, it should be possible to send it a
- request by entering a URL into your browser of the form:
- <P>
- <UL COMPACT><CODE> http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home<P>
- </CODE></UL>
- 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 <CODE>cgi.py</CODE> script has been
- installed correctly. If you follow the same procedure for your own
- script, you should now be able to debug it.
- <P>
- The next step could be to call the <CODE>cgi</CODE> module's test() function from
- your script: replace its main code with the single statement
- <P>
- <UL COMPACT><CODE> cgi.test()<P>
- </CODE></UL>
- This should produce the same results as those gotten from installing
- the <CODE>cgi.py</CODE> file itself.
- <P>
- 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.
- <P>
- 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 <CODE>test()</CODE> function below in this module is an example.
- Here are the rules:
- <P>
- <UL>
- <LI>1. Import the traceback module (before entering the
- try-except!)
- <P>
- <LI>2. Make sure you finish printing the headers and the blank
- line early
- <P>
- <LI>3. Assign <CODE>sys.stderr</CODE> to <CODE>sys.stdout</CODE>
- <P>
- <LI>4. Wrap all remaining code in a try-except statement
- <P>
- <LI>5. In the except clause, call <CODE>traceback.print_exc()</CODE>
- </UL>
- For example:
- <P>
- <UL COMPACT><CODE> import sys<P>
- import traceback<P>
- print "Content-type: text/html"<P>
- print<P>
- sys.stderr = sys.stdout<P>
- try:<P>
- ...your code here...<P>
- except:<P>
- print "\n\n<PRE>"<P>
- traceback.print_exc()<P>
- </CODE></UL>
- Notes: The assignment to <CODE>sys.stderr</CODE> is needed because the traceback
- prints to <CODE>sys.stderr</CODE>. The <CODE>print "nn<PRE>"</CODE> statement is necessary to
- disable the word wrapping in HTML.
- <P>
- 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):
- <P>
- <UL COMPACT><CODE> import sys<P>
- sys.stderr = sys.stdout<P>
- print "Content-type: text/plain"<P>
- print<P>
- ...your code here...<P>
- </CODE></UL>
- 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.
- <P>
-