简体   繁体   中英

Simple HTTP response: why does this work with Python2 but not with Python3

With the following code, running with:

uwsgi --socket myapp.sock --plugins /usr/lib/uwsgi/plugins/python_plugin.so \
--module wsgi --chmod-socket=664

I don't understand why this gives me GET and POST values printed by curl nicely when using /usr/lib/uwsgi/plugins/python_plugin.so but not when using /usr/lib/uwsgi/plugins/python 3 _plugin.so

I'm using curl as follows: curl -v --form 'file=@testfile;filename=newfilename' --form 'q=c' localhost?q=x

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0

    request_body = environ.get('wsgi.input', b'').read(request_body_size).decode('utf-8')
    get_values = environ.get('QUERY_STRING', '')

    return ["Hello There!\n\n" + request_body + get_values]

I added the decode('utf-8') to convert from bytes to string in Python3. When working with Python2 I left this out.

return ["Hello There!\n\n" + request_body + get_values]

changed to:

return [("Hello There!\n\n" + request_body + get_values).encode('utf-8')]

makes it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM