Pregunta

Estoy tratando de coger datos POST de una forma sencilla.

Esta es la primera vez que estoy jugando con wsgiref y me parece que no puede encontrar la forma correcta de hacerlo.

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

Y la función que está claramente ausente la información correcta para atrapar mensaje:

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()
¿Fue útil?

Solución

Se debe leer las respuestas del servidor.

respuesta de nosklo a un problema similar: " PEP 333 dice debe leer Environ [ 'wsgi.input'] ."

código Probado (adaptado de esta respuesta ):
Advertencia: Este código es sólo con fines demostrativos.
Advertencia: Trate de evitar los caminos o nombres de archivos codificación dura

.
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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top