[Twisted-web] serving unicode body in twisted web

Ilja Livenson ilja.livenson at gmail.com
Fri May 27 02:36:57 EDT 2011


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
                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

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


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

More information about the Twisted-web mailing list