WSGI Helpers

The following classes and functions are designed to make working withthe WSGI specification easier or operate on the WSGI layer. All thefunctionality from this module is available on the high-levelRequest / Response classes.

Iterator / Stream Helpers

These classes and functions simplify working with the WSGI applicationiterator and the input stream.

  • class werkzeug.wsgi.ClosingIterator(iterable, callbacks=None)
  • The WSGI specification requires that all middlewares and gatewaysrespect the close callback of the iterable returned by the application.Because it is useful to add another close action to a returned iterableand adding a custom iterable is a boring task this class can be used forthat:
  1. return ClosingIterator(app(environ, start_response), [cleanup_session,
  2. cleanup_locals])

If there is just one close function it can be passed instead of the list.

A closing iterator is not needed if the application uses response objectsand finishes the processing if the response is started:

  1. try:
  2. return response(environ, start_response)
  3. finally:
  4. cleanup_session()
  5. cleanup_locals()
  • class werkzeug.wsgi.FileWrapper(file, buffer_size=8192)
  • This class can be used to convert a file-like object intoan iterable. It yields buffer_size blocks until the file is fullyread.

You should not use this class directly but rather use thewrap_file() function that uses the WSGI server’s file wrappersupport if it’s available.

New in version 0.5.

If you’re using this object together with a BaseResponse you haveto use the direct_passthrough mode.

Parameters:

  • file – a file-like object with a read() method.
  • buffer_size – number of bytes for one iteration.
  • class werkzeug.wsgi.LimitedStream(stream, limit)
  • Wraps a stream so that it doesn’t read more than n bytes. If thestream is exhausted and the caller tries to get more bytes from iton_exhausted() is called which by default returns an emptystring. The return value of that function is forwardedto the reader function. So if it returns an empty stringread() will return an empty string as well.

The limit however must never be higher than what the stream canoutput. Otherwise readlines() will try to read past thelimit.

Note on WSGI compliance

calls to readline() and readlines() are notWSGI compliant because it passes a size argument to thereadline methods. Unfortunately the WSGI PEP is not safelyimplementable without a size argument to readline()because there is no EOF marker in the stream. As a resultof that the use of readline() is discouraged.

For the same reason iterating over the LimitedStreamis not portable. It internally calls readline().

We strongly suggest using read() only or using themake_line_iter() which safely iterates line-basedover a WSGI input stream.

Parameters:

  • stream – the stream to wrap.
  • limit – the limit for the stream, must not be longer thanwhat the string can provide if the stream does notend with EOF (like wsgi.input)
  • exhaust(chunk_size=65536)
  • Exhaust the stream. This consumes all the data left until thelimit is reached.

Parameters:chunk_size – the size for a chunk. It will read the chunkuntil the stream is exhausted and throw awaythe results.

  • is_exhausted
  • If the stream is exhausted this attribute is True.

  • on_disconnect()

  • What should happen if a disconnect is detected? The returnvalue of this function is returned from read functions in casethe client went away. By default aClientDisconnected exception is raised.

  • on_exhausted()

  • This is called when the stream tries to read past the limit.The return value of this function is returned from the readingfunction.

  • read(size=None)

  • Read size bytes or if size is not provided everything is read.

Parameters:size – the number of bytes read.

  • readable()
  • Return whether object was opened for reading.

If False, read() will raise OSError.

  • readline(size=None)
  • Reads one line from the stream.

  • readlines(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 optionalsize argument if the underlaying stream supports it forreadline.

  • tell()

  • Returns the position of the stream.

New in version 0.9.

  • werkzeug.wsgi.makeline_iter(_stream, limit=None, buffer_size=10240, cap_at_buffer=False)
  • Safely iterates line-based over an input stream. If the input streamis not a LimitedStream the limit parameter is mandatory.

This uses the stream’s read() method internally as oppositeto the readline() method that is unsafe and can only be usedin violation of the WSGI specification. The same problem applies to theiter function of the input stream which calls readline()without arguments.

If you need line-by-line processing it’s strongly recommended to iterateover 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 – the stream or iterate to iterate over.
  • limit – the limit in bytes for the stream. (Usuallycontent length. Not necessary if the _stream_is a LimitedStream.
  • buffer_size – The optional buffer size.
  • cap_at_buffer – if this is set chunks are split if they are longerthan the buffer size. Internally this is implementedthat the buffer size might be exhausted by a factorof two however.
  • werkzeug.wsgi.makechunk_iter(_stream, separator, limit=None, buffer_size=10240, cap_at_buffer=False)
  • Works like make_line_iter() but accepts a separatorwhich divides chunks. If you want newline based processingyou should use make_line_iter() instead as itsupports 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 – the stream or iterate to iterate over.
  • separator – the separator that divides chunks.
  • limit – the limit in bytes for the stream. (Usuallycontent length. Not necessary if the _stream_is otherwise already limited).
  • buffer_size – The optional buffer size.
  • cap_at_buffer – if this is set chunks are split if they are longerthan the buffer size. Internally this is implementedthat the buffer size might be exhausted by a factorof two however.
  • werkzeug.wsgi.wrapfile(_environ, file, buffer_size=8192)
  • Wraps a file. This uses the WSGI server’s file wrapper if availableor otherwise the generic FileWrapper.

New in version 0.5.

If the file wrapper from the WSGI server is used it’s important to notiterate over it from inside the application but to pass it throughunchanged. If you want to pass out a file wrapper inside a responseobject you have to set directpassthrough to _True.

More information about file wrappers are available in PEP 333.

Parameters:

  • file – a file-like object with a read() method.
  • buffer_size – number of bytes for one iteration.

Environ Helpers

These functions operate on the WSGI environment. They extract usefulinformation or perform common manipulations:

  • werkzeug.wsgi.gethost(_environ, trusted_hosts=None)
  • Return the host for the given WSGI environment. This first checksthe Host header. If it’s not present, then SERVER_NAME andSERVER_PORT are used. The host will only contain the port if itis different than the standard port for the protocol.

Optionally, verify that the host is trusted usinghost_is_trusted() and raise aSecurityError if it is not.

Parameters:

  • environ – The WSGI environment to get the host from.
  • trusted_hosts – A list of trusted hosts.Returns:Host, with port if necessary.Raises:SecurityError – If the host is nottrusted.
  • werkzeug.wsgi.getcontent_length(_environ)
  • Returns the content length from the WSGI environment asinteger. If it’s not available or chunked transfer encoding is used,None is returned.

New in version 0.9.

Parameters:environ – the WSGI environ to fetch the content length from.

  • werkzeug.wsgi.getinput_stream(_environ, safe_fallback=True)
  • Returns the input stream from the WSGI environment and wraps itin the most sensible way possible. The stream returned is not theraw WSGI stream in most cases but one that is safe to read fromwithout 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 setthe wsgi.input_terminated value in the WSGI environ to indicate that.

New in version 0.9.

Parameters:

  • environ – the WSGI environ to fetch the stream from.
  • safe_fallback – use an empty stream as a safe fallback when thecontent length is not set. Disabling this allows infinite streams,which can be a denial-of-service risk.
  • werkzeug.wsgi.getcurrent_url(_environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None)
  • A handy helper function that recreates the full URL as IRI for thecurrent request or parts of it. Here’s an example:
  1. >>> from werkzeug.test import create_environ
  2. >>> env = create_environ("/?param=foo", "http://localhost/script")
  3. >>> get_current_url(env)
  4. 'http://localhost/script/?param=foo'
  5. >>> get_current_url(env, root_only=True)
  6. 'http://localhost/script/'
  7. >>> get_current_url(env, host_only=True)
  8. 'http://localhost/'
  9. >>> get_current_url(env, strip_querystring=True)
  10. 'http://localhost/script/'

This optionally it verifies that the host is in a list of trusted hosts.If the host is not in there it will raise aSecurityError.

Note that the string returned might contain unicode characters as therepresentation is an IRI not an URI. If you need an ASCII onlyrepresentation you can use the iri_to_uri()function:

  1. >>> from werkzeug.urls import iri_to_uri
  2. >>> iri_to_uri(get_current_url(env))
  3. 'http://localhost/script/?param=foo'

Parameters:

  • environ – the WSGI environment to get the current URL from.
  • root_only – set True if you only want the root URL.
  • strip_querystring – set to True if you don’t want the querystring.
  • host_only – set to True if the host URL should be returned.
  • trusted_hosts – a list of trusted hosts, see host_is_trusted()for more information.
  • werkzeug.wsgi.getquery_string(_environ)
  • Returns the QUERY_STRING from the WSGI environment. This also takescare about the WSGI decoding dance on Python 3 environments as anative string. The string returned will be restricted to ASCIIcharacters.

New in version 0.9.

Parameters:environ – the WSGI environment object to get the query string from.

  • werkzeug.wsgi.getscript_name(_environ, charset='utf-8', errors='replace')
  • Returns the SCRIPT_NAME from the WSGI environment and properlydecodes it. This also takes care about the WSGI decoding danceon Python 3 environments. if the charset is set to None abytestring is returned.

New in version 0.9.

Parameters:

  • environ – the WSGI environment object to get the path from.
  • charset – the charset for the path, or None if nodecoding should be performed.
  • errors – the decoding error handling.
  • werkzeug.wsgi.getpath_info(_environ, charset='utf-8', errors='replace')
  • Returns the PATH_INFO from the WSGI environment and properlydecodes it. This also takes care about the WSGI decoding danceon Python 3 environments. if the charset is set to None abytestring is returned.

New in version 0.9.

Parameters:

  • environ – the WSGI environment object to get the path from.
  • charset – the charset for the path info, or None if nodecoding should be performed.
  • errors – the decoding error handling.
  • werkzeug.wsgi.poppath_info(_environ, charset='utf-8', errors='replace')
  • Removes and returns the next segment of PATH_INFO, pushing it ontoSCRIPT_NAME. Returns None if there is nothing left on PATH_INFO.

If the charset is set to None a bytestring is returned.

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

  1. >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
  2. >>> pop_path_info(env)
  3. 'a'
  4. >>> env['SCRIPT_NAME']
  5. '/foo/a'
  6. >>> pop_path_info(env)
  7. 'b'
  8. >>> env['SCRIPT_NAME']
  9. '/foo/a/b'

New in version 0.5.

Changed in version 0.9: The path is now decoded and a charset and encodingparameter can be provided.

Parameters:environ – the WSGI environment that is modified.

  • werkzeug.wsgi.peekpath_info(_environ, charset='utf-8', errors='replace')
  • Returns the next segment on the PATH_INFO or None if thereis none. Works like pop_path_info() without modifying theenvironment:
  1. >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
  2. >>> peek_path_info(env)
  3. 'a'
  4. >>> peek_path_info(env)
  5. 'a'

If the charset is set to None a bytestring is returned.

New in version 0.5.

Changed in version 0.9: The path is now decoded and a charset and encodingparameter can be provided.

Parameters:environ – the WSGI environment that is checked.

  • werkzeug.wsgi.extractpath_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) andpath. The path info returned is a unicode string, not a bytestringsuitable for a WSGI environment. The URLs might also be IRIs.

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

Some examples:

  1. >>> extract_path_info('http://example.com/app', '/app/hello')
  2. u'/hello'
  3. >>> extract_path_info('http://example.com/app',
  4. ... 'https://example.com/app/hello')
  5. u'/hello'
  6. >>> extract_path_info('http://example.com/app',
  7. ... 'https://example.com/app/hello',
  8. ... collapse_http_schemes=False) is None
  9. True

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

Parameters:

  • environ_or_baseurl – a WSGI environment dict, a base URL orbase IRI. This is the root of theapplication.
  • path_or_url – an absolute path from the server root, arelative path (in which case it’s the path info)or a full URL. Also accepts IRIs and unicodeparameters.
  • charset – the charset for byte data in URLs
  • errors – the error handling on decode
  • collapse_http_schemes – if set to False the algorithm doesnot assume that http and https on thesame server point to the sameresource.

Changed in version 0.15: The errors parameter defaults to leaving invalid bytesquoted instead of replacing them.

New in version 0.6.

  • werkzeug.wsgi.hostis_trusted(_hostname, trusted_list)
  • Checks if a host is trusted against a list. This also takes careof port normalization.

New in version 0.9.

Parameters:

  • hostname – the hostname to check
  • trusted_list – a list of hostnames to check against. If ahostname starts with a dot it will match againstall subdomains as well.

Convenience Helpers

  • werkzeug.wsgi.responder(f)
  • Marks a function as responder. Decorate a function with it and itwill automatically call the return value as WSGI application.

Example:

  1. @responderdef application(environ, start_response): return Response('Hello World!')

  • werkzeug.testapp.testapp(_environ, start_response)
  • Simple test application that dumps the environment. You can useit to check if Werkzeug is working properly:
  1. >>> from werkzeug.serving import run_simple
  2. >>> from werkzeug.testapp import test_app
  3. >>> run_simple('localhost', 3000, test_app)
  4. * Running on http://localhost:3000/

The application displays important information from the WSGI environment,the Python interpreter and the installed libraries.

Bytes, Strings, and Encodings

The WSGI environment on Python 3 works slightly different than it doeson Python 2. Werkzeug hides the differences from you if you use thehigher level APIs.

The WSGI specification (PEP 3333) decided to always use the nativestr type. On Python 2 this means the raw bytes are passed throughand can be decoded directly. On Python 3, however, the raw bytes arealways decoded using the ISO-8859-1 charset to produce a Unicode string.

Python 3 Unicode strings in the WSGI environment are restricted toISO-8859-1 code points. If a string read from the environment mightcontain characters outside that charset, it must first be decoded tobytes as ISO-8859-1, then encoded to a Unicode string using the propercharset (typically UTF-8). The reverse is done when writing to theenviron. This is known as the “WSGI encoding dance”.

Werkzeug provides functions to deal with this automatically so that youdon’t need to be aware of the inner workings. Use the functions on thispage as well as EnvironHeaders() to readdata out of the WSGI environment.

Applications should avoid manually creating or modifying a WSGIenvironment unless they take care of the proper encoding or decodingstep. All high level interfaces in Werkzeug will apply the encoding anddecoding as necessary.

Raw Request URI and Path Encoding

The PATH_INFO in the environ is the path value afterpercent-decoding. For example, the raw path /hello%2fworld wouldshow up from the WSGI server to Werkzeug as /hello/world. This losesthe information that the slash was a raw character as opposed to a pathseparator.

The WSGI specification (PEP 3333) does not provide a way to get theoriginal value, so it is impossible to route some types of data in thepath. The most compatible way to work around this is to send problematicdata in the query string instead of the path.

However, many WSGI servers add a non-standard environ key with the rawpath. To match this behavior, Werkzeug’s test client and developmentserver will add the raw value to both the REQUEST_URI andRAW_URI keys. If you want to route based on this value, you can usemiddleware to replace PATH_INFO in the environ before it reaches theapplication. However, keep in mind that these keys are non-standard andnot guaranteed to be present.