pythoncgi模块文档_21.2. cgi — Common Gateway Interface support — Python 3.6.12 文档

21.2.8.Debugging CGI scripts¶

First of all, check for trivial installation errors — reading the section

above on installing your CGI script carefully can save you a lot of time. If

you wonder whether you have understood the installation procedure correctly, try

installing a copy of this module file (cgi.py) as a CGI script. When

invoked as a script, the file will dump its environment and the contents of the

form in HTML form. Give it the right mode etc, and send it a request. If it’s

installed in the standard cgi-bin directory, it should be possible to

send it a request by entering a URL into your browser of the form:

http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home

If this gives an error of type 404, the server cannot find the script – perhaps

you need to install it in a different directory. If it gives another error,

there’s an installation problem that you should fix before trying to go any

further. If you get a nicely formatted listing of the environment and form

content (in this example, the fields should be listed as “addr” with value “At

Home” and “name” with value “Joe Blow”), the cgi.py script has been

installed correctly. If you follow the same procedure for your own script, you

should now be able to debug it.

The next step could be to call the cgi module’s test() function

from your script: replace its main code with the single statement

cgi.test()

This should produce the same results as those gotten from installing the

cgi.py file itself.

When an ordinary Python script raises an unhandled exception (for whatever

reason: of a typo in a module name, a file that can’t be opened, etc.), the

Python interpreter prints a nice traceback and exits. While the Python

interpreter will still do this when your CGI script raises an exception, most

likely the traceback will end up in one of the HTTP server’s log files, or be

discarded altogether.

Fortunately, once you have managed to get your script to execute some code,

you can easily send tracebacks to the Web browser using the cgitb module.

If you haven’t done so already, just add the lines:

import cgitb

cgitb.enable()

to the top of your script. Then try running it again; when a problem occurs,

you should see a detailed report that will likely make apparent the cause of the

crash.

If you suspect that there may be a problem in importing the cgitb module,

you can use an even more robust approach (which only uses built-in modules):

import sys

sys.stderr = sys.stdout

print("Content-Type: text/plain")

print()

...your code here...

This relies on the Python interpreter to print the traceback. The content type

of the output is set to plain text, which disables all HTML processing. If your

script works, the raw HTML will be displayed by your client. If it raises an

exception, most likely after the first two lines have been printed, a traceback

will be displayed. Because no HTML interpretation is going on, the traceback

will be readable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值