我有以下服务器:
from cherrypy import wsgiserver
def my_crazy_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/plain')]
start_response(status, response_headers)
return ['Hello world!']
server = wsgiserver.CherryPyWSGIServer(
('0.0.0.0', 80), my_crazy_app,
server_name='www.cherrypy.example')
server.start()
当我运行它时:
python3.2 server.py
它给出了以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/cherrypy/wsgiserver/__init__.py", line 982, in communicate
req.respond()
File "/usr/local/lib/python3.2/dist-packages/cherrypy/wsgiserver/__init__.py", line 779, in respond
self.server.gateway(self).respond()
File "/usr/local/lib/python3.2/dist-packages/cherrypy/wsgiserver/__init__.py", line 1735, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "test.py", line 6, in my_crazy_app
start_response(status, response_headers)
File "/usr/local/lib/python3.2/dist-packages/cherrypy/wsgiserver/__init__.py", line 1773, in start_response
raise TypeError("WSGI response header key %r is not a byte string." % k)
TypeError: WSGI response header key 'Content-type' is not a byte string.
我尝试了以下方法将 uni-code 字符串更改为字节而不更改错误消息:
response_headers = [(bytes("Content-type", 'utf-8'),bytes("text/plain", 'utf-8'))]
response_headers = [("Content-type".encode('utf-8'),"text/plain".encode('utf-8'))]