Receive data from socket stream

s0suk3 at gmail.com s0suk3 at gmail.com
Sun Apr 27 14:40:05 EDT 2008


On Apr 26, 7:25 am, Irmen de Jong <irmen.NOS... at xs4all.nl> wrote:
> s0s... at gmail.com wrote:
> > Until now, I've been
> > doing this little trick:
>
> > data = client.recv(256)
> > new = data
> > while len(new) == 256:
> >     new = client.recv(256)
> >     data += new
>
> Are you aware that recv() will not always return the amount of bytes asked for?
> (send() is similar; it doesn't guarantee that the full buffer you pass to it will be
> sent at once)
>
> I suggest reading this:http://www.amk.ca/python/howto/sockets/sockets.html
>
> --irmen

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?



More information about the Python-list mailing list