readlines() on a socket

Skip Montanaro skip at pobox.com
Wed Oct 24 00:52:59 EDT 2001


    Gregory> I'd like to be able to do
    Gregory>    s = socket.socket(...)
    Gregory>    s = socket.connect(....)
    Gregory>    for l in s.readlines():
    Gregory>            # process line l

    Gregory> but sockets don't support readline(), or readlines(), or
    Gregory> anything similar that I can see.

Socket objects have a makefile() method that will return a file object.

    s = socket.socket(...)
    socket.connect(....)
    f = s.makefile()
    for l in f.readlines():
        process_lines()

(Also, note that you should not assign the result of the connect method.  I
believe it simply returns None.)

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list