I have the following server:
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()
When I run it with:
python3.2 server.py
it gives the following error:
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.
I have tried the following methods to change the uni-code string to bytes with no change to the error message:
response_headers = [(bytes("Content-type", 'utf-8'),bytes("text/plain", 'utf-8'))]
response_headers = [("Content-type".encode('utf-8'),"text/plain".encode('utf-8'))]
Try this:
response_headers = [(u'Content-type',u'text/plain')]