class werkzeug.LimitedStream(stream, limit)[source]

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.

  • stream – the stream to wrap.
  • 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)


__init__(stream, limit)
exhaust([chunk_size]) Exhaust the stream.
on_disconnect() 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.
on_exhausted() This is called when the stream tries to read past the limit.
read([size]) Read size bytes or if size is not provided everything is read.
readline([size]) Reads one line from the stream.
readlines([size]) Reads a file into a list of strings.
tell() Returns the position of the stream.


is_exhausted If the stream is exhausted this attribute is True.