reading sockets

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

"Ken Mugrage" <kmugrageREMOVE at> wrote in message
news:FGN07.153841$%i7.102425274 at
> 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.


More information about the Python-list mailing list