Undocumented
Class | ClosingIterator |
No summary |
Class | FileWrapper |
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 | LimitedStream |
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 | _RangeWrapper |
No summary |
Function | _get_server |
Undocumented |
Function | _make_chunk_iter |
Helper for the line and chunk iter functions. |
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.
Parameters | |
environ_or_baseurl:t.Union[ | a WSGI environment dict, a base URL or base IRI. This is the root of the application. |
path_or_url:t.Union[ | an absolute path from the server root, a relative path (in which case it's the path info) or a full URL. |
charset:str | the charset for byte data in URLs |
errors:str | the error handling on decode |
collapse_http_schemes:bool | if set to False the algorithm does
not assume that http and https on the
same server point to the same
resource. |
Returns | |
t.Optional[ | Undocumented |
Returns the content length from the WSGI environment as integer. If it's not available or chunked transfer encoding is used, None is returned.
Parameters | |
environ:WSGIEnvironment | the WSGI environ to fetch the content length from. |
Returns | |
t.Optional[ | Undocumented |
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:WSGIEnvironment | The WSGI environment to get the URL parts from. |
root_only:bool | Only build the root path, don't include the remaining path or query string. |
strip_querystring:bool | Don't include the query string. |
host_only:bool | Only build the scheme and host. |
trusted_hosts:t.Optional[ | A list of trusted host names to validate the host against. |
Returns | |
str | Undocumented |
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:WSGIEnvironment | A WSGI environment dict. |
trusted_hosts:t.Optional[ | A list of trusted host names. |
Returns | |
str | Host, with port if necessary. |
Raises | |
~werkzeug.exceptions.SecurityError | If the host is not trusted. |
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.
Parameters | |
environ:WSGIEnvironment | the WSGI environ to fetch the stream from. |
safe_fallback:bool | use 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[ | Undocumented |
Return the PATH_INFO from the WSGI environment and decode it unless charset is None.
Parameters | |
environ:WSGIEnvironment | WSGI environment to get the path from. |
charset:str | The charset for the path info, or None if no decoding should be performed. |
errors:str | The decoding error handling. |
Returns | |
str | Undocumented |
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.
Parameters | |
environ:WSGIEnvironment | WSGI environment to get the query string from. |
Returns | |
str | Undocumented |
Return the SCRIPT_NAME from the WSGI environment and decode
it unless charset
is set to None.
Parameters | |
environ:WSGIEnvironment | WSGI environment to get the path from. |
charset:str | The charset for the path, or None if no decoding should be performed. |
errors:str | The decoding error handling. |
Returns | |
str | Undocumented |
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.
cap_at_buffer
parameter.Parameters | |
stream:t.Union[ | the stream or iterate to iterate over. |
separator:bytes | the separator that divides chunks. |
limit:t.Optional[ | the limit in bytes for the stream. (Usually
content length. Not necessary if the stream
is otherwise already limited). |
buffer_size:int | The optional buffer size. |
cap_at_buffer:bool | if 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[ | Undocumented |
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.
cap_at_buffer
parameter.Parameters | |
stream:t.Union[ | the stream or iterate to iterate over. |
limit:t.Optional[ | the limit in bytes for the stream. (Usually
content length. Not necessary if the stream
is a LimitedStream . |
buffer_size:int | The optional buffer size. |
cap_at_buffer:bool | if 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[ | Undocumented |
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.
Parameters | |
environ:WSGIEnvironment | the WSGI environment that is checked. |
charset:str | Undocumented |
errors:str | Undocumented |
Returns | |
t.Optional[ | Undocumented |
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'
Parameters | |
environ:WSGIEnvironment | the WSGI environment that is modified. |
charset:str | The encoding parameter passed to
bytes.decode . |
errors:str | The errors paramater passed to
bytes.decode . |
Returns | |
t.Optional[ | Undocumented |
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[ | Undocumented |
Returns | |
WSGIApplication | Undocumented |
Wraps a file. This uses the WSGI server's file wrapper if available
or otherwise the generic FileWrapper
.
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:WSGIEnvironment | Undocumented |
file:t.IO[ | a file -like object with a ~file.read method. |
buffer_size:int | number of bytes for one iteration. |
Returns | |
t.Iterable[ | Undocumented |