Reading from asyncio StreamReader

You should check if StreamReader.read returned an empty bytes object to signal an EOF:

data = bytearray()
while True:
    chunk = yield from reader.read(100)
    if not chunk:
        break
    data += chunk

Also, consider using aiohttp if you need a fully functional HTTP client.


Like so:

empty_bytes = b''
result = empty_bytes

while True:
    chunk = await response.content.read(8)

    if chunk == empty_bytes:
        break

    result += chunk

To determine the EOF use

if chunk == empty_bytes:

in stead of

if not chunk:

See the docs (aiohttp): the read returns an empty byte string

b''

on EOF, so check for that explicitly.

Note: If you would like to read until the end of the chunk as it was delivered from the server, checkout

StreamReader.readchunk()

(Didn't test it, though.)