Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bottle+cherrypy gives ""TypeError: WSGI response header key 'Content-Length' is not a byte string" #195

Closed
rwd2 opened this issue Aug 8, 2011 · 10 comments

Comments

@rwd2
Copy link

rwd2 commented Aug 8, 2011

I am not sure if this is a bottle or a cherrypy issue, but anyway

I'm using the following configuration:

Arch Linux with 2.6.32-lts kernel
hardware: i686 Intel(R) Atom(TM) CPU N270 @ 1.60GHz
python 3.2
cherrypy-svn 2567-1
bottle 0.9.5

Both a Bottle and a Cherrypy 'hello world' run fine. But when I try to run Bottle with Cherrypy like this:


from bottle import route, run

@route('/')
def hello():
return 'hello world'

run(server='cherrypy',host='192.168.1.5', port=8080)


I get this error message:


Traceback (most recent call last):
File "/usr/lib/python3.2/site-packages/bottle.py", line 743, in wsgi
start_response(status, response.headerlist)
File "/usr/lib/python3.2/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 1846, in start_response
raise TypeError("WSGI response header key %r is not a byte string." % k)
TypeError: WSGI response header key 'Content-Length' is not a byte string.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.2/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 1034, in communicate
req.respond()
File "/usr/lib/python3.2/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 830, in respond
self.server.gateway(self).respond()
File "/usr/lib/python3.2/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 1808, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/usr/lib/python3.2/site-packages/bottle.py", line 759, in call
return self.wsgi(environ, start_response)
File "/usr/lib/python3.2/site-packages/bottle.py", line 755, in wsgi
start_response('500 INTERNAL SERVER ERROR', [('Content-Type', 'text/html')])
File "/usr/lib/python3.2/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 1830, in start_response
raise AssertionError("WSGI start_response called a second "
AssertionError: WSGI start_response called a second time with no exc_info.
`

@sc68cal
Copy link
Contributor

sc68cal commented Aug 8, 2011

Is Bottle compatible with Python 3.x @defnull?

@defnull
Copy link
Member

defnull commented Aug 9, 2011

According to PEP 3333, all request and response headers and metadata are "native" strings, that is, bytes for Python 2 and unicode for python 3. 'Content-Length' nit being a byte string is not a WSGI error.

And yes, Bottle is 3.x and pep-3333 compatible.

@sc68cal
Copy link
Contributor

sc68cal commented Aug 23, 2011

OK I could be wrong here, I've not done a lot of work with Python3 yet, but I've done a bit of digging and it seems that wsgi is passing tuples of strs to cherrypy when it needs to be converted to bytestrings, at line 779. At least, that's what the WSGI gateway reference does.

             for header in response_headers:
                 out.write(wsgi_to_bytes('%s: %s\r\n' % header))

@sc68cal
Copy link
Contributor

sc68cal commented Aug 23, 2011

It appears that line 720 is the problem. The call to len returns a str which is assigned to the response headers for Content-Length.

@defnull
Copy link
Member

defnull commented Aug 29, 2011

Headers are always converted to native strings (using str()), so that should be OK.

To quote the PEP:

"Native" strings (which are always implemented using the type named str) that are used for request/response headers and metadata.

CherryPy seems to assume byte string as header names and values, which is wrong in my opinion. I'll file a bug report just to make sure.

@defnull
Copy link
Member

defnull commented Aug 29, 2011

Here is the ticket: http://www.cherrypy.org/ticket/1087

I do not know how hard it is to fix this, but I'll try to get a working patch to the cherrypy guys.

@iurisilvio
Copy link
Member

I guess cherrypy changed their ticket control, so that URL returns a 404.

Your ticket is here https://bitbucket.org/cherrypy/cherrypy/issue/1087/pep-3333-compliance-unicode-response and looks like it is already solved. I'm not sure if it is already released.

@sc68cal
Copy link
Contributor

sc68cal commented Dec 13, 2011

👏

@defnull
Copy link
Member

defnull commented Dec 13, 2011

Thanks for the feedback. Fixed upstream -> closed

@defnull defnull closed this as completed Dec 13, 2011
@simevo
Copy link

simevo commented Jan 4, 2017

for future reference the cherrypy ticket is now here:
cherrypy/cherrypy#1087

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants