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

发布于 2024-08-31 17:19:00 字数 1454 浏览 2 评论 0原文

这个问题可能简单得令人尴尬。

我正在尝试尝试一下 python。我认为开始这样做的一个好方法是创建一个简单的 cgi 脚本来处理一些表单数据并执行一些魔法。我的 python 脚本由 apache 使用 mod_python 正确执行,并将打印出我想要打印的任何内容。

我唯一的问题是 cgi.FieldStorage() 始终为空。我尝试过同时使用 POST 和 GET。每次试验我都会填写两个表格字段。

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

如果我更改表单以指向 perl 脚本,它会正确报告表单数据。 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

我正在使用 Python 2.5.2 和 Apache/2.2.3。这是我的 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>

This problem is probably embarrassingly simple.

I'm trying to give python a spin. 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. My python script is executed properly by apache using mod_python, and will print out whatever I want it to print out.

My only problem is that cgi.FieldStorage() is always empty. I've tried using both POST and 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. The python page always gives me the same result: number of keys: 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. This is what's in my apache conf file (and my script is in /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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-09-07 17:19:00

你的问题是你混合了两种不同的方法:CGI 和 mod_python。您将脚本声明为 mod_python 发布者,这就是调用其 index 方法的原因 - 并且这也使其成为一个模块,而不是脚本。

如果您使用的是 CGI,则可以从 Apache 配置中删除 mod_python 指令,只保留 ExecCGI,然后将脚本重命名为具有 .cgi 扩展名,或者设置 的处理程序。 py 扩展名也为 CGI。然后您的脚本将作为脚本执行,这意味着您在脚本中定义的 index 函数不会被执行,除非您从脚本的顶层调用它。

我记得 - 但自从我为 mod_python 烦恼以来已经很长时间了 - 如果你想使用 mod_python 代替,你应该使用 mod_python.util.FieldStorage 而不是 cgi.FieldStorage 访问 POST 数据。

话虽如此,对于简单的 Web 内容来说,更好的选择是 WSGI,对于例如通过 mod_wsgi。通常,比简单的 Web 内容更好的选择是使用 Web 框架,例如 DjangoTurboGears塔架,或众多塔架之一其他列出的内容,例如 wiki.python.org 上的 Web 框架页面

Your problem is that you're mixing two different approaches: CGI and 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.

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. 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.

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.

All that said, a much better choice for bare-bones web stuff is WSGI, for example through 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文