class documentation

class LimitedStream(io.IOBase):

View In Hierarchy

Wraps a stream so that it doesn't read more than n bytes. If the stream is exhausted and the caller tries to get more bytes from it on_exhausted is called which by default returns an empty string. The return value of that function is forwarded to the reader function. So if it returns an empty string read will return an empty string as well.

The limit however must never be higher than what the stream can output. Otherwise readlines will try to read past the limit.

Note on WSGI compliance

calls to readline and readlines are not WSGI compliant because it passes a size argument to the readline methods. Unfortunately the WSGI PEP is not safely implementable without a size argument to readline because there is no EOF marker in the stream. As a result of that the use of readline is discouraged.

For the same reason iterating over the LimitedStream is not portable. It internally calls readline.

We strongly suggest using read only or using the make_line_iter which safely iterates line-based over a WSGI input stream.

Parameters
streamthe stream to wrap.
limitthe limit for the stream, must not be longer than what the string can provide if the stream does not end with EOF (like wsgi.input)
Method __init__ Undocumented
Method __iter__ Undocumented
Method __next__ Undocumented
Method exhaust Exhaust the stream. This consumes all the data left until the limit is reached.
Method on​_disconnect No summary
Method on​_exhausted This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function.
Method read Read size bytes or if size is not provided everything is read.
Method readable Undocumented
Method readline Reads one line from the stream.
Method readlines No summary
Method tell Returns the position of the stream.
Instance Variable limit Undocumented
Property is​_exhausted If the stream is exhausted this attribute is True.
Instance Variable ​_pos Undocumented
Instance Variable ​_read Undocumented
Instance Variable ​_readline Undocumented
def __init__(self, stream, limit):

Undocumented

Parameters
stream:t.IO[bytes]Undocumented
limit:intUndocumented
def __iter__(self):

Undocumented

Returns
LimitedStreamUndocumented
def __next__(self):

Undocumented

Returns
bytesUndocumented
def exhaust(self, chunk_size=1024*64):
Exhaust the stream. This consumes all the data left until the limit is reached.
Parameters
chunk​_size:intthe size for a chunk. It will read the chunk until the stream is exhausted and throw away the results.
def on_disconnect(self):
What should happen if a disconnect is detected? The return value of this function is returned from read functions in case the client went away. By default a ~werkzeug.exceptions.ClientDisconnected exception is raised.
Returns
bytesUndocumented
def on_exhausted(self):
This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function.
Returns
bytesUndocumented
def read(self, size=None):
Read size bytes or if size is not provided everything is read.
Parameters
size:t.Optional[int]the number of bytes read.
Returns
bytesUndocumented
def readable(self):

Undocumented

Returns
boolUndocumented
def readline(self, size=None):
Reads one line from the stream.
Parameters
size:t.Optional[int]Undocumented
Returns
bytesUndocumented
def readlines(self, size=None):
Reads a file into a list of strings. It calls readline until the file is read to the end. It does support the optional size argument if the underlying stream supports it for readline.
Parameters
size:t.Optional[int]Undocumented
Returns
t.List[bytes]Undocumented
def tell(self):

Returns the position of the stream.

New in version 0.9.
Returns
intUndocumented
limit =

Undocumented

@property
is_exhausted: bool =
If the stream is exhausted this attribute is True.
_pos: int =

Undocumented

_read =

Undocumented

_readline =

Undocumented