Skip to content Skip to sidebar Skip to footer

Twisted Image Transfer From Client To Server Gives Bad Format Error

I was trying to send an image file using tcp from server to client. I tried opening the file, reading it and then transporting it using self.transport.write. On the client side, wh

Solution 1:

You have to open the file in binary mode, with the 'b' flag, like open(..., 'wb').

The reason that the file gets corrupted is that "text mode" does one of two things:

  1. on UNIX, it does nothing.
  2. on Windows, it just replaces \n with \r\n.

Now, if it's a text file, you can hardly tell the difference. But, if it's a binary file, that byte might doesn't mean "newline" any more. Generally, binary files are constructed from fixed-length structures, so sticking two bytes in where one is expected will cause all kinds of havoc.

Post a Comment for "Twisted Image Transfer From Client To Server Gives Bad Format Error"