Werkzeug comes with a bunch of utilties that can be useful for WSGI applications. Most of the classes provided by this module are used by the wrappers, you can however use them without the wrappers too.


A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.

MultiDict implements the all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values too you have to use the list methods as explained below.

You can construct a MultiDict from a list of (key, value) tuples, a dict or another MultiDict.

Basic Usage:

>>> from werkzeug import MultiDict
>>> d = MultiDict([('a', 'b'), ('a', 'c')])
>>> d
MultiDict([('a', 'b'), ('a', 'c')])
>>> d['a']
>>> d.getlist('a')
['b', 'c']
>>> 'a' in d

It behaves like a normal dict thus all dict functions will only return the first value when multiple values for one key are found.

The following additional functions exist that are used to get or set the other values for a key:

Return the list of items for a given key. If that key is not in the MultiDict, the return value will be an empty list.

Remove the old values for a key and add new ones. Note that the list you pass the values in will be shallow-copied before it is inserted in the dictionary.

>>> multidict.setlist('foo', ['1', '2'])
>>> multidict['foo']
>>> multidict.getlist('foo')
['1', '2']
Like setdefault but sets multiple values.
Like items but the values will be lists with all the values for a key.
Like values but it will return lists for all values instead of only the first value.
Like lists but returns an iterator.
Like itervalues but it will yield lists for all values instead of only the first value.
Like pop for normal dicts; pops all the values for a key and removes the key from the dict.
Like popitem for normal dicts; pops all the values for a key, removes the key from the dict and then returns a tuple in the form (key, values).

Also notable: update adds values and does not replace existing.

Because MultiDict usually hold use submitted data there you often have to convert incoming data to integers etc. The get and getlist functions accept an type argument which can point to a function that converts the value or raises an ValueError which is catched. This makes the following pieces of code roughly equivalent:

    page = int(multidict['key'])
except (ValueError, KeyError):
    page = default

page = multidict.get('key', type=int)


A read only MultiDict decorator that you can pass multiple MultiDict instances as sequence and it will combine the return values of all wrapped dicts:

>>> from werkzeug import MultiDict, CombinedMultiDict
>>> post = MultiDict([('foo', 'bar')])
>>> get = MultiDict([('blub', 'blah')])
>>> combined = CombinedMultiDict([get, post])
>>> combined['foo']
>>> combined['blub']

This works for all read operations and will raise a TypeError for methods that want to change data which isn't possible.


Another useful class is Headers. It behaves in a similar to a MultiDict but it stores the values in a different way. In fact it's not a dict at all, it's more a list because the values are stored both sorted and unhashed.

This data structure is useful if you want a nicer way to handle WSGI headers which are stored as tuples in a list.

Changes to MultiDict:

iterlistvalues and listvalues are missing because itervalues and values work completely different.

Add a new value to a key foo.add('Content-Type', 'text/plain')
yields (key, value) tuples.
set / __setitem__
remove all old keys and add a new value.
Convert the headers into a list and converts the unicode headers to the specified charset. The charset parameter is obligatory. (foo.to_list('utf-8'))
Return a list of allvalues for a given key
Add a list of values to a given key. Removes all old values for this key first.
Add a list of values to the headers for this key.
itervalues and similar methods...
...yield all values for a key, not only the first one! iterkeys can of corse yield multiple keys with the same name analog to that.


Works like Headers but is read only and constructed from a WSGI enviornment. It gives a nicer access to the client headers that become part of the WSGI environment. All methods that would modify the header list raise a TypeError.


The FileStorage object is a thin wrapper over incoming files. It has the following attributes and methods:

The name of the form field which represents the data.
The incoming filename
The mimetype of the file. For example 'text/html'.
content_length / len()
The expected length of the file.
Read bytes or all of the incoming file. Note that the default wrappers store the incoming data on the filesystem rather than the RAM. Thus you should keep in mind that reading without an maxmimum length can easily break your application if the user submitted data is too big.
readline() / readlines() / __iter__
Behave like the methods of any other file object.
Gives you access to the underlaying stream. Note that the exact methods of this stream and it's type is not strictly specified. In most situations it will be a TemporaryFile object.
save(dst[, buffer_size]<)
Because uploaded files inside a FileStorage are kept in temporary files without filenames you often have to copy the files over to a new location on the filesystem. This method is able to save the file content to a new stream or file on the filesystem (just pass it a filename in that case) without consuming all the memory. If you have more memory available you can raise the buffer size to speed up the process. The default buffer sizes is 16384 (16KB)


A WSGI middleware that provides static content for development environments or simple server setups. Usage is quite simple:

import os
from werkzeug import SharedDataMiddleware

app = SharedDataMiddleware(app, {
    '/shared': os.path.join(os.path.dirname(__file__), 'shared')

The contents of the folder ./shared will now be available on http://example.com/shared/. This is pretty useful if you don't want to start a standalone media server or something like that. You can also mount a files on the root folder and still continue to use your application because the shared data middleware forwards all unhandled requests to the application, even if they are below one of the shared folders.


The WSGI specification requires that all middlewares and gateways respect the close callback of an iterator. Because you often want to add another close action to a returned iterator you have to create a new iterator class that has such a close method.

Because this is a very common task you can use the ClosingIterator to add more close methods to an iterable:

return ClosingIterator(app(environ, start_response), [cleanup_session,

If you just have one close function you can pass it directly without wrapping it in a list/tuple.


An Accept object is just a list subclass for lists of (value, quality) tuples. It is automatically sorted by quality and provides the following access methods and members:

True if the Accept object was created from a list, False is the initial value was None. This use used by there request wrappers to keep an information if the header was present or not.
The best value (does not return a tuple!)
Beside index lookup (getting item n) you can also pass it a string to get the quality for the item. If the item is not in the list the returned quality is 0.
find(key) / index(key)
Work like their list equivalents but you can also look up by the value and not only the tuple.
Returns a list with all the values.
Returns a generator for all the values.


Subclass of a dict that stores values for a Cache-Control header. It has accesors for all the cache-control directives specified in RFC 2616. The class does not differentiate between request and response directives.

Because the cache-control directives in the HTTP header use dashes the python descriptors use underscores for that.

To get a header of the CacheControl object again you can convert the object into a string or call the to_header() function. If you plan to subclass it and add your own items have a look at the sourcecode for that class.

Helper Functions

url_decode(s, charset='utf-8', decode_keys=False)

parse a querystring and return it as MultiDict. Per default only values are decoded into unicode strings. If you set decode_keys to True the same will happen for keys.

decode_keys is new in Werkzeug 0.2

url_encode(s, charset='utf-8', encode_keys=False)

URL encode a dict/MultiDict. If a value is None it will not appear in the result string. Per default only values are encoded into the target charset strings. If you set encode_keys to True You can use unicode keys too.

encode_keys is new in Werkzeug 0.2

url_quote(s, charset='utf-8')
URL encode a single string with a given encoding.
url_unquote(s, charset='utf-8')
URL decode a single string with a given decoding.
escape(s, quote=False)
SGML/XML escape an unicode object or string.

A decorator that converts a function into a lazy property. The function wrapped is called the first time to retrieve the result and than that calculated result is used the next time you access the value.

class Foo(object):

    def foo(self):
        # calculate something important here
        return 42

Renamed in Werkzeug 0.2. In previous versions this was called lazy_property but as it turned out the name is misleading. 0.2 will allow both names but starting with 0.3 you have to use the new name exclusively.

Return the real host for the given WSGI enviornment. This takes care of the X-Forwarded-Host header.
get_current_url(environ, root_only=False, strip_querystring=False, host_only=False)

This method allows you to recreate the full URL for the current request or parts of it. Here an example:

>>> env = create_environ("/?param=foo", "http://localhost/script")
>>> get_current_url(env)
>>> get_current_url(env, root_only=True)
>>> get_current_url(env, host_only=True)
>>> get_current_url(env, strip_querystring=True)

Formats the time to ensure compatibility with Netscape's cookie standard.

Accepts a floating point number expressed in seconds since the epoc in, a datetime object or a timetuple. All times in UTC.

Outputs a string in the format Wdy, DD-Mon-YYYY HH:MM:SS GMT.


Formats the time to match the RFC1123 date format.

Accepts a floating point number expressed in seconds since the epoc in, a datetime object or a timetuple. All times in UTC.

Outputs a string in the format Wdy, DD Mon YYYY HH:MM:SS GMT.

Parse an HTTP Accept-* header and return a new Accept object.
Parse a new Cache-Control header and return a new CacheControl object.
redirect(location, code=302)
Return a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, and 307. 300 is not supported because it's not a real redirect and 304 because it's the answer for a request with a request with defined If-Modified-Since headers.
create_environ(path='/', base_url=None, query_string=None, **options)

Create a new WSGI environ dict based on the values passed. The first parameter should be the path of the request which defaults to '/'. The second one can either be a absolute path (in that case the url host is localhost:80) or a full path to the request with scheme, netloc port and the path to the script.

If the path contains a query string it will be used, even if the query_string parameter was given. If it does not contain one the query_string parameter is used as querystring. In that case it can either be a dict, MultiDict or string.

The following options exist:

The request method. Defaults to GET
The input stream. Defaults to an empty read only stream.
The content type for this request. Default is an empty content type.
The value for the content length header. Defaults to 0.
The wsgi.errors stream. Defaults to sys.stderr.
The multithreaded flag for the WSGI Environment. Defaults to False.
The multiprocess flag for the WSGI Environment. Defaults to False.
The run_once flag for the WSGI Environment. Defaults to False.
run_wsgi_app(app, environ, buffered=False)

Return a tuple in the form (app_iter, status, headers) of the application output. This works best if you pass it an application that returns a generator all the time.

Sometimes applications may use the write() callable returned by the start_response function. This tries to resolve such edge cases automatically. But if you don't get the expected output you should set buffered to True which enforces buffering.

test_app(environ, start_response)
A small WSGI application that shows the WSGI environ, information about the python interpreter and a list of installed eggs when called. This is useful to test a middleware (when you need an application) or a WSGI server (installation).

Decorates a function so that the return value is evaluated as WSGI application. This is useful if you want to return response objects:

from werkzeug import BaseResponse, responder

def application(environ, start_response):
    return BaseResponse('Hello World!')