0

私は次のサーバーを持っています:

    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.

エラーメッセージを変更せずに、Unicode文字列をバイトに変更するために次の方法を試しました。

response_headers = [(bytes("Content-type", 'utf-8'),bytes("text/plain", 'utf-8'))]
response_headers = [("Content-type".encode('utf-8'),"text/plain".encode('utf-8'))]
4

1 に答える 1

0

これを試して:

response_headers = [(u'Content-type',u'text/plain')]
于 2012-08-16T09:27:40.857 に答える