python - Sending a StringIO to a Twisted FileSender -
i trying send image data filesender in twisted. works fine if use temporary file save image;
#works img = image(format='png', blob=base64.decodestring(dt)) img.save(filename='/tmp/tmp.png') file = open('/tmp/tmp.png','rb') filesender = filesender().beginfiletransfer(file, request) def filefinished(ignored): request.finish() filesender.addcallback(filefinished)
however do in memory rather file. have tried use stringio this, filesender seems send few bytes before giving up. doing wrong?
img = image(format='png', blob=base64.decodestring(dt)) buffer = stringio() img.save(buffer) filesender = filesender().beginfiletransfer(buffer, request) def filefinished(ignored): request.finish() filesender.addcallback(filefinished)
your stringio
positioned @ eof when try send it. it's surprising sends few bytes (i suspect doesn't , you're seeing http framing). try seeking beginning before call beginfiletransfer
.
also, don't need use filesender
here. data entirely in memory. filesender
may increase cost of sending it. instead, might consider simpler:
request.write(buffer.getvalue()) request.finish()
Comments
Post a Comment