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 ----- This results in a error: .... File "C:\Python25\lib\site-packages\twisted\internet\selectreactor.py", line 146, in _doReadOrWrite why = getattr(selectable, method)() File "C:\Python25\lib\site-packages\twisted\internet\tcp.py", line 428, in doWrite result = abstract.FileDescriptor.doWrite(self) File "C:\Python25\lib\site-packages\twisted\internet\abstract.py", line 199, in doWrite self.dataBuffer = buffer(self.dataBuffer, self.offset) + "".join(self._tempDataBuffer) exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 10: ordinal not in range(128) Any ideas? I assumed that I can put any contents into http body. thanks, Ilja
On 26 May, 10:52 pm, ilja.livenson@gmail.com 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
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, <exarkun@twistedmatrix.com> wrote:
On 26 May, 10:52 pm, ilja.livenson@gmail.com 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 Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (2)
-
exarkun@twistedmatrix.com
-
Ilja Livenson