reading sockets

Alex Martelli aleaxit at yahoo.com
Thu Jul 5 08:28:35 EDT 2001


"Ken Mugrage" <kmugrageREMOVE at hotmail.com> wrote in message
news:FGN07.153841$%i7.102425274 at news1.rdc1.sfba.home.com...
    ...
> In perl, I would do something like
>
> while(<SOCKET>) {
>     print $_;
> }
>
> and it would print everything as it is sent, one line at a time.
>
> In Python, I have something like
>
> while 1:
>   data = mysocket.recv(1024)
>   print data
>
> which of course print everything in 1024 byte blocks.

from xreadlines import xreadlines
    ...
for line in xreadlines(sock.makefile()):
    print line,

should be what you want.  sock.makefile() gives you a
file-like object that reads data from the socket, and
the xreadlines() wrapper makes it easy and efficient
to loop on it line by line.


Alex






More information about the Python-list mailing list