Pergunta

Eu estou tentando recuperar dados POST de um formulário simples.

Esta é a primeira vez que eu estou brincando com wsgiref e eu não consigo encontrar a maneira correta de fazer isso.

This is the form:
<form action="test" method="POST">
<input type="text" name="name">
<input type="submit"></form>

E a função que é, obviamente, faltando a informação certa para pós captura:

def app(environ, start_response):
    """starts the response for the webserver"""
    path = environ[ 'PATH_INFO']
    method = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            start_response('200 OK',[('Content-type', 'text/html')])
            return "POST info would go here %s" % post_info
    else:
        start_response('200 OK', [('Content-type', 'text/html')])
        return form()
Foi útil?

Solução

Você deve estar lendo as respostas do servidor.

A partir de nosklo resposta para um problema similar: " PEP 333 diz você deve ler environ [ 'wsgi.input'] ."

código testado (adaptado de esta resposta ):
Aviso: Este código é apenas para fins demonstrativos.
Aviso:. Tente evitar caminhos ou nomes de arquivos embutir

def app(environ, start_response):
    path    = environ['PATH_INFO']
    method  = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            try:
                request_body_size = int(environ['CONTENT_LENGTH'])
                request_body = environ['wsgi.input'].read(request_body_size)
            except (TypeError, ValueError):
                request_body = "0"
            try:
                response_body = str(request_body)
            except:
                response_body = "error"
            status = '200 OK'
            headers = [('Content-type', 'text/plain')]
            start_response(status, headers)
            return [response_body]
    else:
        response_body = open('test.html').read()
        status = '200 OK'
        headers = [('Content-type', 'text/html'),
                    ('Content-Length', str(len(response_body)))]
        start_response(status, headers)
        return [response_body]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top