我正在尝试使用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()
。 - Rolbrokmod_wsgi
。 - Remi Guanstatus.encode('utf-8')
? - Rolbrok