pending = None
while True:
    chunk = os.read(fd.fileno(), chunk_size)
if not chunk:
    break
if pending is not None:
    chunk = pending + chunk
pending = None
lines = chunk.splitlines()
if lines and lines[-1]:
    pending = lines.pop()
for line in lines:
    yield line
if pending:
    yield pending
Чтение потокового http-ответа с библиотекой запросов Python