python - twisted image transfer from client to server gives bad format error -
i trying send image file using tcp server client. tried opening file, reading , transporting using self.transport.write. on client side, when receive data, open file named image in append mode, , write it.
client:
class echoclient(protocol.protocol): def datareceived(self, data): print 'writing file' f = open('image.png','a') f.write(data) f.close()
server (inherits protocol):
//somewhere in code image = open(self.newdict[device_str] + attribute_str + '.png') data = image.read() image.close() self.comm_protocol.transport.write(data)
opening file on client side gives bad format error. ideas doing wrong ? idea stream image string bad ? if so, there other way can transfer data client ?
you have open file in binary mode, 'b' flag, open(..., 'wb'
).
the reason file gets corrupted "text mode" 1 of 2 things:
- on unix, nothing.
- on windows, replaces
\n
\r\n
.
now, if it's text file, can hardly tell difference. but, if it's binary file, byte might doesn't mean "newline" more. generally, binary files constructed fixed-length structures, sticking 2 bytes in 1 expected cause kinds of havoc.
Comments
Post a Comment