[Twisted-web] serving unicode body in twisted web

Ilja Livenson ilja.livenson at gmail.com
Thu May 26 18:52:59 EDT 2011


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) +
        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.


More information about the Twisted-web mailing list