# Middlewares
Middlewares wrap applications to dispatch between then or provideadditional request handling. Additionally to the middlewares documentedhere, there is also the DebuggedApplication class that isimplemented 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 developmentenvironments 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 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:
~~~
app = SharedDataMiddleware(app, {
'/shared': ('myapplication', 'shared_files')
})
~~~
This will then serve the shared_files folder in the myapplicationPython package.
The optional disallow parameter can be a list of [fnmatch()](http://docs.python.org/dev/library/fnmatch.html#fnmatch.fnmatch "(在 Python v3.5)") [http://docs.python.org/dev/library/fnmatch.html#fnmatch.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 mimetypemodule. If it's unable to figure out the charset it will fall backto fallback_mimetype.
在 0.5 版更改: The cache timeout is configurable now.
0.6 新版功能: The fallback_mimetype parameter was added.
<table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple"><li><strong>app</strong> – the application to wrap. If you don't want to wrap anapplication you can pass it <tt class="xref py py-exc docutils literal"><span class="pre">NotFound</span></tt>.</li><li><strong>exports</strong> – a dict of exported files and folders.</li><li><strong>disallow</strong> – a list of <a class="reference external" href="http://docs.python.org/dev/library/fnmatch.html#fnmatch.fnmatch" title="(在 Python v3.5)"><tt class="xref py py-func docutils literal"><span class="pre">fnmatch()</span></tt></a><span class="link-target"> [http://docs.python.org/dev/library/fnmatch.html#fnmatch.fnmatch]</span> rules.</li><li><strong>fallback_mimetype</strong> – the fallback mimetype for unknown files.</li><li><strong>cache</strong> – enable or disable caching headers.</li></ul></td></tr><tr class="field-even field"><th class="field-name" colspan="2">Param cache_timeout:</th></tr><tr class="field-even field"><td> </td><td class="field-body"><p class="first last">the cache timeout in seconds for the headers.</p></td></tr></tbody></table>
is_allowed(*filename*)
Subclasses can override this method to disallow the access tocertain files. However by providing disallow in the constructorthis method is overwritten.
*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?
- 开始
- Werkzeug 文档概览
- 安装
- 过渡到 Werkzeug 1.0
- Werkzeug 教程
- API 标准
- 快速开始
- Python 3 Notes
- 服务和测试
- Debugging Applications
- 在服务器运行 WSGI 应用
- 单元测试
- 参考
- Request / Response Objects
- URL Routing
- WSGI Helpers
- HTTP Utilities
- Data Structures
- Utilities
- Context Locals
- Middlewares
- HTTP Exceptions
- 部署
- CGI
- mod_wsgi (Apache)
- FastCGI
- HTTP Proxying
- 贡献模块
- Atom Syndication
- Sessions
- Secure Cookie
- Cache
- Extra Wrappers
- Iter IO
- Fixers
- WSGI Application Profiler
- Lint Validation Middleware
- 额外说明
- Werkzeug Changelog
- Important Terms
- Unicode
- Dealing with Request Data