简体   繁体   中英

cgi.FieldStorage always empty - never returns POSTed form Data

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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