/usr/share/doc/python-werkzeug-doc/html/wsgi.html is in python-werkzeug-doc 0.9.4+dfsg-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WSGI Helpers — Werkzeug 0.9.4 documentation</title>
<link rel="stylesheet" href="_static/werkzeug.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.9.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Werkzeug 0.9.4 documentation" href="index.html" />
<link rel="next" title="HTTP Utilities" href="http.html" />
<link rel="prev" title="URL Routing" href="routing.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="http.html" title="HTTP Utilities"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="routing.html" title="URL Routing"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Werkzeug 0.9.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-werkzeug.wsgi">
<span id="wsgi-helpers"></span><h1>WSGI Helpers<a class="headerlink" href="#module-werkzeug.wsgi" 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 internal" href="wrappers.html#wrappers"><em>Request/Response classes</em></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.wsgi.ClosingIterator">
<em class="property">class </em><tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">ClosingIterator</tt><big>(</big><em>iterable</em>, <em>callbacks=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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.wsgi.FileWrapper">
<em class="property">class </em><tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">FileWrapper</tt><big>(</big><em>file</em>, <em>buffer_size=8192</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.wrap_file" title="werkzeug.wsgi.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>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<p>If you’re using this object together with a <tt class="xref py py-class docutils literal"><span class="pre">BaseResponse</span></tt> 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-odd 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 <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> 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.wsgi.LimitedStream">
<em class="property">class </em><tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">LimitedStream</tt><big>(</big><em>stream</em>, <em>limit</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.on_exhausted" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.read" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.readlines" title="werkzeug.wsgi.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>
<div class="admonition-note-on-wsgi-compliance admonition">
<p class="first admonition-title">Note on WSGI compliance</p>
<p>calls to <a class="reference internal" href="#werkzeug.wsgi.LimitedStream.readline" title="werkzeug.wsgi.LimitedStream.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> and <a class="reference internal" href="#werkzeug.wsgi.LimitedStream.readlines" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.readline" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.readline" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream" title="werkzeug.wsgi.LimitedStream"><tt class="xref py py-class docutils literal"><span class="pre">LimitedStream</span></tt></a>
is not portable. It internally calls <a class="reference internal" href="#werkzeug.wsgi.LimitedStream.readline" title="werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream.read" title="werkzeug.wsgi.LimitedStream.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> only or using the
<a class="reference internal" href="#werkzeug.wsgi.make_line_iter" title="werkzeug.wsgi.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-odd 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>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.exhaust">
<tt class="descname">exhaust</tt><big>(</big><em>chunk_size=65536</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>chunk_size</strong> – the size for a chunk. It will read the chunk
until the stream is exhausted and throw away
the results.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.wsgi.LimitedStream.is_exhausted">
<tt class="descname">is_exhausted</tt><a class="headerlink" href="#werkzeug.wsgi.LimitedStream.is_exhausted" title="Permalink to this definition">¶</a></dt>
<dd><p>If the stream is exhausted this attribute is <cite>True</cite>.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.on_disconnect">
<tt class="descname">on_disconnect</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.wsgi.LimitedStream.on_disconnect" title="Permalink to this definition">¶</a></dt>
<dd><p>What should happen if a disconnect is detected? The return
value of this function is returned from read functions in case
the client went away. By default a
<a class="reference internal" href="exceptions.html#werkzeug.exceptions.ClientDisconnected" title="werkzeug.exceptions.ClientDisconnected"><tt class="xref py py-exc docutils literal"><span class="pre">ClientDisconnected</span></tt></a> exception is raised.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.on_exhausted">
<tt class="descname">on_exhausted</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.read">
<tt class="descname">read</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>size</strong> – the number of bytes read.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.readline">
<tt class="descname">readline</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.LimitedStream.readline" title="Permalink to this definition">¶</a></dt>
<dd><p>Reads one line from the stream.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.readlines">
<tt class="descname">readlines</tt><big>(</big><em>size=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.LimitedStream.readlines" title="Permalink to this definition">¶</a></dt>
<dd><p>Reads a file into a list of strings. It calls <a class="reference internal" href="#werkzeug.wsgi.LimitedStream.readline" title="werkzeug.wsgi.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>.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.wsgi.LimitedStream.tell">
<tt class="descname">tell</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.wsgi.LimitedStream.tell" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the position of the stream.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
</dd></dl>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.make_line_iter">
<tt class="descclassname">werkzeug.wsgi.</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.wsgi.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 class="reference internal" href="#werkzeug.wsgi.LimitedStream" title="werkzeug.wsgi.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 <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> method internally as opposite
to the <tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt> 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 <tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt>
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>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span>This function now ensures that the limit was reached.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9: </span>added support for iterators as 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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>stream</strong> – the stream or iterate 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 class="reference internal" href="#werkzeug.wsgi.LimitedStream" title="werkzeug.wsgi.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.wsgi.make_chunk_iter">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">make_chunk_iter</tt><big>(</big><em>stream</em>, <em>separator</em>, <em>limit=None</em>, <em>buffer_size=10240</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.make_chunk_iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Works like <a class="reference internal" href="#werkzeug.wsgi.make_line_iter" title="werkzeug.wsgi.make_line_iter"><tt class="xref py py-func docutils literal"><span class="pre">make_line_iter()</span></tt></a> but accepts a separator
which divides chunks. If you want newline based processing
you should use <a class="reference internal" href="#werkzeug.wsgi.make_line_iter" title="werkzeug.wsgi.make_line_iter"><tt class="xref py py-func docutils literal"><span class="pre">make_line_iter()</span></tt></a> instead as it
supports arbitrary newline markers.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.8.</span></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9: </span>added support for iterators as 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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>stream</strong> – the stream or iterate to iterate over.</li>
<li><strong>separator</strong> – the separator that divides chunks.</li>
<li><strong>limit</strong> – the limit in bytes for the stream. (Usually
content length. Not necessary if the <cite>stream</cite>
is otherwise already limited).</li>
<li><strong>buffer_size</strong> – The optional buffer size.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.wrap_file">
<tt class="descclassname">werkzeug.wsgi.</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.wsgi.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 class="reference internal" href="#werkzeug.wsgi.FileWrapper" title="werkzeug.wsgi.FileWrapper"><tt class="xref py py-class docutils literal"><span class="pre">FileWrapper</span></tt></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<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 <tt class="xref py py-attr docutils literal"><span class="pre">direct_passthrough</span></tt> 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-odd 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 <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> 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.wsgi.get_host">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_host</tt><big>(</big><em>environ</em>, <em>trusted_hosts=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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. Optionally it verifies that the host
is in a list of trusted hosts. If the host is not in there it will raise
a <a class="reference internal" href="exceptions.html#werkzeug.exceptions.SecurityError" title="werkzeug.exceptions.SecurityError"><tt class="xref py py-exc docutils literal"><span class="pre">SecurityError</span></tt></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-odd 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>
<li><strong>trusted_hosts</strong> – a list of trusted hosts, see <a class="reference internal" href="#werkzeug.wsgi.host_is_trusted" title="werkzeug.wsgi.host_is_trusted"><tt class="xref py py-func docutils literal"><span class="pre">host_is_trusted()</span></tt></a>
for more information.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_content_length">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_content_length</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.get_content_length" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the content length from the WSGI environment as
integer. If it’s not available <cite>None</cite> is returned.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>environ</strong> – the WSGI environ to fetch the content length from.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_input_stream">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_input_stream</tt><big>(</big><em>environ</em>, <em>safe_fallback=True</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.get_input_stream" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the input stream from the WSGI environment and wraps it
in the most sensible way possible. The stream returned is not the
raw WSGI stream in most cases but one that is safe to read from
without taking into account the content length.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – the WSGI environ to fetch the stream from.</li>
<li><strong>safe</strong> – indicates weather the function should use an empty
stream as safe fallback or just return the original
WSGI input stream if it can’t wrap it safely. The
default is to return an empty string in those cases.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_current_url">
<tt class="descclassname">werkzeug.wsgi.</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>, <em>trusted_hosts=None</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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.test</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>
<p>This optionally it verifies that the host is in a list of trusted hosts.
If the host is not in there it will raise a
<a class="reference internal" href="exceptions.html#werkzeug.exceptions.SecurityError" title="werkzeug.exceptions.SecurityError"><tt class="xref py py-exc docutils literal"><span class="pre">SecurityError</span></tt></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-odd 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>
<li><strong>trusted_hosts</strong> – a list of trusted hosts, see <a class="reference internal" href="#werkzeug.wsgi.host_is_trusted" title="werkzeug.wsgi.host_is_trusted"><tt class="xref py py-func docutils literal"><span class="pre">host_is_trusted()</span></tt></a>
for more information.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_query_string">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_query_string</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.get_query_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <cite>QUERY_STRING</cite> from the WSGI environment. This also takes
care about the WSGI decoding dance on Python 3 environments as a
native string. The string returned will be restricted to ASCII
characters.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>environ</strong> – the WSGI environment object to get the query string from.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_script_name">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_script_name</tt><big>(</big><em>environ</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.get_script_name" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <cite>SCRIPT_NAME</cite> from the WSGI environment and properly
decodes it. This also takes care about the WSGI decoding dance
on Python 3 environments. if the <cite>charset</cite> is set to <cite>None</cite> a
bytestring is returned.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – the WSGI environment object to get the path from.</li>
<li><strong>charset</strong> – the charset for the path, or <cite>None</cite> if no
decoding should be performed.</li>
<li><strong>errors</strong> – the decoding error handling.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.get_path_info">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">get_path_info</tt><big>(</big><em>environ</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.get_path_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <cite>PATH_INFO</cite> from the WSGI environment and properly
decodes it. This also takes care about the WSGI decoding dance
on Python 3 environments. if the <cite>charset</cite> is set to <cite>None</cite> a
bytestring is returned.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – the WSGI environment object to get the path from.</li>
<li><strong>charset</strong> – the charset for the path info, or <cite>None</cite> if no
decoding should be performed.</li>
<li><strong>errors</strong> – the decoding error handling.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.pop_path_info">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">pop_path_info</tt><big>(</big><em>environ</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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 the <cite>charset</cite> is set to <cite>None</cite> a bytestring is returned.</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>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.9: </span>The path is now decoded and a charset and encoding
parameter can be provided.</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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>environ</strong> – the WSGI environment that is modified.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.peek_path_info">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">peek_path_info</tt><big>(</big><em>environ</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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 class="reference internal" href="#werkzeug.wsgi.pop_path_info" title="werkzeug.wsgi.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>If the <cite>charset</cite> is set to <cite>None</cite> a bytestring is returned.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.9: </span>The path is now decoded and a charset and encoding
parameter can be provided.</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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>environ</strong> – the WSGI environment that is checked.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.extract_path_info">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">extract_path_info</tt><big>(</big><em>environ_or_baseurl</em>, <em>path_or_url</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em>, <em>collapse_http_schemes=True</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.extract_path_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Extracts the path info from the given URL (or WSGI environment) and
path. The path info returned is a unicode string, not a bytestring
suitable for a WSGI environment. The URLs might also be IRIs.</p>
<p>If the path info could not be determined, <cite>None</cite> is returned.</p>
<p>Some examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">extract_path_info</span><span class="p">(</span><span class="s">'http://example.com/app'</span><span class="p">,</span> <span class="s">'/app/hello'</span><span class="p">)</span>
<span class="go">u'/hello'</span>
<span class="gp">>>> </span><span class="n">extract_path_info</span><span class="p">(</span><span class="s">'http://example.com/app'</span><span class="p">,</span>
<span class="gp">... </span> <span class="s">'https://example.com/app/hello'</span><span class="p">)</span>
<span class="go">u'/hello'</span>
<span class="gp">>>> </span><span class="n">extract_path_info</span><span class="p">(</span><span class="s">'http://example.com/app'</span><span class="p">,</span>
<span class="gp">... </span> <span class="s">'https://example.com/app/hello'</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">collapse_http_schemes</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="ow">is</span> <span class="bp">None</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Instead of providing a base URL you can also pass a WSGI environment.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ_or_baseurl</strong> – a WSGI environment dict, a base URL or
base IRI. This is the root of the
application.</li>
<li><strong>path_or_url</strong> – an absolute path from the server root, a
relative path (in which case it’s the path info)
or a full URL. Also accepts IRIs and unicode
parameters.</li>
<li><strong>charset</strong> – the charset for byte data in URLs</li>
<li><strong>errors</strong> – the error handling on decode</li>
<li><strong>collapse_http_schemes</strong> – if set to <cite>False</cite> the algorithm does
not assume that http and https on the
same server point to the same
resource.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.wsgi.host_is_trusted">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">host_is_trusted</tt><big>(</big><em>hostname</em>, <em>trusted_list</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.host_is_trusted" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks if a host is trusted against a list. This also takes care
of port normalization.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></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-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>hostname</strong> – the hostname to check</li>
<li><strong>trusted_list</strong> – a list of hostnames to check against. If a
hostname starts with a dot it will match against
all subdomains as well.</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.wsgi.responder">
<tt class="descclassname">werkzeug.wsgi.</tt><tt class="descname">responder</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#werkzeug.wsgi.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>
<dl class="function">
<dt id="werkzeug.testapp.test_app">
<tt class="descclassname">werkzeug.testapp.</tt><tt class="descname">test_app</tt><big>(</big><em>environ</em>, <em>start_response</em><big>)</big><a class="headerlink" href="#werkzeug.testapp.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.serving</span> <span class="kn">import</span> <span class="n">run_simple</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">werkzeug.testapp</span> <span class="kn">import</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>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
<img class="logo" src="_static/werkzeug.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">WSGI Helpers</a><ul>
<li><a class="reference internal" href="#iterator-stream-helpers">Iterator / Stream Helpers</a></li>
<li><a class="reference internal" href="#environ-helpers">Environ Helpers</a></li>
<li><a class="reference internal" href="#convenience-helpers">Convenience Helpers</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="routing.html" title="previous chapter">URL Routing</a></li>
<li>Next: <a href="http.html" title="next chapter">HTTP Utilities</a></li>
</ul></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/wsgi.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2011, The Werkzeug Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|