1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>21.3. cgitb — Traceback manager for CGI scripts — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="21. Internet Protocols and Support" href="internet.html" />
34
<link rel="next" title="21.4. wsgiref — WSGI Utilities and Reference Implementation" href="wsgiref.html" />
35
<link rel="prev" title="21.2. cgi — Common Gateway Interface support" href="cgi.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="wsgiref.html" title="21.4. wsgiref — WSGI Utilities and Reference Implementation"
57
accesskey="N">next</a> |</li>
59
<a href="cgi.html" title="21.2. cgi — Common Gateway Interface support"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-cgitb">
95
<span id="cgitb-traceback-manager-for-cgi-scripts"></span><h1>21.3. <a class="reference internal" href="#module-cgitb" title="cgitb: Configurable traceback handler for CGI scripts."><code class="xref py py-mod docutils literal"><span class="pre">cgitb</span></code></a> — Traceback manager for CGI scripts<a class="headerlink" href="#module-cgitb" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/cgitb.py">Lib/cgitb.py</a></p>
97
<hr class="docutils" id="index-0" />
98
<p>The <a class="reference internal" href="#module-cgitb" title="cgitb: Configurable traceback handler for CGI scripts."><code class="xref py py-mod docutils literal"><span class="pre">cgitb</span></code></a> module provides a special exception handler for Python scripts.
99
(Its name is a bit misleading. It was originally designed to display extensive
100
traceback information in HTML for CGI scripts. It was later generalized to also
101
display this information in plain text.) After this module is activated, if an
102
uncaught exception occurs, a detailed, formatted report will be displayed. The
103
report includes a traceback showing excerpts of the source code for each level,
104
as well as the values of the arguments and local variables to currently running
105
functions, to help you debug the problem. Optionally, you can save this
106
information to a file instead of sending it to the browser.</p>
107
<p>To enable this feature, simply add this to the top of your CGI script:</p>
108
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cgitb</span>
109
<span class="n">cgitb</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
112
<p>The options to the <a class="reference internal" href="#cgitb.enable" title="cgitb.enable"><code class="xref py py-func docutils literal"><span class="pre">enable()</span></code></a> function control whether the report is
113
displayed in the browser and whether the report is logged to a file for later
115
<dl class="function">
116
<dt id="cgitb.enable">
117
<code class="descclassname">cgitb.</code><code class="descname">enable</code><span class="sig-paren">(</span><em>display=1</em>, <em>logdir=None</em>, <em>context=5</em>, <em>format="html"</em><span class="sig-paren">)</span><a class="headerlink" href="#cgitb.enable" title="Permalink to this definition">¶</a></dt>
118
<dd><p id="index-1">This function causes the <a class="reference internal" href="#module-cgitb" title="cgitb: Configurable traceback handler for CGI scripts."><code class="xref py py-mod docutils literal"><span class="pre">cgitb</span></code></a> module to take over the interpreter’s
119
default handling for exceptions by setting the value of <a class="reference internal" href="sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-attr docutils literal"><span class="pre">sys.excepthook</span></code></a>.</p>
120
<p>The optional argument <em>display</em> defaults to <code class="docutils literal"><span class="pre">1</span></code> and can be set to <code class="docutils literal"><span class="pre">0</span></code> to
121
suppress sending the traceback to the browser. If the argument <em>logdir</em> is
122
present, the traceback reports are written to files. The value of <em>logdir</em>
123
should be a directory where these files will be placed. The optional argument
124
<em>context</em> is the number of lines of context to display around the current line
125
of source code in the traceback; this defaults to <code class="docutils literal"><span class="pre">5</span></code>. If the optional
126
argument <em>format</em> is <code class="docutils literal"><span class="pre">"html"</span></code>, the output is formatted as HTML. Any other
127
value forces plain text output. The default value is <code class="docutils literal"><span class="pre">"html"</span></code>.</p>
130
<dl class="function">
131
<dt id="cgitb.handler">
132
<code class="descclassname">cgitb.</code><code class="descname">handler</code><span class="sig-paren">(</span><em>info=None</em><span class="sig-paren">)</span><a class="headerlink" href="#cgitb.handler" title="Permalink to this definition">¶</a></dt>
133
<dd><p>This function handles an exception using the default settings (that is, show a
134
report in the browser, but don’t log to a file). This can be used when you’ve
135
caught an exception and want to report it using <a class="reference internal" href="#module-cgitb" title="cgitb: Configurable traceback handler for CGI scripts."><code class="xref py py-mod docutils literal"><span class="pre">cgitb</span></code></a>. The optional
136
<em>info</em> argument should be a 3-tuple containing an exception type, exception
137
value, and traceback object, exactly like the tuple returned by
138
<a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a>. If the <em>info</em> argument is not supplied, the current
139
exception is obtained from <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a>.</p>
148
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
149
<div class="sphinxsidebarwrapper">
150
<h4>Previous topic</h4>
151
<p class="topless"><a href="cgi.html"
152
title="previous chapter">21.2. <code class="docutils literal"><span class="pre">cgi</span></code> — Common Gateway Interface support</a></p>
154
<p class="topless"><a href="wsgiref.html"
155
title="next chapter">21.4. <code class="docutils literal"><span class="pre">wsgiref</span></code> — WSGI Utilities and Reference Implementation</a></p>
156
<div role="note" aria-label="source link">
158
<ul class="this-page-menu">
159
<li><a href="../bugs.html">Report a Bug</a></li>
160
<li><a href="../_sources/library/cgitb.txt"
161
rel="nofollow">Show Source</a></li>
166
<div class="clearer"></div>
168
<div class="related" role="navigation" aria-label="related navigation">
171
<li class="right" style="margin-right: 10px">
172
<a href="../genindex.html" title="General Index"
175
<a href="../py-modindex.html" title="Python Module Index"
178
<a href="wsgiref.html" title="21.4. wsgiref — WSGI Utilities and Reference Implementation"
181
<a href="cgi.html" title="21.2. cgi — Common Gateway Interface support"
183
<li><img src="../_static/py.png" alt=""
184
style="vertical-align: middle; margin-top: -1px"/></li>
185
<li><a href="https://www.python.org/">Python</a> »</li>
187
<span class="version_switcher_placeholder">3.5.2</span>
188
<a href="../index.html">Documentation </a> »
191
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
192
<li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> »</li>
196
<div class="inline-search" style="display: none" role="search">
197
<form class="inline-search" action="../search.html" method="get">
198
<input placeholder="Quick search" type="text" name="q" />
199
<input type="submit" value="Go" />
200
<input type="hidden" name="check_keywords" value="yes" />
201
<input type="hidden" name="area" value="default" />
204
<script type="text/javascript">$('.inline-search').show(0);</script>
211
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
213
The Python Software Foundation is a non-profit corporation.
214
<a href="https://www.python.org/psf/donations/">Please donate.</a>
216
Last updated on Sep 23, 2016.
217
<a href="../bugs.html">Found a bug</a>?
219
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'