Re: serving unicode body in twisted web

[email protected]
Newsgroups gmane.comp.python.twisted.web
Message-ID <20110527012049.3983.1650791680.divmod.xquotient.289@localhost.localdomain>
On 26 May, 10:52 pm, [email protected] wrote:
>Hi,
>
>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.

Jean-Paul
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.