📄 node246.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>11.2.8 Debugging CGI scripts</title>
<META NAME="description" CONTENT="11.2.8 Debugging CGI scripts">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html">
<LINK REL="previous" HREF="node245.html" tppabs="http://www.python.org/doc/current/lib/node245.html">
<LINK REL="up" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">
<LINK REL="next" HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node245.html" tppabs="http://www.python.org/doc/current/lib/node245.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node245.html" tppabs="http://www.python.org/doc/current/lib/node245.html">11.2.7 Testing your CGI</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">11.2 cgi </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html">11.2.9 Common problems and</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION0013280000000000000000">
11.2.8 Debugging CGI scripts</A>
</H2>
<P>
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 (<span class="file">cgi.py</span>) 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 <span class="file">cgi-bin</span> directory, it should be possible to send it a
request by entering a URL into your browser of the form:
<P>
<dl><dd><pre class="verbatim">
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
</pre></dl>
<P>
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 <span class="file">cgi.py</span> 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 <tt class="module">cgi</tt> module's
<tt class="function">test()</tt> function from your script: replace its main code
with the single statement
<P>
<dl><dd><pre class="verbatim">
cgi.test()
</pre></dl>
<P>
This should produce the same results as those gotten from installing
the <span class="file">cgi.py</span> 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
<i>some</i> code, it is easy to catch exceptions and cause a traceback
to be printed. The <tt class="function">test()</tt> function below in this module is
an example. Here are the rules:
<P>
<OL>
<LI>Import the traceback module before entering the <tt class="keyword">try</tt>
... <tt class="keyword">except</tt> statement
<P>
</LI>
<LI>Assign <code>sys.stderr</code> to be <code>sys.stdout</code>
<P>
</LI>
<LI>Make sure you finish printing the headers and the blank line
early
<P>
</LI>
<LI>Wrap all remaining code in a <tt class="keyword">try</tt> ... <tt class="keyword">except</tt>
statement
<P>
</LI>
<LI>In the except clause, call <tt class="function">traceback.print_exc()</tt>
</LI>
</OL>
<P>
For example:
<P>
<dl><dd><pre class="verbatim">
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()
</pre></dl>
<P>
Notes: The assignment to <code>sys.stderr</code> is needed because the
traceback prints to <code>sys.stderr</code>.
The <code>print "\n\n<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>
<dl><dd><pre class="verbatim">
import sys
sys.stderr = sys.stdout
print "Content-Type: text/plain"
print
...your code here...
</pre></dl>
<P>
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>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node245.html" tppabs="http://www.python.org/doc/current/lib/node245.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node245.html" tppabs="http://www.python.org/doc/current/lib/node245.html">11.2.7 Testing your CGI</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">11.2 cgi </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node247.html" tppabs="http://www.python.org/doc/current/lib/node247.html">11.2.9 Common problems and</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -