cgi.FieldStorage always empty - never returns POSTed form Data

Posted by Dan Carlson on Stack Overflow See other posts from Stack Overflow or by Dan Carlson
Published on 2010-05-06T13:31:15Z Indexed on 2010/05/06 14:08 UTC
Read the original article Hit count: 327

Filed under:
|
|
|

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>

© Stack Overflow or respective owner

Related posts about python

Related posts about cgi