When you write a new script, consider adding these lines:
import cgitb
cgitb.enable()
This activates a special exception handler that will display detailed reports in the Web browser if any errors occur.
If you’d rather not show the guts of your program to users of your script, you can have the reports saved to files instead, with code like this:
import cgitb
cgitb.enable(display=0, logdir="/tmp")
It’s very helpful to use this feature during script development. The reports produced by cgitb provide information that can save you a lot of time in tracking down bugs. You can always remove the cgitb line later when you have tested your script and are confident that it works correctly.
如以下为第七章中运用cgitb.enable()
浏览器中显示错误的示例: