socket receive file does not match sent file

Fredrik Lundh fredrik at pythonware.com
Tue Nov 8 03:19:24 EST 2005


zelzel.zsu at gmail.com wrote:

> while True:
>        data = f.read(8192)
>        if not data: break
>        else:
>                s.send(data)

> What is the cause of the problem, can anyone tell me?

using sendall instead of send should fix this.  see the library reference for
details:

    send( string[, flags])
      Send data to the socket. The socket must be connected to a remote
      socket. The optional flags argument has the same meaning as for recv()
      above. Returns the number of bytes sent. Applications are responsible
      for checking that all data has been sent; if only some of the data was
      transmitted, the application needs to attempt delivery of the
      remaining data.

      sendall( string[, flags])

      Send data to the socket. The socket must be connected to a remote
      socket. The optional flags argument has the same meaning as for recv()
      above. Unlike send(), this method continues to send data from string
      until either all data has been sent or an error occurs. None is
      returned on success. On error, an exception is raised, and there is no
      way to determine how much data, if any, was successfully sent.

  </F>






More information about the Python-list mailing list