Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / node478.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="node479.html" />
13<link rel="prev" href="node477.html" />
14<link rel="parent" href="module-cgi.html" />
15<link rel="next" href="node479.html" />
16<meta name='aesop' content='information' />
17<title>11.2.9 Debugging CGI scripts</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="11.2.8 Testing your CGI"
25 href="node477.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="11.2 cgi "
28 href="module-cgi.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="11.2.10 Common problems and"
31 href="node479.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="node477.html">11.2.8 Testing your CGI</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-cgi.html">11.2 cgi </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node479.html">11.2.10 Common problems and</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION0013290000000000000000">
5611.2.9 Debugging CGI scripts</A>
57</H2> <a id='l2h-3175' xml:id='l2h-3175'></a>
58<P>
59First of all, check for trivial installation errors -- reading the
60section above on installing your CGI script carefully can save you a
61lot of time. If you wonder whether you have understood the
62installation procedure correctly, try installing a copy of this module
63file (<span class="file">cgi.py</span>) as a CGI script. When invoked as a script, the file
64will dump its environment and the contents of the form in HTML form.
65Give it the right mode etc, and send it a request. If it's installed
66in the standard <span class="file">cgi-bin</span> directory, it should be possible to send it a
67request by entering a URL into your browser of the form:
68
69<P>
70<div class="verbatim"><pre>
71http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&amp;addr=At+Home
72</pre></div>
73
74<P>
75If this gives an error of type 404, the server cannot find the script
76- perhaps you need to install it in a different directory. If it
77gives another error, there's an installation problem that
78you should fix before trying to go any further. If you get a nicely
79formatted listing of the environment and form content (in this
80example, the fields should be listed as ``addr'' with value ``At Home''
81and ``name'' with value ``Joe Blow''), the <span class="file">cgi.py</span> script has been
82installed correctly. If you follow the same procedure for your own
83script, you should now be able to debug it.
84
85<P>
86The next step could be to call the <tt class="module">cgi</tt> module's
87<tt class="function">test()</tt> function from your script: replace its main code
88with the single statement
89
90<P>
91<div class="verbatim"><pre>
92cgi.test()
93</pre></div>
94
95<P>
96This should produce the same results as those gotten from installing
97the <span class="file">cgi.py</span> file itself.
98
99<P>
100When an ordinary Python script raises an unhandled exception (for
101whatever reason: of a typo in a module name, a file that can't be
102opened, etc.), the Python interpreter prints a nice traceback and
103exits. While the Python interpreter will still do this when your CGI
104script raises an exception, most likely the traceback will end up in
105one of the HTTP server's log files, or be discarded altogether.
106
107<P>
108Fortunately, once you have managed to get your script to execute
109<em>some</em> code, you can easily send tracebacks to the Web browser
110using the <tt class="module"><a href="module-cgitb.html">cgitb</a></tt> module. If you haven't done so already,
111just add the line:
112
113<P>
114<div class="verbatim"><pre>
115import cgitb; cgitb.enable()
116</pre></div>
117
118<P>
119to the top of your script. Then try running it again; when a
120problem occurs, you should see a detailed report that will
121likely make apparent the cause of the crash.
122
123<P>
124If you suspect that there may be a problem in importing the
125<tt class="module"><a href="module-cgitb.html">cgitb</a></tt> module, you can use an even more robust approach
126(which only uses built-in modules):
127
128<P>
129<div class="verbatim"><pre>
130import sys
131sys.stderr = sys.stdout
132print "Content-Type: text/plain"
133print
134...your code here...
135</pre></div>
136
137<P>
138This relies on the Python interpreter to print the traceback. The
139content type of the output is set to plain text, which disables all
140HTML processing. If your script works, the raw HTML will be displayed
141by your client. If it raises an exception, most likely after the
142first two lines have been printed, a traceback will be displayed.
143Because no HTML interpretation is going on, the traceback will be
144readable.
145
146<P>
147
148<DIV CLASS="navigation">
149<div class='online-navigation'>
150<p></p><hr />
151<table align="center" width="100%" cellpadding="0" cellspacing="2">
152<tr>
153<td class='online-navigation'><a rel="prev" title="11.2.8 Testing your CGI"
154 href="node477.html"><img src='../icons/previous.png'
155 border='0' height='32' alt='Previous Page' width='32' /></A></td>
156<td class='online-navigation'><a rel="parent" title="11.2 cgi "
157 href="module-cgi.html"><img src='../icons/up.png'
158 border='0' height='32' alt='Up One Level' width='32' /></A></td>
159<td class='online-navigation'><a rel="next" title="11.2.10 Common problems and"
160 href="node479.html"><img src='../icons/next.png'
161 border='0' height='32' alt='Next Page' width='32' /></A></td>
162<td align="center" width="100%">Python Library Reference</td>
163<td class='online-navigation'><a rel="contents" title="Table of Contents"
164 href="contents.html"><img src='../icons/contents.png'
165 border='0' height='32' alt='Contents' width='32' /></A></td>
166<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
167 border='0' height='32' alt='Module Index' width='32' /></a></td>
168<td class='online-navigation'><a rel="index" title="Index"
169 href="genindex.html"><img src='../icons/index.png'
170 border='0' height='32' alt='Index' width='32' /></A></td>
171</tr></table>
172<div class='online-navigation'>
173<b class="navlabel">Previous:</b>
174<a class="sectref" rel="prev" href="node477.html">11.2.8 Testing your CGI</A>
175<b class="navlabel">Up:</b>
176<a class="sectref" rel="parent" href="module-cgi.html">11.2 cgi </A>
177<b class="navlabel">Next:</b>
178<a class="sectref" rel="next" href="node479.html">11.2.10 Common problems and</A>
179</div>
180</div>
181<hr />
182<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
183</DIV>
184<!--End of Navigation Panel-->
185<ADDRESS>
186See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
187</ADDRESS>
188</BODY>
189</HTML>