cgitb
— Traceback manager for CGI scriptsCGI脚本的跟踪管理器¶
Source code: Lib/cgitb.py
Deprecated since version 3.11: 自3.11版起已弃用:The cgitb
module is deprecated (see PEP 594 for details).cgitb
模块已弃用(有关详细信息,请参阅PEP 594)。
The cgitb
module provides a special exception handler for Python scripts. cgitb
模块为Python脚本提供了一个特殊的异常处理程序。(Its name is a bit misleading. (它的名字有点误导人。It was originally designed to display extensive traceback information in HTML for CGI scripts. 它最初的设计目的是为CGI脚本显示HTML中的大量回溯信息。It was later generalized to also display this information in plain text.) 它后来被推广为也以纯文本显示此信息。)After this module is activated, if an uncaught exception occurs, a detailed, formatted report will be displayed. 激活此模块后,如果发生未捕获的异常,将显示详细的格式化报告。The report includes a traceback showing excerpts of the source code for each level, as well as the values of the arguments and local variables to currently running functions, to help you debug the problem. 该报告包括一个回溯,显示了每个级别的源代码摘录,以及当前运行函数的参数和局部变量的值,以帮助您调试问题。Optionally, you can save this information to a file instead of sending it to the browser.或者,您可以将此信息保存到文件中,而不是将其发送到浏览器。
To enable this feature, simply add this to the top of your CGI script:要启用此功能,只需将其添加到CGI脚本的顶部:
import cgitb
cgitb.enable()
The options to the enable()
function control whether the report is displayed in the browser and whether the report is logged to a file for later analysis.enable()
函数的选项控制报告是否显示在浏览器中,以及报告是否记录到文件中以供以后分析。
-
cgitb.
enable
(display=1, logdir=None, context=5, format='html')¶ -
This function causes the该函数通过设置cgitb
module to take over the interpreter’s default handling for exceptions by setting the value ofsys.excepthook
.sys.excepthook
的值,使cgitb
模块接管解释器对异常的默认处理。The optional argument display defaults to可选参数display默认为1
and can be set to0
to suppress sending the traceback to the browser.1
,可以设置为0
以禁止向浏览器发送回溯。If the argument logdir is present, the traceback reports are written to files.如果参数logdir存在,则跟踪报告将写入文件。The value of logdir should be a directory where these files will be placed.logdir的值应该是放置这些文件的目录。The optional argument context is the number of lines of context to display around the current line of source code in the traceback; this defaults to可选参数context是回溯中当前源代码行周围要显示的上下文行数;默认值为5
.5
。If the optional argument format is如果可选参数format为"html"
, the output is formatted as HTML."html"
,则输出格式为html。Any other value forces plain text output.任何其他值强制纯文本输出。The default value is默认值为"html"
."html"
。
-
cgitb.
text
(info, context=5)¶ This function handles the exception described by info (a 3-tuple containing the result of此函数处理info(一个包含sys.exc_info()
), formatting its traceback as text and returning the result as a string.sys.exc_info()
结果的三元组)描述的异常,将其回溯格式化为文本,并将结果作为字符串返回。The optional argument context is the number of lines of context to display around the current line of source code in the traceback; this defaults to可选参数上下文是回溯中当前源代码行周围要显示的上下文行数;默认值为5
.5
。
-
cgitb.
html
(info, context=5)¶ This function handles the exception described by info (a 3-tuple containing the result of此函数处理info(一个包含sys.exc_info()
), formatting its traceback as HTML and returning the result as a string.sys.exc_info()
结果的三元组)描述的异常,将其回溯格式化为HTML,并将结果作为字符串返回。The optional argument context is the number of lines of context to display around the current line of source code in the traceback; this defaults to可选参数context是回溯中当前源代码行周围要显示的上下文行数;默认值为5
.5
。
-
cgitb.
handler
(info=None)¶ This function handles an exception using the default settings (that is, show a report in the browser, but don’t log to a file).此函数使用默认设置处理异常(即,在浏览器中显示报告,但不记录到文件)。This can be used when you’ve caught an exception and want to report it using当您捕捉到异常并希望使用cgitb
.cgitb
报告它时,可以使用此选项。The optional info argument should be a 3-tuple containing an exception type, exception value, and traceback object, exactly like the tuple returned by可选的info参数应该是一个包含异常类型、异常值和回溯对象的三元组,与sys.exc_info()
.sys.exc_info()
返回的元组完全相同。If the info argument is not supplied, the current exception is obtained from如果未提供info参数,则从sys.exc_info()
.sys.exc_info()
获取当前异常。