Former-commit-id:a02aeb236c
[formerly9f19e3f712
] [formerlya02aeb236c
[formerly9f19e3f712
] [formerly06a8b51d6d
[formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]] Former-commit-id:06a8b51d6d
Former-commit-id:8e80217e59
[formerly3360eb6c5f
] Former-commit-id:377dcd10b9
443 lines
No EOL
30 KiB
HTML
Executable file
443 lines
No EOL
30 KiB
HTML
Executable file
|
|
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>Werkzeug Documentation</title>
|
|
<link rel="stylesheet" href="_static/style.css" type="text/css">
|
|
<link rel="stylesheet" href="_static/print.css" type="text/css" media="print">
|
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css">
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '#',
|
|
VERSION: '0.6.1'
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
<script type="text/javascript" src="_static/interface.js"></script>
|
|
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
<script type="text/javascript" src="_static/werkzeug.js"></script>
|
|
<link rel="contents" title="Global table of contents" href="contents.html">
|
|
<link rel="index" title="Global index" href="genindex.html">
|
|
<link rel="search" title="Search" href="search.html">
|
|
<link rel="top" title="Werkzeug v0.6.1 documentation" href="index.html">
|
|
<link rel="next" title="HTTP Utilties" href="http.html">
|
|
<link rel="prev" title="URL Routing" href="routing.html">
|
|
|
|
</head>
|
|
<body>
|
|
<div class="page">
|
|
<div class="header">
|
|
<h1 class="heading"><a href="index.html"
|
|
title="back to the documentation overview"><span>Werkzeug</span></a></h1>
|
|
</div>
|
|
<ul class="navigation">
|
|
<li class="indexlink"><a href="index.html">Overview</a></li>
|
|
<li><a href="routing.html">« URL Routing</a></li>
|
|
<li class="active"><a href="#">WSGI Helpers</a></li>
|
|
<li><a href="http.html">HTTP Utilties »</a></li>
|
|
</ul>
|
|
<div class="body">
|
|
<div id="toc">
|
|
<h3>Table Of Contents</h3>
|
|
<div class="inner"><ul>
|
|
<li><a class="reference external" href="#">WSGI Helpers</a><ul>
|
|
<li><a class="reference external" href="#iterator-stream-helpers">Iterator / Stream Helpers</a></li>
|
|
<li><a class="reference external" href="#environ-helpers">Environ Helpers</a></li>
|
|
<li><a class="reference external" href="#convenience-helpers">Convenience Helpers</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section" id="module-werkzeug">
|
|
<span id="wsgi-helpers"></span><h1>WSGI Helpers<a class="headerlink" href="#module-werkzeug" title="Permalink to this headline">¶</a></h1>
|
|
<p>The following classes and functions are designed to make working with
|
|
the WSGI specification easier or operate on the WSGI layer. All the
|
|
functionality from this module is available on the high-level
|
|
<a class="reference external" href="wrappers.html#wrappers"><em>Request/Response classes</em></a>.</p>
|
|
<p>You can import all these objects directly from <a class="reference internal" href="#module-werkzeug"><tt class="xref py py-mod docutils literal"><span class="pre">werkzeug</span></tt></a>.</p>
|
|
<div class="section" id="iterator-stream-helpers">
|
|
<h2>Iterator / Stream Helpers<a class="headerlink" href="#iterator-stream-helpers" title="Permalink to this headline">¶</a></h2>
|
|
<p>These classes and functions simplify working with the WSGI application
|
|
iterator and the input stream.</p>
|
|
<dl class="class">
|
|
<dt id="werkzeug.ClosingIterator">
|
|
<em class="property">class </em><tt class="descclassname">werkzeug.</tt><tt class="descname">ClosingIterator</tt><big>(</big><em>iterable</em>, <em>callbacks=None</em><big>)</big><a class="headerlink" href="#werkzeug.ClosingIterator" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>The WSGI specification requires that all middlewares and gateways
|
|
respect the <cite>close</cite> callback of an iterator. Because it is useful to add
|
|
another close action to a returned iterator and adding a custom iterator
|
|
is a boring task this class can be used for that:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">ClosingIterator</span><span class="p">(</span><span class="n">app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">),</span> <span class="p">[</span><span class="n">cleanup_session</span><span class="p">,</span>
|
|
<span class="n">cleanup_locals</span><span class="p">])</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If there is just one close function it can be passed instead of the list.</p>
|
|
<p>A closing iterator is not needed if the application uses response objects
|
|
and finishes the processing if the response is started:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">response</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
|
|
<span class="k">finally</span><span class="p">:</span>
|
|
<span class="n">cleanup_session</span><span class="p">()</span>
|
|
<span class="n">cleanup_locals</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="class">
|
|
<dt id="werkzeug.FileWrapper">
|
|
<em class="property">class </em><tt class="descclassname">werkzeug.</tt><tt class="descname">FileWrapper</tt><big>(</big><em>file</em>, <em>buffer_size=8192</em><big>)</big><a class="headerlink" href="#werkzeug.FileWrapper" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This class can be used to convert a <tt class="xref py py-class docutils literal"><span class="pre">file</span></tt>-like object into
|
|
an iterable. It yields <cite>buffer_size</cite> blocks until the file is fully
|
|
read.</p>
|
|
<p>You should not use this class directly but rather use the
|
|
<a title="werkzeug.wrap_file" class="reference internal" href="#werkzeug.wrap_file"><tt class="xref py py-func docutils literal"><span class="pre">wrap_file()</span></tt></a> function that uses the WSGI server’s file wrapper
|
|
support if it’s available.</p>
|
|
<p class="versionadded">
|
|
<span class="versionmodified">New in version 0.5.</span></p>
|
|
<p>If you’re using this object together with a <a title="werkzeug.BaseResponse" class="reference external" href="wrappers.html#werkzeug.BaseResponse"><tt class="xref py py-class docutils literal"><span class="pre">BaseResponse</span></tt></a> you have
|
|
to use the <cite>direct_passthrough</cite> mode.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>file</strong> – a <tt class="xref py py-class docutils literal"><span class="pre">file</span></tt>-like object with a <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#file.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> method.</li>
|
|
<li><strong>buffer_size</strong> – number of bytes for one iteration.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="class">
|
|
<dt id="werkzeug.LimitedStream">
|
|
<em class="property">class </em><tt class="descclassname">werkzeug.</tt><tt class="descname">LimitedStream</tt><big>(</big><em>stream</em>, <em>limit</em>, <em>silent=True</em><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Wraps a stream so that it doesn’t read more than n bytes. If the
|
|
stream is exhausted and the caller tries to get more bytes from it
|
|
<a title="werkzeug.LimitedStream.on_exhausted" class="reference internal" href="#werkzeug.LimitedStream.on_exhausted"><tt class="xref py py-func docutils literal"><span class="pre">on_exhausted()</span></tt></a> is called which by default returns an empty
|
|
string. The return value of that function is forwarded
|
|
to the reader function. So if it returns an empty string
|
|
<a title="werkzeug.LimitedStream.read" class="reference internal" href="#werkzeug.LimitedStream.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> will return an empty string as well.</p>
|
|
<p>The limit however must never be higher than what the stream can
|
|
output. Otherwise <a title="werkzeug.LimitedStream.readlines" class="reference internal" href="#werkzeug.LimitedStream.readlines"><tt class="xref py py-meth docutils literal"><span class="pre">readlines()</span></tt></a> will try to read past the
|
|
limit.</p>
|
|
<p>The <cite>silent</cite> parameter has no effect if <a title="werkzeug.LimitedStream.is_exhausted" class="reference internal" href="#werkzeug.LimitedStream.is_exhausted"><tt class="xref py py-meth docutils literal"><span class="pre">is_exhausted()</span></tt></a> is
|
|
overriden by a subclass.</p>
|
|
<p class="versionchanged">
|
|
<span class="versionmodified">Changed in version 0.6: </span>Non-silent usage was deprecated because it causes confusion.
|
|
If you want that, override <a title="werkzeug.LimitedStream.is_exhausted" class="reference internal" href="#werkzeug.LimitedStream.is_exhausted"><tt class="xref py py-meth docutils literal"><span class="pre">is_exhausted()</span></tt></a> and raise a
|
|
<a title="werkzeug.exceptions.BadRequest" class="reference external" href="exceptions.html#werkzeug.exceptions.BadRequest"><tt class="xref py py-exc docutils literal"><span class="pre">BadRequest</span></tt></a> yourself.</p>
|
|
<div class="admonition-note-on-wsgi-compliance admonition ">
|
|
<p class="first admonition-title">Note on WSGI compliance</p>
|
|
<p>calls to <a title="werkzeug.LimitedStream.readline" class="reference internal" href="#werkzeug.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> and <a title="werkzeug.LimitedStream.readlines" class="reference internal" href="#werkzeug.LimitedStream.readlines"><tt class="xref py py-meth docutils literal"><span class="pre">readlines()</span></tt></a> are not
|
|
WSGI compliant because it passes a size argument to the
|
|
readline methods. Unfortunately the WSGI PEP is not safely
|
|
implementable without a size argument to <a title="werkzeug.LimitedStream.readline" class="reference internal" href="#werkzeug.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a>
|
|
because there is no EOF marker in the stream. As a result
|
|
of that the use of <a title="werkzeug.LimitedStream.readline" class="reference internal" href="#werkzeug.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> is discouraged.</p>
|
|
<p>For the same reason iterating over the <a title="werkzeug.LimitedStream" class="reference internal" href="#werkzeug.LimitedStream"><tt class="xref py py-class docutils literal"><span class="pre">LimitedStream</span></tt></a>
|
|
is not portable. It internally calls <a title="werkzeug.LimitedStream.readline" class="reference internal" href="#werkzeug.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a>.</p>
|
|
<p class="last">We strongly suggest using <a title="werkzeug.LimitedStream.read" class="reference internal" href="#werkzeug.LimitedStream.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> only or using the
|
|
<a title="werkzeug.make_line_iter" class="reference internal" href="#werkzeug.make_line_iter"><tt class="xref py py-func docutils literal"><span class="pre">make_line_iter()</span></tt></a> which safely iterates line-based
|
|
over a WSGI input stream.</p>
|
|
</div>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>stream</strong> – the stream to wrap.</li>
|
|
<li><strong>limit</strong> – the limit for the stream, must not be longer than
|
|
what the string can provide if the stream does not
|
|
end with <cite>EOF</cite> (like <cite>wsgi.input</cite>)</li>
|
|
<li><strong>silent</strong> – If set to <cite>True</cite> the stream will allow reading
|
|
past the limit and will return an empty string.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<dl class="method">
|
|
<dt id="werkzeug.LimitedStream.exhaust">
|
|
<tt class="descname">exhaust</tt><big>(</big><em>chunk_size=16384</em><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream.exhaust" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Exhaust the stream. This consumes all the data left until the
|
|
limit is reached.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>chunk_size</strong> – the size for a chunk. It will read the chunk
|
|
until the stream is exhausted and throw away
|
|
the results.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="werkzeug.LimitedStream.is_exhausted">
|
|
<tt class="descname">is_exhausted</tt><a class="headerlink" href="#werkzeug.LimitedStream.is_exhausted" title="Permalink to this definition">¶</a></dt>
|
|
<dd>If the stream is exhausted this attribute is <cite>True</cite>.</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="werkzeug.LimitedStream.on_exhausted">
|
|
<tt class="descname">on_exhausted</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream.on_exhausted" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This is called when the stream tries to read past the limit.
|
|
The return value of this function is returned from the reading
|
|
function.</p>
|
|
<p>Per default this raises a <a title="werkzeug.exceptions.BadRequest" class="reference external" href="exceptions.html#werkzeug.exceptions.BadRequest"><tt class="xref py py-exc docutils literal"><span class="pre">BadRequest</span></tt></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="werkzeug.LimitedStream.read">
|
|
<tt class="descname">read</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream.read" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Read <cite>size</cite> bytes or if size is not provided everything is read.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>size</strong> – the number of bytes read.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="werkzeug.LimitedStream.readline">
|
|
<tt class="descname">readline</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream.readline" title="Permalink to this definition">¶</a></dt>
|
|
<dd>Reads one line from the stream.</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="werkzeug.LimitedStream.readlines">
|
|
<tt class="descname">readlines</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.LimitedStream.readlines" title="Permalink to this definition">¶</a></dt>
|
|
<dd>Reads a file into a list of strings. It calls <a title="werkzeug.LimitedStream.readline" class="reference internal" href="#werkzeug.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a>
|
|
until the file is read to the end. It does support the optional
|
|
<cite>size</cite> argument if the underlaying stream supports it for
|
|
<cite>readline</cite>.</dd></dl>
|
|
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.make_line_iter">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">make_line_iter</tt><big>(</big><em>stream</em>, <em>limit=None</em>, <em>buffer_size=10240</em><big>)</big><a class="headerlink" href="#werkzeug.make_line_iter" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Safely iterates line-based over an input stream. If the input stream
|
|
is not a <a title="werkzeug.LimitedStream" class="reference internal" href="#werkzeug.LimitedStream"><tt class="xref py py-class docutils literal"><span class="pre">LimitedStream</span></tt></a> the <cite>limit</cite> parameter is mandatory.</p>
|
|
<p>This uses the stream’s <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#file.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> method internally as opposite
|
|
to the <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#file.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method that is unsafe and can only be used
|
|
in violation of the WSGI specification. The same problem applies to the
|
|
<cite>__iter__</cite> function of the input stream which calls <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#file.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a>
|
|
without arguments.</p>
|
|
<p>If you need line-by-line processing it’s strongly recommended to iterate
|
|
over the input stream using this helper function.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>stream</strong> – the stream to iterate over.</li>
|
|
<li><strong>limit</strong> – the limit in bytes for the stream. (Usually
|
|
content length. Not necessary if the <cite>stream</cite>
|
|
is a <a title="werkzeug.LimitedStream" class="reference internal" href="#werkzeug.LimitedStream"><tt class="xref py py-class docutils literal"><span class="pre">LimitedStream</span></tt></a>.</li>
|
|
<li><strong>buffer_size</strong> – The optional buffer size.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.wrap_file">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">wrap_file</tt><big>(</big><em>environ</em>, <em>file</em>, <em>buffer_size=8192</em><big>)</big><a class="headerlink" href="#werkzeug.wrap_file" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Wraps a file. This uses the WSGI server’s file wrapper if available
|
|
or otherwise the generic <a title="werkzeug.FileWrapper" class="reference internal" href="#werkzeug.FileWrapper"><tt class="xref py py-class docutils literal"><span class="pre">FileWrapper</span></tt></a>.</p>
|
|
<p class="versionadded">
|
|
<span class="versionmodified">New in version 0.5.</span></p>
|
|
<p>If the file wrapper from the WSGI server is used it’s important to not
|
|
iterate over it from inside the application but to pass it through
|
|
unchanged. If you want to pass out a file wrapper inside a response
|
|
object you have to set <a title="werkzeug.BaseResponse.direct_passthrough" class="reference external" href="wrappers.html#werkzeug.BaseResponse.direct_passthrough"><tt class="xref py py-attr docutils literal"><span class="pre">direct_passthrough</span></tt></a> to <cite>True</cite>.</p>
|
|
<p>More information about file wrappers are available in <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>file</strong> – a <tt class="xref py py-class docutils literal"><span class="pre">file</span></tt>-like object with a <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#file.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> method.</li>
|
|
<li><strong>buffer_size</strong> – number of bytes for one iteration.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
</div>
|
|
<div class="section" id="environ-helpers">
|
|
<h2>Environ Helpers<a class="headerlink" href="#environ-helpers" title="Permalink to this headline">¶</a></h2>
|
|
<p>These functions operate on the WSGI environment. They extract useful
|
|
information or perform common manipulations:</p>
|
|
<dl class="function">
|
|
<dt id="werkzeug.get_host">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">get_host</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.get_host" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Return the real host for the given WSGI environment. This takes care
|
|
of the <cite>X-Forwarded-Host</cite> header.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>environ</strong> – the WSGI environment to get the host of.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.get_current_url">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">get_current_url</tt><big>(</big><em>environ</em>, <em>root_only=False</em>, <em>strip_querystring=False</em>, <em>host_only=False</em><big>)</big><a class="headerlink" href="#werkzeug.get_current_url" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>A handy helper function that recreates the full URL for the current
|
|
request or parts of it. Here an example:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">werkzeug</span> <span class="kn">import</span> <span class="n">create_environ</span>
|
|
<span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="n">create_environ</span><span class="p">(</span><span class="s">"/?param=foo"</span><span class="p">,</span> <span class="s">"http://localhost/script"</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">get_current_url</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
|
<span class="go">'http://localhost/script/?param=foo'</span>
|
|
<span class="gp">>>> </span><span class="n">get_current_url</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">root_only</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
|
<span class="go">'http://localhost/script/'</span>
|
|
<span class="gp">>>> </span><span class="n">get_current_url</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">host_only</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
|
<span class="go">'http://localhost/'</span>
|
|
<span class="gp">>>> </span><span class="n">get_current_url</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">strip_querystring</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
|
<span class="go">'http://localhost/script/'</span>
|
|
</pre></div>
|
|
</div>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>environ</strong> – the WSGI environment to get the current URL from.</li>
|
|
<li><strong>root_only</strong> – set <cite>True</cite> if you only want the root URL.</li>
|
|
<li><strong>strip_querystring</strong> – set to <cite>True</cite> if you don’t want the querystring.</li>
|
|
<li><strong>host_only</strong> – set to <cite>True</cite> if the host URL should be returned.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.pop_path_info">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">pop_path_info</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.pop_path_info" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Removes and returns the next segment of <cite>PATH_INFO</cite>, pushing it onto
|
|
<cite>SCRIPT_NAME</cite>. Returns <cite>None</cite> if there is nothing left on <cite>PATH_INFO</cite>.</p>
|
|
<p>If there are empty segments (<tt class="docutils literal"><span class="pre">'/foo//bar</span></tt>) these are ignored but
|
|
properly pushed to the <cite>SCRIPT_NAME</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="p">{</span><span class="s">'SCRIPT_NAME'</span><span class="p">:</span> <span class="s">'/foo'</span><span class="p">,</span> <span class="s">'PATH_INFO'</span><span class="p">:</span> <span class="s">'/a/b'</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="n">pop_path_info</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
|
<span class="go">'a'</span>
|
|
<span class="gp">>>> </span><span class="n">env</span><span class="p">[</span><span class="s">'SCRIPT_NAME'</span><span class="p">]</span>
|
|
<span class="go">'/foo/a'</span>
|
|
<span class="gp">>>> </span><span class="n">pop_path_info</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
|
<span class="go">'b'</span>
|
|
<span class="gp">>>> </span><span class="n">env</span><span class="p">[</span><span class="s">'SCRIPT_NAME'</span><span class="p">]</span>
|
|
<span class="go">'/foo/a/b'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p class="versionadded">
|
|
<span class="versionmodified">New in version 0.5.</span></p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>environ</strong> – the WSGI environment that is modified.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.peek_path_info">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">peek_path_info</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.peek_path_info" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Returns the next segment on the <cite>PATH_INFO</cite> or <cite>None</cite> if there
|
|
is none. Works like <a title="werkzeug.pop_path_info" class="reference internal" href="#werkzeug.pop_path_info"><tt class="xref py py-func docutils literal"><span class="pre">pop_path_info()</span></tt></a> without modifying the
|
|
environment:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="p">{</span><span class="s">'SCRIPT_NAME'</span><span class="p">:</span> <span class="s">'/foo'</span><span class="p">,</span> <span class="s">'PATH_INFO'</span><span class="p">:</span> <span class="s">'/a/b'</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="n">peek_path_info</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
|
<span class="go">'a'</span>
|
|
<span class="gp">>>> </span><span class="n">peek_path_info</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
|
<span class="go">'a'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p class="versionadded">
|
|
<span class="versionmodified">New in version 0.5.</span></p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>environ</strong> – the WSGI environment that is checked.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
</div>
|
|
<div class="section" id="convenience-helpers">
|
|
<h2>Convenience Helpers<a class="headerlink" href="#convenience-helpers" title="Permalink to this headline">¶</a></h2>
|
|
<dl class="function">
|
|
<dt id="werkzeug.test_app">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">test_app</tt><big>(</big><em>environ</em>, <em>start_response</em><big>)</big><a class="headerlink" href="#werkzeug.test_app" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Simple test application that dumps the environment. You can use
|
|
it to check if Werkzeug is working properly:</p>
|
|
<div class="highlight-pycon"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">werkzeug</span> <span class="kn">import</span> <span class="n">run_simple</span><span class="p">,</span> <span class="n">test_app</span>
|
|
<span class="gp">>>> </span><span class="n">run_simple</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">,</span> <span class="mi">3000</span><span class="p">,</span> <span class="n">test_app</span><span class="p">)</span>
|
|
<span class="go"> * Running on http://localhost:3000/</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The application displays important information from the WSGI environment,
|
|
the Python interpreter and the installed libraries.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="werkzeug.responder">
|
|
<tt class="descclassname">werkzeug.</tt><tt class="descname">responder</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#werkzeug.responder" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Marks a function as responder. Decorate a function with it and it
|
|
will automatically call the return value as WSGI application.</p>
|
|
<p>Example:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@responder</span>
|
|
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
|
|
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">'Hello World!'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2008 by the <a href="http://pocoo.org/">Pocoo Team</a>,
|
|
documentation generated by <a href="http://sphinx.pocoo.org/">Sphinx</a>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |