[Twisted-web] serving unicode body in twisted web

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu May 26 21:20:49 EDT 2011

On 26 May, 10:52 pm, ilja.livenson at gmail.com wrote:
>I'm having a problem putting binary data into a body of http response
>using twisted web.I'm doing something very wrong, any help is
>appreciated. The funny thing is that it runs on python2.7, but fails
>on python2.5. Most probably it has something to do with unicode/str.
>Code snipplet:
>def render_GET(self, request):
>    # get contents of a binary file, e.g. zip archive
>    request.setHeader('Content-type', 'some-mime')
>    return content

I'm not sure why you get different behavior on Python 2.5 vs Python 2.7. 
You can certainly put any data you want, including "binary" (that is, 
non-ASCII) bytes.
>This results in a error:
>        exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte
>0x92 in position 10: ordinal not in range(128)

This error does suggest that a unicode object has gotten into the send 
buffer somehow.  Check to make sure the content you returned from 
render_GET is of type `str` and that all of the header names and values 
that you set are also of type `str`.  A `unicode` object slipping in to 
one of those places is the most likely cause of this problem.


More information about the Twisted-web mailing list