Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sockets

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.

like image 991
dwxw Avatar asked May 01 '11 11:05

dwxw


1 Answers

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)
like image 196
jfs Avatar answered Sep 23 '22 19:09

jfs