Serve Shared Static Files

  • class werkzeug.middleware.shareddata.SharedDataMiddleware(_app, exports, disallow=None, cache=True, cache_timeout=43200, fallback_mimetype='text/plain')
  • A WSGI middleware that provides static content for developmentenvironments or simple server setups. Usage is quite simple:
  1. import os
  2. from werkzeug.wsgi import SharedDataMiddleware
  3.  
  4. app = SharedDataMiddleware(app, {
  5. '/static': os.path.join(os.path.dirname(__file__), 'static')
  6. })

The contents of the folder ./shared will now be available onhttp://example.com/shared/. This is pretty useful during developmentbecause a standalone media server is not required. One can also mountfiles on the root folder and still continue to use the application becausethe shared data middleware forwards all unhandled requests to theapplication, even if the requests are below one of the shared folders.

If pkg_resources is available you can also tell the middleware to servefiles from package data:

  1. app = SharedDataMiddleware(app, {
  2. '/static': ('myapplication', 'static')
  3. })

This will then serve the static folder in the _myapplication_Python package.

The optional disallow parameter can be a list of fnmatch()rules for files that are not accessible from the web. If cache is set toFalse no caching headers are sent.

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

The middleware will guess the mimetype using the Python mimetype_module. If it’s unable to figure out the charset it will fall backto _fallback_mimetype.

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

New in version 0.6: The fallback_mimetype parameter was added.

Parameters:

  • app – the application to wrap. If you don’t want to wrap anapplication 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.
  • isallowed(_filename)
  • Subclasses can override this method to disallow the access tocertain files. However by providing disallow in the constructorthis method is overwritten.