P
P
patriot022016-03-27 13:47:51
Python
patriot02, 2016-03-27 13:47:51

How to get GET and POST parameters in WSGI script?

How to get GET and POST parameters using WSGI script? For example
www.site.ru?name=Pupkin
how to display the name?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
patriot02, 2016-04-02
@patriot02

solved the issue

from pprint import pformat
from cgi import parse_qsl, escape

def application(environ, start_response):
    output = b'<p>WSGI!</p>'

    output.append('Post:')
    output.append('<form method="post">')
    output.append('<input type="text" name = "test">')
    output.append('<input type="submit" value="Send">')
    output.append('</form>')

    d = parse_qsl(environ['QUERY_STRING'])
    if environ['REQUEST_METHOD'] == 'POST':
        output.append('<h1>Post  data:</h1>')
        output.append(pformat(environ['wsgi.input'].read()))

    if environ['REQUEST_METHOD'] == 'GET':
        if environ['QUERY_STRING'] != '':
            output.append('<h1>Get data:</h1>')
            for ch in d:
                output.append(' = '.join(ch))
                output.append('<br>')

    output_len = sum(len(line) for line in output)
    start_response('200 OK', [('Content-type', 'text/html'),
                              ('Content-Length', str(output_len))])
    return output

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question