20.3. cgitb — Traceback manager for CGI scripts - Python 2.7.18 documentation 编辑
New in version 2.2.
The cgitb
module provides a special exception handler for Python scripts. (Its name is a bit misleading. It was originally designed to display extensive traceback information in HTML for CGI scripts. 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:
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.
cgitb.
enable
([display[, logdir[, context[, format]]]])This function causes the
cgitb
module to take over the interpreter’s default handling for exceptions by setting the value ofsys.excepthook
.The optional argument display defaults to
1
and can be set to0
to suppress sending the traceback to the browser. If the argument logdir is present, the traceback reports are written to files. The value of logdir should be a directory where these files will be placed. 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 to5
. If the optional argument format is"html"
, the output is formatted as HTML. Any other value forces plain text output. The default value is"html"
.
cgitb.
handler
([info])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
. The optional info argument should be a 3-tuple containing an exception type, exception value, and traceback object, exactly like the tuple returned bysys.exc_info()
. If the info argument is not supplied, the current exception is obtained fromsys.exc_info()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论