开发者

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

开发者 https://www.devze.com 2023-03-01 16:34 出处:网络
I have a simple generator that reads data from a socket and yields each chunk of data as it is received.

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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号