problem with sockets and transferring binary files

Gabriel Genellina gagsl-py2 at
Wed Jun 3 07:29:55 CEST 2009

En Wed, 03 Jun 2009 01:54:02 -0300, <thebiggestbangtheory at>  

>            I am a python newbie. I am now progressing to writing a
> network app in python to learn more about it. [...]
> Surprisingly, the sha1 hash of the encrypted data before it is sent
> from server is different  from the encrypted file data received by the
> client.

>           data = connection.recv(10000000) # receive up to 10000000
> bytes

recv returns *up* *to* 10000000 bytes: maybe only one byte.
You'll have to keep recv'ing the pieces until you get an empty string  
(when client closes the connection), or send the file size first and then  
the actual data.

>             buf = open(currentpath+'received-container.tar.gz', 'w')
>             buf.write(info)
>             buf.close()

If you're on Linux it doesn't matter, but the 'w' above should be 'wb' for  
a binary file.

> def send_to_sender(cipher, servername):
>     ...
>     clientSocket.send(cipher)

Similar to recv above: send() might not send the whole string at once (try  
sendall instead).

Gabriel Genellina

More information about the Python-list mailing list