Receive data from socket stream

Nick Craig-Wood nick at craig-wood.com
Mon Apr 28 11:30:03 CEST 2008


Mark Tolonen <M8R-yfto6h at mailinator.com> wrote:
> > So every time I use I want to send some thing, I must use
> >
> > totalsent = 0
> > while sent < len(data):
> >    sent = sock.send(data[totalsent:])
> >    totalsent += sent
> >
> > instead of a simple sock.send(data)? That's kind of nasty. Also, is it
> > better then to use sockets as file objects? Maybe unbuffered?
> 
>  I think you meant:
> 
>      while totalsent < len(data):
> 
>  Python also has the sendall() function.

Just to elaborate, sendall does exactly the above for you.

     sendall(self, *args)
         sendall(data[, flags])
         
         Send a data string to the socket.  For the optional flags
         argument, see the Unix manual.  This calls send() repeatedly
         until all data is sent.  If an error occurs, it's impossible
         to tell how much data has been sent.

There should really be a recvall for symmetry, but I don't think it
would get much use!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list