Streaming API¶
aiohttp uses streams for retrieving BODIES:
aiohttp.web.BaseRequest.content and
aiohttp.ClientResponse.content are properties with stream API.
- class aiohttp.StreamReader¶
The reader from incoming stream.
User should never instantiate streams manually but use existing
aiohttp.web.BaseRequest.contentandaiohttp.ClientResponse.contentproperties for accessing raw BODY data.
Reading Methods¶
Asynchronous Iteration Support¶
Stream reader supports asynchronous iteration over BODY.
By default it iterates over lines:
async for line in response.content:
print(line)
Also there are methods for iterating over data chunks with maximum size limit and over any available data.
Helpers¶
- StreamReader.exception()¶
Get the exception occurred on data reading.
- aiohttp.is_eof()¶
Return
Trueif EOF was reached.Internal buffer may be not empty at the moment.
See also
- StreamReader.at_eof()¶
Return
Trueif the buffer is empty and EOF was reached.
- StreamReader.read_nowait(n=None)¶
Returns data from internal buffer if any, empty bytes object otherwise.
Raises
RuntimeErrorif other coroutine is waiting for stream.- Parameters
n (int) – how many bytes to read,
-1for the whole internal buffer.- Return bytes
the given data
- StreamReader.unread_data(data)¶
Rollback reading some data from stream, inserting it to buffer head.
- Parameters
data (bytes) – data to push back into the stream.
Warning
The method does not wake up waiters.
E.g.
read()will not be resumed.