简体   繁体   English

cgi.FieldStorage始终为空 - 永远不会返回POSTed表单数据

[英]cgi.FieldStorage always empty - never returns POSTed form Data

This problem is probably embarrassingly simple. 这个问题可能很简单。

I'm trying to give python a spin. 我试图给python一个旋转。 I thought a good way to start doing that would be to create a simple cgi script to process some form data and do some magic. 我认为开始这样做的一个好方法是创建一个简单的cgi脚本来处理一些表单数据并做一些魔术。 My python script is executed properly by apache using mod_python, and will print out whatever I want it to print out. 我的python脚本由apache使用mod_python正确执行,并将打印出我想要打印的任何内容。

My only problem is that cgi.FieldStorage() is always empty . 我唯一的问题是cgi.FieldStorage()总是空的 I've tried using both POST and GET. 我尝试过使用POST和GET。 Each trial I fill out both form fields. 每次试用我都填写两个表格字段。

<form action="pythonScript.py" method="POST" name="ARGH">
<input name="TaskName" type="text" />
<input name="TaskNumber" type="text" />
<input type="submit" />
</form>

If I change the form to point to a perl script it reports the form data properly. 如果我将表单更改为指向perl脚本,则会正确报告表单数据。 The python page always gives me the same result: number of keys: 0 python页面总是给我相同的结果:键数:0

#!/usr/bin/python

    import cgi

    def index(req):

            pageContent = """<html><head><title>A page from"""
            pageContent += """Python</title></head><body>"""
            form = cgi.FieldStorage()
            keys = form.keys()
            keys.sort()
            pageContent += "<br />number of keys: "+str(len(keys))
            for key in keys:
                    pageContent += fieldStorage[ key ].value
            pageContent += """</body></html>"""
            return pageContent

I'm using Python 2.5.2 and Apache/2.2.3. 我使用的是Python 2.5.2和Apache / 2.2.3。 This is what's in my apache conf file (and my script is in /var/www/python): 这是我的apache conf文件(我的脚本在/ var / www / python中):

   <Directory /var/www/python/>
      Options FollowSymLinks +ExecCGI
      Order allow,deny
      allow from all
      AddHandler mod_python .py
      PythonHandler mod_python.publisher
    </Directory>

Your problem is that you're mixing two different approaches: CGI and mod_python. 你的问题是你混合了两种不同的方法:CGI和mod_python。 You declare your script as a mod_python publisher, which is why its index method gets called -- and which also makes it a module, not a script. 您将脚本声明为mod_python发布者,这就是调用其index方法的原因 - 这也使其成为模块,而不是脚本。

If you were using CGI, you would remove the mod_python directives from your Apache configuration, just leave the ExecCGI, and either rename the script to have the .cgi extension or set the handler for the .py extension to be CGI, as well. 如果您使用的是CGI,则可以从Apache配置中删除mod_python指令,只需保留ExecCGI,并将脚本重命名为.cgi扩展名,或者将.py扩展名的处理程序设置为CGI。 Then your script would be executed as a script, which means the index function you defined in your script wouldn't be executed unless you called it from the toplevel of the script. 然后您的脚本将作为脚本执行,这意味着您在脚本中定义的index函数将不会被执行,除非您从脚本的顶层调用它。

As I recall -- but it's been a long time since I bothered with mod_python -- if you want to use mod_python instead, you should be using mod_python.util.FieldStorage instead of cgi.FieldStorage to access the POST data. 我记得 - 但是自从我使用mod_python以来已经很久了 - 如果你想使用mod_python,你应该使用mod_python.util.FieldStorage而不是cgi.FieldStorage来访问POST数据。

All that said, a much better choice for bare-bones web stuff is WSGI , for example through mod_wsgi . 所有这一切说,对于裸机网页内容更好的选择是WSGI ,例如,通过mod_wsgi的 And usually a better choice than bare-bones web stuff is using a web framework, like Django , TurboGears , Pylons , or one of the many others listed on, for example, the web frameworks page on wiki.python.org 并且通常比简单的Web内容更好的选择是使用Web框架,例如DjangoTurboGearsPylons ,或者在wiki.python.org上web框架页面上列出的众多其他框架之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python cgi.FieldStorage() 在表单提交时为空 - Python cgi.FieldStorage() is empty on form submission cgi.FieldStorage保持为空 - cgi.FieldStorage remains empty Python cgi.FieldStorage()始终返回空白表格,并且无法从http POST读取参数 - Python cgi.FieldStorage() always returns a blank form, and can't read params from http POST python cgi.FieldStorage在multipart / form-data类型的分块传输中为空 - python cgi.FieldStorage is empty on chunked transport of type multipart/form-data Python服务器cgi.FieldStorage解析multipart / form-data - Python server cgi.FieldStorage parsing multipart/form-data 使用Python,为什么迭代sys.stdin永远不会退出并且form = cgi.FieldStorage()挂起? - Using Python, why would iterating sys.stdin never exit and form = cgi.FieldStorage() hang? 使用cgi.FieldStorage解析multipart / form-data; 无钥匙 - Parse multipart/form-data using cgi.FieldStorage; None keys 如果未指定“ filename =”,则带有multipart / form-data的cgi.FieldStorage尝试将二进制文件解码为UTF-8 - cgi.FieldStorage with multipart/form-data tries to decode binary file as UTF-8 if “filename=” not specified cgi.FieldStorage的错误处理 - the error handling of cgi.FieldStorage cgi.FieldStorage不从requests.post读取json数据 - cgi.FieldStorage not reads json data from requests.post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM