python3 cherryPy字节字符串错误

时间:2012-08-16 07:32:38

标签: python python-3.x cherrypy

我有以下服务器:

    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'))]

1 个答案:

答案 0 :(得分:0)

试试这个:

response_headers = [(u'Content-type',u'text/plain')]