简体   繁体   English

Python cgi.FieldStorage() 在表单提交时为空

[英]Python cgi.FieldStorage() is empty on form submission

I have the following directives in my Apache 2 virtual host configuration:我的 Apache 2 虚拟主机配置中有以下指令:

<Directory /var/www/foo_test>
  Options MultiViews +ExecCGI
  AddHandler cgi-script .py
  AllowOverride All 
</Directory>

I have the following simple form HTML:我有以下简单的表单 HTML:

<html>
  <body>
    <form action="foo.py" method="post" id="foo_form">
      <input type="text" id="foo" />
      <input type="submit" />
    </form>
  </body>
</html>

Here is foo.py , the executable script that is called when the form is submitted:这是foo.py ,提交表单时调用的可执行脚本:

#!/usr/bin/env python

import cgi
arguments = cgi.FieldStorage()

print "Content-Type: text/html\n"
print repr(arguments)

When I submit the form, I get the following response:当我提交表单时,我收到以下回复:

FieldStorage(None, None, [])

There is nothing in the FieldStorage object instance that contains values for the fields specified in the form foo_form . FieldStorage对象实例中没有任何内容包含以foo_form形式指定的字段的值。

If I replace the Python script with a Perl script and its CGI module, along with changing the Apache directives to handle Perl CGIs, I have no problems reading fields out of the form.如果我将 Python 脚本替换为 Perl 脚本及其CGI模块,同时更改 Apache 指令以处理 Perl CGI,那么从表单中读取字段就没有问题了。 This suggests to me that my Apache setup is okay.这向我表明我的 Apache 设置没问题。

However, am I missing something with instantiating the cgi.FieldStorage() object, or with my Apache setup, that is causing the form submission to fail?但是,我是否在实例化cgi.FieldStorage()对象或我的 Apache 设置时遗漏了一些导致表单提交失败的内容?

如果希望输入的数据以表单的形式提交,则需要给输入一个name属性。

It blew my mind, but this is what happened to me.这让我大吃一惊,但这就是发生在我身上的事情。

Apparently you cannot "pull" the information from cgi.FieldStorage more than once ?显然你不能不止一次地从cgi.FieldStorage “提取”信息?

Run from any console tab in a browser:从浏览器中的任何console选项卡运行:

$.post('/api/save.cgi', {'hello': 'world'}, (response) => {
   console.log("DONE");
});

Python code in backend后端 Python 代码

#!/usr/bin/env python3

import cgi

fcgi = cgi.FieldStorage()
sys.stderr.write("try 1 %s \n" % fcgi)

fcgi = cgi.FieldStorage()
sys.stderr.write("try 2: %s \n" % fcgi)

See error log:查看错误日志:

try 1: FieldStorage(None, None, [MiniFieldStorage('hello', 'world')])
try 2: FieldStorage(None, None, [])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM