reading sockets
Martin Sjögren
martin at strakt.com
Thu Jul 5 02:59:46 EDT 2001
On Wed, Jul 04, 2001 at 11:30:45PM +0000, Ken Mugrage wrote:
> Sorry for the newbie question, but I'm just learning python and trying to
> write a small program that uses sockets.
>
> Basically, I wam to emulate the perl behavior of reading the response from
> the server one line at a time.
>
> 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.
>
> I tried using string.split( data, "\n") but the results are inaccurate,
> since the end of data may be in the middle of a line.
I'm no star on Python, and I'm not sure this'll work, but you might want
to try this:
f = mysocket.makefile('r')
line = f.readline()
while line:
print line, # line already has a \n
line = f.readline()
--
Martin Sjögren
martin at strakt.com ICQ : 41245059
Phone: +46 (0)31 405242 Cell: +46 (0)739 169191
GPG key: http://www.strakt.com/~martin/gpg.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 248 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20010705/51a8a6b3/attachment.sig>
More information about the Python-list
mailing list