How do I get POST form variables using wsgi? Please help

2,552 views
Skip to first unread message

Wyatt Biker

unread,
Jun 24, 2008, 11:58:16 AM6/24/08
to modwsgi
I looked everywhere for examples how to get form fields from a web page. I couldnt find a good example.
Platform: Windows Vista/Apache/2.2.9 (Win32) mod_wsgi/2.0 Python/2.5.2
I can single step and debug the code (using ActiveState IDE), but, I cannot see or get my form variables "name" and "submit" values.
Is there some special function or is my config wron? Where do i see full examples?

Thanks.

Here is my Apache config file.
---------------------------------------------
LoadModule wsgi_module modules/mod_wsgi.so
Alias /mymed c:/med/
<Directory c:/med/>
Options ExecCGI Indexes
AddHandler wsgi-script .py
Order allow,deny
Allow from all
</Directory>

Here is hello.html
--------------------------
<html>
<body>
    <form action="hello.py" target="top" method="POST">
        Name : <input id="name" type="text" value="John Doe">
        Submit: <input id="submit" type="submit" value="submit">
    </form>
</body>
</html>

Here is my hello.py
--------------------------------------
import sys
sys.path.append("C:\\Program Files\\ActiveState Komodo IDE 4\\lib\\support\\dbgp\\pythonlib")
from dbgp import client
import os

def application(environ, start_response):
    client.brk(host="localhost", port=9000)
# at this point param environ has no objects or field information from the form.
# How do I get it?
    status = '200 OK'
    output = 'Hello World!' + 'abc'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]



Graham Dumpleton

unread,
Jun 25, 2008, 8:49:15 AM6/25/08
to mod...@googlegroups.com
I would suggest you use one of the web toolkits/frameworks rather than
trying to do it from scratch yourself.

A nice nuts and bolts approach is:

http://werkzeug.pocoo.org/

FWIW, for a POST request you have to read request content and parse
out form arguments. Toolkits like above do this for you and why you
probably want to use one of them.

Graham

2008/6/25 Wyatt Biker <wyatt...@gmail.com>:

Clodoaldo

unread,
Jun 25, 2008, 9:18:04 AM6/25/08
to mod...@googlegroups.com
2008/6/24 Wyatt Biker <wyatt...@gmail.com>:

> I looked everywhere for examples how to get form fields from a web page. I
> couldnt find a good example.

They are in the response body. To retrieve them you use the file like
object in environ['wsgi.input']:

# app.wsgi
from urllib import unquote_plus

def application(environ, start_response):

body = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))
parameter_pairs = body.split('&')
parameter = dict()

for parameter_pair in parameter_pairs:

parameter_pair = parameter_pair.split('=')
parameter[unquote_plus(parameter_pair[0])] = \
unquote_plus(parameter_pair[1])

output = repr(parameter)
status = '200 OK'


response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

In addition to the id parameter in the input tags you must also use a
name parameter:

Name : <input id="name" name="name" type="text" value="John Doe">

Obviously it would be much easier to use modules or middeware provided
by a framework or something else not that heavy.

Regards, Clodoaldo

Robert Corsaro

unread,
Jun 25, 2008, 11:57:51 AM6/25/08
to mod...@googlegroups.com
You can also use paste:
{{{
from paste import request

def application(environ, start_response):
    if (environ['REQUEST_METHOD'] == 'POST):
        fields = request.parse_formvars(environ)
        field1 = fields.get('field1', 'default)
}}}

paste has more then a few convenience methods.

fulj...@gmail.com

unread,
Oct 31, 2013, 9:57:45 AM10/31/13
to mod...@googlegroups.com, rcor...@gmail.com

On Wednesday, June 25, 2008 5:57:51 PM UTC+2, Robert Corsaro wrote:
You can also use paste:
{{{
from paste import request

def application(environ, start_response):
    if (environ['REQUEST_METHOD'] == 'POST):
        fields = request.parse_formvars(environ)
        field1 = fields.get('field1', 'default)
}}}

paste has more then a few convenience methods.


Beautiful answer! Update 2013:



from webob import Request


def application(environ, start_response):
    if (environ['REQUEST_METHOD'] == 'POST):
        req = Request(environ)
        field1 = req.params.get('field1', 'default')



webob can be installed in Ubuntu with:
sudo apt-get install python-webob

read more about webob at:
https://en.wikipedia.org/wiki/Python_Paste
http://webob.org/
http://docs.repoze.org/moonshining/tools/webob.html

Reply all
Reply to author
Forward
0 new messages