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 :func:`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 :meth:`read` will return an empty string as well. The limit however must never be higher than what the stream can output. Otherwise :meth:`readlines` will try to read past the limit. .. admonition:: Note on WSGI compliance calls to :meth:`readline` and :meth:`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 :meth:`readline` because there is no EOF marker in the stream. As a result of that the use of :meth:`readline` is discouraged. For the same reason iterating over the :class:`LimitedStream` is not portable. It internally calls :meth:`readline`. We strongly suggest using :meth:`read` only or using the :func:`make_line_iter` which safely iterates line-based over a WSGI input stream. :param stream: the stream to wrap. :param limit: the 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 by reading until the limit is reached or the client disconnects, discarding the data. |
Method | on |
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 :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised. |
Method | on |
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 up to ``size`` bytes from the underlying stream. If size is not provided, read until the limit. |
Method | readable |
Undocumented |
Method | readline |
Reads one line from the stream. |
Method | readlines |
Reads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlying stream supports it for `readline`. |
Method | tell |
Returns the position of the stream. |
Instance Variable | limit |
Undocumented |
Property | is |
If the stream is exhausted this attribute is `True`. |
Method | _exhaust |
Exhaust the stream by reading until the limit is reached or the client disconnects, yielding each chunk. |
Instance Variable | _pos |
Undocumented |
Instance Variable | _read |
Undocumented |
Instance Variable | _readline |
Undocumented |
Exhaust the stream by reading until the limit is reached or the client disconnects, discarding the data. :param chunk_size: How many bytes to read at a time. .. versionchanged:: 2.2.3 Handle case where wrapped stream returns fewer bytes than requested.
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 :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised.
This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function.
Read up to ``size`` bytes from the underlying stream. If size is not provided, read until the limit. If the limit is reached, :meth:`on_exhausted` is called, which returns empty bytes. If no bytes are read and the limit is not reached, or if an error occurs during the read, :meth:`on_disconnect` is called, which raises :exc:`.ClientDisconnected`. :param size: The number of bytes to read. ``None``, default, reads until the limit is reached. .. versionchanged:: 2.2.3 Handle case where wrapped stream returns fewer bytes than requested.
Reads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlying stream supports it for `readline`.