cgi错误:TypeError:不可索引

时间:2018-01-05 11:32:53

标签: python cgi typeerror

我希望我的python cgi脚本从HTTP请求接收参数。但是,当我执行请求时,我得到TypeError:

[Fri Jan 05 11:04:14.541250 2018] [cgid:error] [pid 23983:tid 139869410019072] [client xx.xxx.xx.xxx:yyyyy] End of script output before headers: parameters_test.py
Traceback (most recent call last):
File "path/to/script.py", line 18, in <module>
low_value = arguments.getvalue("low_value")
File "/home/ubuntu/anaconda3/lib/python3.6/cgi.py", line 612, in getvalue
if key in self:
File "/home/ubuntu/anaconda3/lib/python3.6/cgi.py", line 652, in __contains__
raise TypeError("not indexable")
TypeError: not indexable

我通过ajax GET请求将参数发送到python脚本,如下所示:

    <script type=text/javascript>
       $.ajax({
        url: "path/to/script.py",
        data: {
          "low_value": 500,
          "high_value": 4000
        },
        type: "GET",
        datatype: "json",
        contentType: "application/json; charset=utf-8"
     });
</script>

我尝试使用以下python脚本接收参数:

#! /path/to/python
import cgi

arguments = cgi.FieldStorage()
low_value = arguments.getvalue("low_value")
high_value = arguments.gervalue("high_value")

很明显,错误出现在low_value = arguments.getvalue("low_value")。有人能解释为什么会发生这种错误吗谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

#!/path/to/python

import cgi
import cgitb
cgitb.enable()

arguments = cgi.FieldStorage()
low_value = arguments.getvalue("low_value")
high_value = arguments.getvalue("high_value")

print("Content-type: text/html")
print()
print("High value: " + high_value + "Low value: " + low_value)

从浏览器中点击它,将显示任何错误。