TypeError: sequence of byte string values expected, value of type str found

TypeError: sequence of byte string values expected, value of type str found

我正在尝试 运行 一个简单的 "hello world" 应用程序使用 mod_wsgi for Python 3. 我正在使用 Fedora 23。这是我的 Apache 虚拟主机配置:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin admin@localhost
    # ServerAlias foo.localhost
    WSGIScriptAlias /headers /home/httpd/localhost/python/headers/wsgi.py
    DocumentRoot /home/httpd/localhost/public_html
    ErrorLog /home/httpd/localhost/error.log
    CustomLog /home/httpd/localhost/requests.log combined
</VirtualHost>

wsgi.py:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-Type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output]

如果我将 mod_wsgi 用于 Python 2 (sudo dnf remove python3-mod_wsgi -y && sudo dnf install mod_wsgi -y && sudo apachectl restart),它工作正常,但是当我使用 Python 3 时出现 500 内部服务器错误。这是错误日志:

mod_wsgi (pid=899): Exception occurred processing WSGI script '/home/httpd/localhost/python/headers/wsgi.py'.
TypeError: sequence of byte string values expected, value of type str found

更新

str(len(output)) 上使用 encode()(或 encode('utf-8'))也不起作用。现在我得到:

Traceback (most recent call last):
  File "/home/httpd/localhost/python/headers/wsgi.py", line 8, in application
    start_response(status, response_headers)
TypeError: expected unicode object, value of type bytes found

显然变量 output 本身需要有一个字节字符串而不是一个 unicode 字符串。它不仅需要针对 response_headers 进行更改,而且还需要针对 所有使用 output 的地方进行更改 (因此第 6 行的 str(len(output)).encode('utf-8') 将不起作用,例如我一直在努力)。

所以我的解决方案是:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

(根据 Rolbrok 在评论中的建议,我在官方 mod_wsgi 存储库的 one of the tests 中找到了它。)

背景

这个问题是因为Python3默认UTF-8,因为今天发现有很多非母语英文字符,最好容纳一下。 HTTP 仅适用于 ASCII 字符。它不能很好地处理 UTF-8。因此,Apache 和 mod_wsgi 都不能很好地与 UTF 8 配合使用。

解决方案

因此,在准备好整个 html 字符串后,您可以使用内置的 python 函数 - bytes() 对其进行类型转换。这需要一个字符串并给出一个字节字符串。

示例代码

html = "This "
html += "is the code"
html = bytes(html, encoding= 'utf-8')
response_header = [('Content-type', 'text/html')]
start_response(status, response_header)
yield html