Haris Bašić
Haris Bašić

Reputation: 1383

Processing mod_wsgi.input

I am newbie in python and mod_wsgi development. When I want to get data from POST i read envron['wsgi.input'] and as a result I get name=username&fullname=name&pass=password. Now my question is how to split it to get that input as array. For example to read like name = arraypost[0]?

Upvotes: 1

Views: 1075

Answers (2)

Dissident Rage
Dissident Rage

Reputation: 2716

Here I'm demonstrating how to do it in Python 3. Using urllib.parse.parse_qs converts it to a dictionary of lists.

import urllib.parse

post = urllib.parse.parse_qs(environ['QUERY_STRING'])

# get value of first instance of POST var 'foo'
print(post.get(b'foo',[''])[0])

Note the use of get() above. The first argument is a byte string, the second argument is a default value. As I said, it's a dictionary of lists, and since we're immediately fetching from index 0, we want at least a zero-length string there so we don't go out-of-bounds.

Upvotes: 1

Graeme Stuart
Graeme Stuart

Reputation: 6053

You could split the data and pass it into dict() to create a dictionary of key, value pairs:

post_dict = dict([item.split('=') for item in envron['wsgi.input'].split('&')])
print post_dict['name']
print post_dict['fullname']
print post_dict['pass']

Upvotes: 1

Related Questions