dwxw
dwxw

Reputation: 1099

How to use a generator to split socket read data on newlines

I have a simple generator that reads data from a socket and yields each chunk of data as it is received.

while True:
    data = s.recv(512)
    if not data:
        break
    yield data

The data looks like a csv file and so contains newlines. How can I change my code to yield the lines of text instead of the buffer size? I've played by with split('\n'), but always get stuck on how to detect the fact that last chunk might not be a complete line and I need to wait for the next chunk of data.

Thanks.

Upvotes: 5

Views: 2948

Answers (1)

jfs
jfs

Reputation: 414395

You could call socket.makefile() and then work with a file object (either iterate over lines or call .readline()):

import socket
from contextlib import closing

with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
    s.connect(("127.0.0.1", 8080))
    s.sendall("GET / HTTP/1.0\r\n\r\n")

    with closing(s.makefile()) as f: #NOTE: closed independently
        for line in f:
            print line,

    s.shutdown(socket.SHUT_WR)

Upvotes: 5

Related Questions