views:

380

answers:

1

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>
+2  A: 

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

Thomas Wouters
Adding to Thomas' response, if using mod_python.publisher the POST data is already processed with the FieldStorage object left in req.form. If you accept arguments in the index() function, the named form arguments will be passed in argument of the same name. Thus, you don't have to worry about FieldStorage at all. See 'http://webpython.codepoint.net/mod_python_publisher_form'.
Graham Dumpleton
Thanks Guys! That makes a lot of sense. I had not come across that detail about mod_python.publisher storing form data in req.form - that was really helpful! For my small experiments at the moment, it doesn't really make sense to use wsgi, but I'll definitely look into it in the future. Thanks again for your help!
Dan Carlson