module documentation

Undocumented

Class ​Closing​Iterator No summary
Class ​File​Wrapper This class can be used to convert a file-like object into an iterable. It yields buffer_size blocks until the file is fully read.
Class ​Limited​Stream No summary
Function extract​_path​_info Extracts the path info from the given URL (or WSGI environment) and path. The path info returned is a string. The URLs might also be IRIs.
Function get​_content​_length Returns the content length from the WSGI environment as integer. If it's not available or chunked transfer encoding is used, None is returned.
Function get​_current​_url Recreate the URL for a request from the parts in a WSGI environment.
Function get​_host Return the host for the given WSGI environment.
Function get​_input​_stream No summary
Function get​_path​_info Return the PATH_INFO from the WSGI environment and decode it unless charset is None.
Function get​_query​_string Returns the QUERY_STRING from the WSGI environment. This also takes care of the WSGI decoding dance. The string returned will be restricted to ASCII characters.
Function get​_script​_name Return the SCRIPT_NAME from the WSGI environment and decode it unless charset is set to None.
Function make​_chunk​_iter No summary
Function make​_line​_iter Safely iterates line-based over an input stream. If the input stream is not a LimitedStream the limit parameter is mandatory.
Function peek​_path​_info Returns the next segment on the PATH_INFO or None if there is none. Works like pop_path_info without modifying the environment:
Function pop​_path​_info Removes and returns the next segment of PATH_INFO, pushing it onto SCRIPT_NAME. Returns None if there is nothing left on PATH_INFO.
Function responder Marks a function as responder. Decorate a function with it and it will automatically call the return value as WSGI application.
Function wrap​_file Wraps a file. This uses the WSGI server's file wrapper if available or otherwise the generic FileWrapper.
Class _​Range​Wrapper No summary
Function ​_get​_server Undocumented
Function ​_make​_chunk​_iter Helper for the line and chunk iter functions.
def extract_path_info(environ_or_baseurl, path_or_url, charset='utf-8', errors='werkzeug.url_quote', collapse_http_schemes=True):

Extracts the path info from the given URL (or WSGI environment) and path. The path info returned is a string. The URLs might also be IRIs.

If the path info could not be determined, None is returned.

Some examples:

>>> extract_path_info('http://example.com/app', '/app/hello')
'/hello'
>>> extract_path_info('http://example.com/app',
...                   'https://example.com/app/hello')
'/hello'
>>> extract_path_info('http://example.com/app',
...                   'https://example.com/app/hello',
...                   collapse_http_schemes=False) is None
True

Instead of providing a base URL you can also pass a WSGI environment.

Changed in version 0.15: The errors parameter defaults to leaving invalid bytes quoted instead of replacing them.
New in version 0.6.
Parameters
environ​_or​_baseurl:t.Union[str, WSGIEnvironment]a WSGI environment dict, a base URL or base IRI. This is the root of the application.
path​_or​_url:t.Union[str, _URLTuple]an absolute path from the server root, a relative path (in which case it's the path info) or a full URL.
charset:strthe charset for byte data in URLs
errors:strthe error handling on decode
collapse​_http​_schemes:boolif set to False the algorithm does not assume that http and https on the same server point to the same resource.
Returns
t.Optional[str]Undocumented
def get_content_length(environ):

Returns the content length from the WSGI environment as integer. If it's not available or chunked transfer encoding is used, None is returned.

New in version 0.9.
Parameters
environ:WSGIEnvironmentthe WSGI environ to fetch the content length from.
Returns
t.Optional[int]Undocumented
def get_current_url(environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None):

Recreate the URL for a request from the parts in a WSGI environment.

The URL is an IRI, not a URI, so it may contain Unicode characters. Use ~werkzeug.urls.iri_to_uri to convert it to ASCII.

Parameters
environ:WSGIEnvironmentThe WSGI environment to get the URL parts from.
root​_only:boolOnly build the root path, don't include the remaining path or query string.
strip​_querystring:boolDon't include the query string.
host​_only:boolOnly build the scheme and host.
trusted​_hosts:t.Optional[t.Iterable[str]]A list of trusted host names to validate the host against.
Returns
strUndocumented
def get_host(environ, trusted_hosts=None):

Return the host for the given WSGI environment.

The Host header is preferred, then SERVER_NAME if it's not set. The returned host will only contain the port if it is different than the standard port for the protocol.

Optionally, verify that the host is trusted using host_is_trusted and raise a ~werkzeug.exceptions.SecurityError if it is not.

Parameters
environ:WSGIEnvironmentA WSGI environment dict.
trusted​_hosts:t.Optional[t.Iterable[str]]A list of trusted host names.
Returns
strHost, with port if necessary.
Raises
~werkzeug.exceptions.SecurityErrorIf the host is not trusted.
def get_input_stream(environ, safe_fallback=True):

Returns the input stream from the WSGI environment and wraps it in the most sensible way possible. The stream returned is not the raw WSGI stream in most cases but one that is safe to read from without taking into account the content length.

If content length is not set, the stream will be empty for safety reasons. If the WSGI server supports chunked or infinite streams, it should set the wsgi.input_terminated value in the WSGI environ to indicate that.

New in version 0.9.
Parameters
environ:WSGIEnvironmentthe WSGI environ to fetch the stream from.
safe​_fallback:booluse an empty stream as a safe fallback when the content length is not set. Disabling this allows infinite streams, which can be a denial-of-service risk.
Returns
t.IO[bytes]Undocumented
def get_path_info(environ, charset='utf-8', errors='replace'):

Return the PATH_INFO from the WSGI environment and decode it unless charset is None.

New in version 0.9.
Parameters
environ:WSGIEnvironmentWSGI environment to get the path from.
charset:strThe charset for the path info, or None if no decoding should be performed.
errors:strThe decoding error handling.
Returns
strUndocumented
def get_query_string(environ):

Returns the QUERY_STRING from the WSGI environment. This also takes care of the WSGI decoding dance. The string returned will be restricted to ASCII characters.

New in version 0.9.
Parameters
environ:WSGIEnvironmentWSGI environment to get the query string from.
Returns
strUndocumented
def get_script_name(environ, charset='utf-8', errors='replace'):

Return the SCRIPT_NAME from the WSGI environment and decode it unless charset is set to None.

New in version 0.9.
Parameters
environ:WSGIEnvironmentWSGI environment to get the path from.
charset:strThe charset for the path, or None if no decoding should be performed.
errors:strThe decoding error handling.
Returns
strUndocumented
def make_chunk_iter(stream, separator, limit=None, buffer_size=10*1024, cap_at_buffer=False):

Works like make_line_iter but accepts a separator which divides chunks. If you want newline based processing you should use make_line_iter instead as it supports arbitrary newline markers.

New in version 0.8.
New in version 0.9: added support for iterators as input stream.
New in version 0.11.10: added support for the cap_at_buffer parameter.
Parameters
stream:t.Union[t.Iterable[bytes], t.IO[bytes]]the stream or iterate to iterate over.
separator:bytesthe separator that divides chunks.
limit:t.Optional[int]the limit in bytes for the stream. (Usually content length. Not necessary if the stream is otherwise already limited).
buffer​_size:intThe optional buffer size.
cap​_at​_buffer:boolif this is set chunks are split if they are longer than the buffer size. Internally this is implemented that the buffer size might be exhausted by a factor of two however.
Returns
t.Iterator[bytes]Undocumented
def make_line_iter(stream, limit=None, buffer_size=10*1024, cap_at_buffer=False):

Safely iterates line-based over an input stream. If the input stream is not a LimitedStream the limit parameter is mandatory.

This uses the stream's ~file.read method internally as opposite to the ~file.readline method that is unsafe and can only be used in violation of the WSGI specification. The same problem applies to the __iter__ function of the input stream which calls ~file.readline without arguments.

If you need line-by-line processing it's strongly recommended to iterate over the input stream using this helper function.

Changed in version 0.8: This function now ensures that the limit was reached.
New in version 0.9: added support for iterators as input stream.
New in version 0.11.10: added support for the cap_at_buffer parameter.
Parameters
stream:t.Union[t.Iterable[bytes], t.IO[bytes]]the stream or iterate to iterate over.
limit:t.Optional[int]the limit in bytes for the stream. (Usually content length. Not necessary if the stream is a LimitedStream.
buffer​_size:intThe optional buffer size.
cap​_at​_buffer:boolif this is set chunks are split if they are longer than the buffer size. Internally this is implemented that the buffer size might be exhausted by a factor of two however.
Returns
t.Iterator[bytes]Undocumented
def peek_path_info(environ, charset='utf-8', errors='replace'):

Returns the next segment on the PATH_INFO or None if there is none. Works like pop_path_info without modifying the environment:

>>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
>>> peek_path_info(env)
'a'
>>> peek_path_info(env)
'a'

If the charset is set to None bytes are returned.

New in version 0.5.
Changed in version 0.9: The path is now decoded and a charset and encoding parameter can be provided.
Parameters
environ:WSGIEnvironmentthe WSGI environment that is checked.
charset:strUndocumented
errors:strUndocumented
Returns
t.Optional[str]Undocumented
def pop_path_info(environ, charset='utf-8', errors='replace'):

Removes and returns the next segment of PATH_INFO, pushing it onto SCRIPT_NAME. Returns None if there is nothing left on PATH_INFO.

If the charset is set to None bytes are returned.

If there are empty segments ('/foo//bar) these are ignored but properly pushed to the SCRIPT_NAME:

>>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
>>> pop_path_info(env)
'a'
>>> env['SCRIPT_NAME']
'/foo/a'
>>> pop_path_info(env)
'b'
>>> env['SCRIPT_NAME']
'/foo/a/b'
New in version 0.5.
Changed in version 0.9: The path is now decoded and a charset and encoding parameter can be provided.
Parameters
environ:WSGIEnvironmentthe WSGI environment that is modified.
charset:strThe encoding parameter passed to bytes.decode.
errors:strThe errors paramater passed to bytes.decode.
Returns
t.Optional[str]Undocumented
def responder(f):

Marks a function as responder. Decorate a function with it and it will automatically call the return value as WSGI application.

Example:

@responder
def application(environ, start_response):
    return Response('Hello World!')
Parameters
f:t.Callable[..., WSGIApplication]Undocumented
Returns
WSGIApplicationUndocumented
def wrap_file(environ, file, buffer_size=8192):

Wraps a file. This uses the WSGI server's file wrapper if available or otherwise the generic FileWrapper.

New in version 0.5.

If the file wrapper from the WSGI server is used it's important to not iterate over it from inside the application but to pass it through unchanged. If you want to pass out a file wrapper inside a response object you have to set Response.direct_passthrough to True.

More information about file wrappers are available in PEP 333.

Parameters
environ:WSGIEnvironmentUndocumented
file:t.IO[bytes]a file-like object with a ~file.read method.
buffer​_size:intnumber of bytes for one iteration.
Returns
t.Iterable[bytes]Undocumented
def _get_server(environ):

Undocumented

Parameters
environ:WSGIEnvironmentUndocumented
Returns
t.Optional[t.Tuple[str, t.Optional[int]]]Undocumented
def _make_chunk_iter(stream, limit, buffer_size):
Helper for the line and chunk iter functions.
Parameters
stream:t.Union[t.Iterable[bytes], t.IO[bytes]]Undocumented
limit:t.Optional[int]Undocumented
buffer​_size:intUndocumented
Returns
t.Iterator[bytes]Undocumented