Receive data from socket stream

Mark Tolonen M8R-yfto6h at mailinator.com
Sun Apr 27 23:09:58 EDT 2008


<s0suk3 at gmail.com> wrote in message 
news:4ca7b83c-0ca4-4e38-95e5-264780d30ec0 at 56g2000hsm.googlegroups.com...
> 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?

I think you meant:

    while totalsent < len(data):

Python also has the sendall() function.

-Mark 




More information about the Python-list mailing list