Toggle navigation

Middlewares

Middlewares wrap applications to dispatch between them or provide additional request handling. Additionally to the middlewares documented here, there is also the DebuggedApplication class that is implemented as a WSGI middleware.

class werkzeug.wsgi.SharedDataMiddleware(app, exports, disallow=None, cache=True, cache_timeout=43200, fallback_mimetype='text/plain')

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

import os
from werkzeug.wsgi 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 during development because a standalone media server is not required. One can also mount files on the root folder and still continue to use the application because the shared data middleware forwards all unhandled requests to the application, even if the requests are below one of the shared folders.

If [UNKNOWN NODE title_reference] is available you can also tell the middleware to serve files from package data:

app = SharedDataMiddleware(app, {
    '/shared': ('myapplication', 'shared_files')
})

This will then serve the shared_files folder in the [UNKNOWN NODE title_reference] Python package.

The optional [UNKNOWN NODE title_reference] parameter can be a list of fnmatch() rules for files that are not accessible from the web. If [UNKNOWN NODE title_reference] is set to [UNKNOWN NODE title_reference] no caching headers are sent.

Currently the middleware does not support non ASCII filenames. If the encoding on the file system happens to be the encoding of the URI it may work but this could also be by accident. We strongly suggest using ASCII only file names for static files.

The middleware will guess the mimetype using the Python [UNKNOWN NODE title_reference] module. If it’s unable to figure out the charset it will fall back to [UNKNOWN NODE title_reference].

Changed in version 0.5: The cache timeout is configurable now.

New in version 0.6: The [UNKNOWN NODE title_reference] parameter was added.

Parameters
  • app – the application to wrap. If you don’t want to wrap an application you can pass it NotFound.
  • exports – a list or dict of exported files and folders.
  • disallow – a list of fnmatch() rules.
  • fallback_mimetype – the fallback mimetype for unknown files.
  • cache – enable or disable caching headers.
  • cache_timeout – the cache timeout in seconds for the headers.
is_allowed(filename)

Subclasses can override this method to disallow the access to certain files. However by providing [UNKNOWN NODE title_reference] in the constructor this method is overwritten.

class werkzeug.wsgi.ProxyMiddleware(app, targets, chunk_size=16384, timeout=10)

This middleware routes some requests to the provided WSGI app and proxies some requests to an external server. This is not something that can generally be done on the WSGI layer and some HTTP requests will not tunnel through correctly (for instance websocket requests cannot be proxied through WSGI). As a result this is only really useful for some basic requests that can be forwarded.

Example configuration:

app = ProxyMiddleware(app, {
    '/static/': {
        'target': 'http://127.0.0.1:5001/',
    }
})

For each host options can be specified. The following options are supported:

target:
the target URL to dispatch to
remove_prefix:
if set to [UNKNOWN NODE title_reference] the prefix is chopped off the URL before dispatching it to the server.
host:
When set to '<auto>' which is the default the host header is automatically rewritten to the URL of the target. If set to [UNKNOWN NODE title_reference] then the host header is unmodified from the client request. Any other value overwrites the host header with that value.
headers:
An optional dictionary of headers that should be sent with the request to the target host.
ssl_context:
In case this is an HTTPS target host then an SSL context can be provided here (ssl.SSLContext). This can be used for instance to disable SSL verification.

In this case everything below '/static/' is proxied to the server on port 5001. The host header is automatically rewritten and so are request URLs (eg: the leading [UNKNOWN NODE title_reference] prefix here gets chopped off).

New in version 0.14.

class werkzeug.wsgi.DispatcherMiddleware(app, mounts=None)

Allows one to mount middlewares or applications in a WSGI application. This is useful if you want to combine multiple WSGI applications:

app = DispatcherMiddleware(app, {
    '/app2':        app2,
    '/app3':        app3
})

Also there’s the …

werkzeug._internal._easteregg(app=None)

Like the name says. But who knows how it works?