【发布时间】:2016-01-17 12:43:10
【问题描述】:
我正在尝试使用mod_wsgi 为 Python 3 运行一个简单的“hello world”应用程序。我使用的是 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
【问题讨论】:
-
你试过编码你的字符串吗?示例:
status.encode() -
@Rolbrok:我确实做过,忘了提。现在我得到
TypeError: expected unicode object, value of type bytes found。 -
status.encode('utf-8')? -
听起来你需要把其中一些改回来,是吗?查看错误指的是哪些。实验。 如果一切都失败了,请阅读文档 :)
标签: python python-3.x mod-wsgi