简体   繁体   English

简单的HTTP响应:为什么此功能适用于Python2但不适用于Python3

[英]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 我不明白为什么在使用/usr/lib/uwsgi/plugins/python_plugin.so时为什么会给我卷曲的GET和POST值,但在使用/ 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 我正在使用curl,如下所示: 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. 我在Python3中添加了解码('utf-8')以将字节转换为字符串。 When working with Python2 I left this out. 使用Python2时,我忽略了这一点。

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. 使它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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