Re: serving unicode body in twisted web

Ilja Livenson <[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
Hi,

thanks a lot for the hint! This was indeed the problem - one of the
headers was in wrong. I have a conditional content-type, one of the
providers returned unicode instance. I guess on python2.7 it behaves
in a different manner.

Just for the archive - solved by adding a bit of debug code in
twisted/internet/abstract.py:doWrite()
            try:
                self.dataBuffer = buffer(self.dataBuffer, self.offset)
+ "".join(self._tempDataBuffer)
                self.offset = 0
                self._tempDataBuffer = []
                self._tempDataLen = 0
            except UnicodeDecodeError:
                err = sys.exc_info()[1]
                print "_tempDataBuffer value: ", self._tempDataBuffer
                print "_tempDataBuffer types: ", [type(x) for x in
self._tempDataBuffer]

Should this perhaps be added as an additional check to Twisted?

thanks,
Ilja

On 27 May 2011 03:20,  <[email protected]> wrote:
> 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
>
> _______________________________________________
> Twisted-web mailing list
> [email protected]
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>
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.