2019-04-21 11:50:48 +08:00

3 lines
5.6 KiB
HTML

<div class="body" role="main"><div class="section" id="module-cgitb"><h1><span class="yiyi-st" id="yiyi-10">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> — — CGI 脚本回馈错误管理 </span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/cgitb.py">Lib / cgitb.py</a></span></p><p><span class="yiyi-st" id="yiyi-12"><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> 模块为 Python 脚本提供了一个特殊的异常处理程序。</span><span class="yiyi-st" id="yiyi-13">(它的名字是有点误导性的。</span><span class="yiyi-st" id="yiyi-14">最初,它旨在为 CGI 脚本在 HTML 中显示广泛的追踪信息。</span><span class="yiyi-st" id="yiyi-15">它后来被推广为以纯文本形式显示此信息。)</span><span class="yiyi-st" id="yiyi-16">本模块被激活,如果未捕获的异常发生后,将显示详细的、 已格式化的报告。</span><span class="yiyi-st" id="yiyi-17">该报告包括回溯显示摘录的每个级别的源代码以及参数和局部变量于当前运行的功能,以帮助您调试问题的值。</span><span class="yiyi-st" id="yiyi-18">您还可以选择将此信息保存到文件而不是将其发送到浏览器。</span></p><p><span class="yiyi-st" id="yiyi-19">若要启用此功能,只需要简单地引入模块和写下你的 CGI 脚本︰</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">cgitb</span>
<span class="n">cgitb</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-20"><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> 函数的参数选项控制在浏览器中显示错误信息还是记录到文件中供以后分析。</span></p><dl class="function"><dt id="cgitb.enable"><span class="yiyi-st" id="yiyi-21"> <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></span></dt><dd><p id="index-1"><span class="yiyi-st" id="yiyi-22">此函数使<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>模块通过设置<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>的值来接管解释器对异常的默认处理。</span></p><p><span class="yiyi-st" id="yiyi-23">可选参数,<em>display</em> 默认为 <code class="docutils literal"><span class="pre">1</span></code>,可以设置为 <code class="docutils literal"><span class="pre">0</span></code>,以禁止向浏览器发送的错误消息。</span><span class="yiyi-st" id="yiyi-24">如果参数 <em>logdir</em>被设置为日志文件所在目录,错误消息将被写入文件。</span><span class="yiyi-st" id="yiyi-25"><em>Logdir</em> 的值应该是一个目录,就是错误日志文件将被放置的位置。</span><span class="yiyi-st" id="yiyi-26">可选参数 <em>context</em> 是要显示出错行上下文的行数; 这将默认为 <code class="docutils literal"><span class="pre">5</span></code></span><span class="yiyi-st" id="yiyi-27">如果可选参数 <em>format</em><code class="docutils literal"><span class="pre">"html"</span></code>,输出的将被格式化为为 HTML格式。</span><span class="yiyi-st" id="yiyi-28">任何其他值都将导致纯文本输出。</span><span class="yiyi-st" id="yiyi-29">默认值为 <code class="docutils literal"><span class="pre">"html"</span></code></span></p></dd></dl><dl class="function"><dt id="cgitb.handler"><span class="yiyi-st" id="yiyi-30"> <code class="descclassname">cgitb.</code><code class="descname">handler</code><span class="sig-paren">(</span><em>info=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-31">此函数使用默认设置处理异常 (也即是,在浏览器中显示错误小心但不记录到一个文件中小心)。</span><span class="yiyi-st" id="yiyi-32">这可以使用当您已经捕获到异常,并且想要把它交给 <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>处理。</span><span class="yiyi-st" id="yiyi-33">可选 <em>info</em> 参数应包含一个异常类型、 异常值和追踪对象,就像 <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> 所返回的元组的 3 元组。</span><span class="yiyi-st" id="yiyi-34">如果不提供 <em>info</em> 参数,则会从 <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> 获得当前异常。</span></p></dd></dl></div></div>