Files
hustoj/web/python-docs/library/wsgiref.html
2025-03-09 17:10:56 +08:00

801 lines
103 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>wsgiref --- WSGI 工具和参考实现 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="urllib --- URL 处理模块" href="urllib.html" />
<link rel="prev" title="cgitb --- 用于 CGI 脚本的回溯管理器" href="cgitb.html" />
<link rel="canonical" href="https://docs.python.org/3/library/wsgiref.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code> --- WSGI 工具和参考实现</a><ul>
<li><a class="reference internal" href="#module-wsgiref.util"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code> -- WSGI 环境工具</a></li>
<li><a class="reference internal" href="#module-wsgiref.headers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.headers</span></code> -- WSGI 响应标头工具</a></li>
<li><a class="reference internal" href="#module-wsgiref.simple_server"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.simple_server</span></code> -- 一个简单的 WSGI HTTP 服务器</a></li>
<li><a class="reference internal" href="#module-wsgiref.validate"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.validate</span></code> --- WSGI 一致性检查器</a></li>
<li><a class="reference internal" href="#module-wsgiref.handlers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code> -- 服务器/网关基类</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="cgitb.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cgitb</span></code> --- 用于 CGI 脚本的回溯管理器</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="urllib.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code> --- URL 处理模块</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/wsgiref.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/wsgiref.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="urllib.html" title="urllib --- URL 处理模块"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="cgitb.html" title="cgitb --- 用于 CGI 脚本的回溯管理器"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">互联网协议和支持</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-wsgiref">
<span id="wsgiref-wsgi-utilities-and-reference-implementation"></span><h1><a class="reference internal" href="#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code></a> --- WSGI 工具和参考实现<a class="headerlink" href="#module-wsgiref" title="永久链接至标题"></a></h1>
<hr class="docutils" />
<p>Web 服务器网关接口WSGI是 Web 服务器软件和用 Python 编写的 Web 应用程序之间的标准接口。 具有标准接口能够让支持 WSGI 的应用程序与多种不同的 Web 服务器配合使用。</p>
<p>只有 Web 服务器和编程框架的开发者才需要了解 WSGI 设计的每个细节和边界情况。 你不需要了解 WSGI 的每个细节而只需要安装一个 WSGI 应用程序或编写使用现有框架的 Web 应用程序。</p>
<p><a class="reference internal" href="#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code></a> 是一个 WSGI 规范的参考实现,可被用于将 WSGI 支持添加到 Web 服务器或框架中。 它提供了操作 WSGI 环境变量和响应标头的工具,实现 WSGI 服务器的基类,一个可部署 WSGI 应用程序的演示性 HTTP 服务器,以及一个用于检查 WSGI 服务器和应用程序是否符合 WSGI 规范的验证工具 (<span class="target" id="index-34"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a>)。</p>
<p>参看 <a class="reference external" href="https://wsgi.readthedocs.io/">wsgi.readthedocs.io</a> 获取有关 WSGI 的更多信息,以及教程和其他资源的链接。</p>
<section id="module-wsgiref.util">
<span id="wsgiref-util-wsgi-environment-utilities"></span><h2><a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code></a> -- WSGI 环境工具<a class="headerlink" href="#module-wsgiref.util" title="永久链接至标题"></a></h2>
<p>本模块提供了多种工具配合 WSGI 环境使用。 WSGI 环境就是一个包含 <span class="target" id="index-35"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所描述的 HTTP 请求变量的目录。 所有接受 <em>environ</em> 形参的函数都会预期被得到一个符合 WSGI 规范的目录;请参阅 <span class="target" id="index-36"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 来了解相关规范的细节。</p>
<dl class="function">
<dt id="wsgiref.util.guess_scheme">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">guess_scheme</code><span class="sig-paren">(</span><em class="sig-param">environ</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.guess_scheme" title="永久链接至目标"></a></dt>
<dd><p>返回对于 <code class="docutils literal notranslate"><span class="pre">wsgi.url_scheme</span></code> 应为 &quot;http&quot; 还是 &quot;https&quot; 的猜测,具体方式是在 <em>environ</em> 中检查 <code class="docutils literal notranslate"><span class="pre">HTTPS</span></code> 环境变量。 返回值是一个字符串。</p>
<p>此函数适用于创建一个包装了 CGI 或 CGI 类协议例如 FastCGI 的网关。 通常,提供这种协议的服务器将包括一个 <code class="docutils literal notranslate"><span class="pre">HTTPS</span></code> 变量并会在通过 SSL 接收请求时将其值设为 &quot;1&quot;, &quot;yes&quot;&quot;on&quot;。 这样,此函数会在找到上述值时返回 &quot;https&quot;,否则返回 &quot;http&quot;</p>
</dd></dl>
<dl class="function">
<dt id="wsgiref.util.request_uri">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">request_uri</code><span class="sig-paren">(</span><em class="sig-param">environ</em>, <em class="sig-param">include_query=True</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.request_uri" title="永久链接至目标"></a></dt>
<dd><p>使用 <span class="target" id="index-37"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a>&quot;URL Reconstruction&quot; 一节中的算法返回完整的请求 URL可能包括查询字符串。 如果 <em>include_query</em> 为假值,则结果 URI 中将不包括查询字符串。</p>
</dd></dl>
<dl class="function">
<dt id="wsgiref.util.application_uri">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">application_uri</code><span class="sig-paren">(</span><em class="sig-param">environ</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.application_uri" title="永久链接至目标"></a></dt>
<dd><p>类似于 <a class="reference internal" href="#wsgiref.util.request_uri" title="wsgiref.util.request_uri"><code class="xref py py-func docutils literal notranslate"><span class="pre">request_uri()</span></code></a>,区别在于 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code><code class="docutils literal notranslate"><span class="pre">QUERY_STRING</span></code> 变量会被忽略。 结果为请求所指定的应用程序对象的基准 URI。</p>
</dd></dl>
<dl class="function">
<dt id="wsgiref.util.shift_path_info">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">shift_path_info</code><span class="sig-paren">(</span><em class="sig-param">environ</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.shift_path_info" title="永久链接至目标"></a></dt>
<dd><p>将单个名称从 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 变换为 <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 并返回该名称。 <em>environ</em> 目录将被原地 <em>修改</em>;如果你需要保留原始 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code><code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 不变请使用一个副本。</p>
<p>如果 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 中没有剩余的路径节,则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
<p>通常,此例程被用来处理请求 URI 路径的每个部分,比如说将路径当作是一系列字典键。 此例程会修改传入的环境以使其适合发起调用位于目标 URI 上的其他 WSGI 应用程序,如果有一个 WSGI 应用程序位于 <code class="docutils literal notranslate"><span class="pre">/foo</span></code>,而请求 URI 路径为 <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz</span></code>,且位于 <code class="docutils literal notranslate"><span class="pre">/foo</span></code> 的 WSGI 应用程序调用了 <a class="reference internal" href="#wsgiref.util.shift_path_info" title="wsgiref.util.shift_path_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">shift_path_info()</span></code></a>,它将获得字符串 &quot;bar&quot;,而环境将被更新以适合传递给位于 <code class="docutils literal notranslate"><span class="pre">/foo/bar</span></code> 的 WSGI 应用程序。 也就是说,<code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 将由 <code class="docutils literal notranslate"><span class="pre">/foo</span></code> 变为 <code class="docutils literal notranslate"><span class="pre">/foo/bar</span></code>,而 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 将由 <code class="docutils literal notranslate"><span class="pre">/bar/baz</span></code> 变为 <code class="docutils literal notranslate"><span class="pre">/baz</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 只是 &quot;/&quot; 时,此例程会返回一个空字符串并在 <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 末尾添加一个斜杠,虽然空的路径节通常会被忽略,并且 <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 通常也不以斜杠作为结束。 此行为是有意为之的,用来确保应用程序在使用此例程执行对象遍历时能区分以 <code class="docutils literal notranslate"><span class="pre">/x</span></code> 结束的和以 <code class="docutils literal notranslate"><span class="pre">/x/</span></code> 结束的 URI。</p>
</dd></dl>
<dl class="function">
<dt id="wsgiref.util.setup_testing_defaults">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">setup_testing_defaults</code><span class="sig-paren">(</span><em class="sig-param">environ</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.setup_testing_defaults" title="永久链接至目标"></a></dt>
<dd><p>以简短的默认值更新 <em>environ</em> 用于测试目的。</p>
<p>此例程会添加 WSGI 所需要的各种参数,包括 <code class="docutils literal notranslate"><span class="pre">HTTP_HOST</span></code>, <code class="docutils literal notranslate"><span class="pre">SERVER_NAME</span></code>, <code class="docutils literal notranslate"><span class="pre">SERVER_PORT</span></code>, <code class="docutils literal notranslate"><span class="pre">REQUEST_METHOD</span></code>, <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code>, <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 以及 <span class="target" id="index-38"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 中定义的所有 <code class="docutils literal notranslate"><span class="pre">wsgi.*</span></code> 变量。 它只提供默认值,而不会替换这些变量的现有设置。</p>
<p>此例程的目的是让 WSGI 服务器的单元测试以及应用程序设置测试环境更为容易。 它不应该被实际的 WSGI 服务器或应用程序所使用,因为它用的是假数据!</p>
<p>用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">wsgiref.util</span> <span class="kn">import</span> <span class="n">setup_testing_defaults</span>
<span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>
<span class="c1"># A relatively simple WSGI application. It&#39;s going to print out the</span>
<span class="c1"># environment dictionary after being updated by setup_testing_defaults</span>
<span class="k">def</span> <span class="nf">simple_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="n">setup_testing_defaults</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="n">status</span> <span class="o">=</span> <span class="s1">&#39;200 OK&#39;</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain; charset=utf-8&#39;</span><span class="p">)]</span>
<span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">environ</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="k">with</span> <span class="n">make_server</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">simple_app</span><span class="p">)</span> <span class="k">as</span> <span class="n">httpd</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Serving on port 8000...&quot;</span><span class="p">)</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>
<p>除了上述的环境函数,<a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code></a> 模块还提供了以下辅助工具:</p>
<dl class="function">
<dt id="wsgiref.util.is_hop_by_hop">
<code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">is_hop_by_hop</code><span class="sig-paren">(</span><em class="sig-param">header_name</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.is_hop_by_hop" title="永久链接至目标"></a></dt>
<dd><p>如果 'header_name' 是 <span class="target" id="index-39"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a> 所定义的 HTTP/1.1 &quot;Hop-by-Hop&quot; 标头则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.util.FileWrapper">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.util.</code><code class="sig-name descname">FileWrapper</code><span class="sig-paren">(</span><em class="sig-param">filelike</em>, <em class="sig-param">blksize=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.util.FileWrapper" title="永久链接至目标"></a></dt>
<dd><p>一个将文件类对象转换为 <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> 的包装器。 结果对象同时支持 <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a><a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> 迭代形式,以便兼容 Python 2.1 和 Jython。 当对象被迭代时,可选的 <em>blksize</em> 形参将被反复地传给 <em>filelike</em> 对象的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 方法以获取字节串并输出。 当 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 返回空字节串时,迭代将结束并不可再恢复。</p>
<p>如果 <em>filelike</em> 具有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code> 方法,返回的对象也将具有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code> 方法,并且它将在被调用时发起调用 <em>filelike</em> 对象的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code> 方法。</p>
<p>用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="kn">from</span> <span class="nn">wsgiref.util</span> <span class="kn">import</span> <span class="n">FileWrapper</span>
<span class="c1"># We&#39;re using a StringIO-buffer for as the file-like object</span>
<span class="n">filelike</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s2">&quot;This is an example file-like object&quot;</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span>
<span class="n">wrapper</span> <span class="o">=</span> <span class="n">FileWrapper</span><span class="p">(</span><span class="n">filelike</span><span class="p">,</span> <span class="n">blksize</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">wrapper</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
</pre></div>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.8 版后已移除: </span><a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">序列协议</span></code></a> 的支持已被弃用。</p>
</div>
</dd></dl>
</section>
<section id="module-wsgiref.headers">
<span id="wsgiref-headers-wsgi-response-header-tools"></span><h2><a class="reference internal" href="#module-wsgiref.headers" title="wsgiref.headers: WSGI response header tools."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.headers</span></code></a> -- WSGI 响应标头工具<a class="headerlink" href="#module-wsgiref.headers" title="永久链接至标题"></a></h2>
<p>此模块提供了一个单独的类 <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a>,可以方便地使用一个映射类接口操作 WSGI 响应标头。</p>
<dl class="class">
<dt id="wsgiref.headers.Headers">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.headers.</code><code class="sig-name descname">Headers</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">headers</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.headers.Headers" title="永久链接至目标"></a></dt>
<dd><p>创建一个包装了 <em>headers</em> 的映射类对象,它必须为如 <span class="target" id="index-40"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所描述的由标头名称/值元组构成的列表。 <em>headers</em> 的默认值为一个空列表。</p>
<p><a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象支持典型的映射操作包括 <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code>, <a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delitem__()</span></code></a><a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a>。 对于以上各个方法,映射的键是标头名称(大小写不敏感),而值是关联到该标头名称的第一个值。 设置一个标头会移除该标头的任何现有值,再将一个新值添加到所包装的标头列表末尾。 标头的现有顺序通常会保持不变,只在所包装的列表末尾添加新的标头。</p>
<p>与字典不同,当你试图获取或删除所包装的标头列表中不存在的键时 <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象不会引发错误。 获取一个不存在的标头只是返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>,而删除一个不存在的标头则没有任何影响。</p>
<p><a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象还支持 <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code> 以及 <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> 方法。 如果存在具有多个值的键则 <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> 所返回的列表可能包括多个相同的键。 对 <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象执行 <code class="docutils literal notranslate"><span class="pre">len()</span></code> 的结果与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> 的长度相同,也与所包装的标头列表的长度相同。 实际上,<code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code> 方法只是返回所包装的标头列表的一个副本。</p>
<p><a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象上调用 <code class="docutils literal notranslate"><span class="pre">bytes()</span></code> 将返回适用于作为 HTTP 响应标头来传输的已格式化字节串。 每个标头附带以逗号加空格分隔的值放置在一行中。 每一行都以回车符加换行符结束,且该字节串会以一个空行作为结束。</p>
<p>除了映射接口和格式化特性,<a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">Headers</span></code></a> 对象还具有下列方法用来查询和添加多值标头,以及添加具有 MIME 参数的标头:</p>
<dl class="method">
<dt id="wsgiref.headers.Headers.get_all">
<code class="sig-name descname">get_all</code><span class="sig-paren">(</span><em class="sig-param">name</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.headers.Headers.get_all" title="永久链接至目标"></a></dt>
<dd><p>返回包含指定标头的所有值的列表。</p>
<p>返回的列表项将按它们在原始标头列表中的出现或被添加到实例中的顺序排序,并可能包含重复项。 任何被删除并重新插入的字段总是会被添加到标头列表末尾。 如果给定名称的字段不存在,则返回一个空列表。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.headers.Headers.add_header">
<code class="sig-name descname">add_header</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">value</em>, <em class="sig-param">**_params</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.headers.Headers.add_header" title="永久链接至目标"></a></dt>
<dd><p>添加一个(可能有多个值)标头,带有通过关键字参数指明的可选的 MIME 参数。</p>
<p><em>name</em> 是要添加的标头字段。 可以使用关键字参数来为标头字段设置 MIME 参数。 每个参数必须为字符串或 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 参数名中的下划线会被转换为连字符,因为连字符不可在 Python 标识符中出现,但许多 MIME 参数名都包括连字符。 如果参数值为字符串,它会以 <code class="docutils literal notranslate"><span class="pre">name=&quot;value&quot;</span></code> 的形式被添加到标头值参数中。 如果为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则只会添加参数名。 (这适用于没有值的 MIME 参数。) 示例用法:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">h</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;content-disposition&#39;</span><span class="p">,</span> <span class="s1">&#39;attachment&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;bud.gif&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>以上代码将添加一个这样的标头:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Content</span><span class="o">-</span><span class="n">Disposition</span><span class="p">:</span> <span class="n">attachment</span><span class="p">;</span> <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;bud.gif&quot;</span>
</pre></div>
</div>
</dd></dl>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>headers</em> 形参是可选的。</p>
</div>
</dd></dl>
</section>
<section id="module-wsgiref.simple_server">
<span id="wsgiref-simple-server-a-simple-wsgi-http-server"></span><h2><a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.simple_server</span></code></a> -- 一个简单的 WSGI HTTP 服务器<a class="headerlink" href="#module-wsgiref.simple_server" title="永久链接至标题"></a></h2>
<p>此模块实现了一个简单的 HTTP 服务器 (基于 <a class="reference internal" href="http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a>) 来发布 WSGI 应用程序。 每个服务器实例会在给定的主机名和端口号上发布一个 WSGI 应用程序。 如果你想在一个主机名和端口号上发布多个应用程序,你应当创建一个通过解析 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 来选择每个请求要发起调用哪个应用程序的 WSGI 应用程序。 (例如,使用 <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code></a> 中的 <code class="xref py py-func docutils literal notranslate"><span class="pre">shift_path_info()</span></code> 函数。)</p>
<dl class="function">
<dt id="wsgiref.simple_server.make_server">
<code class="sig-prename descclassname">wsgiref.simple_server.</code><code class="sig-name descname">make_server</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port</em>, <em class="sig-param">app</em>, <em class="sig-param">server_class=WSGIServer</em>, <em class="sig-param">handler_class=WSGIRequestHandler</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.make_server" title="永久链接至目标"></a></dt>
<dd><p>创建一个新的 WSGI 服务器并在 <em>host</em><em>port</em> 上进行监听,接受对 <em>app</em> 的连接。 返回值是所提供的 <em>server_class</em> 的实例,并将使用指定的 <em>handler_class</em> 来处理请求。 <em>app</em> 必须是一个如 <span class="target" id="index-41"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所定义的 WSGI 应用程序对象。</p>
<p>用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span><span class="p">,</span> <span class="n">demo_app</span>
<span class="k">with</span> <span class="n">make_server</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">demo_app</span><span class="p">)</span> <span class="k">as</span> <span class="n">httpd</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Serving HTTP on port 8000...&quot;</span><span class="p">)</span>
<span class="c1"># Respond to requests until process is killed</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
<span class="c1"># Alternative: serve one request, then exit</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">handle_request</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="wsgiref.simple_server.demo_app">
<code class="sig-prename descclassname">wsgiref.simple_server.</code><code class="sig-name descname">demo_app</code><span class="sig-paren">(</span><em class="sig-param">environ</em>, <em class="sig-param">start_response</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.demo_app" title="永久链接至目标"></a></dt>
<dd><p>此函数是一个小巧但完整的 WSGI 应用程序,它返回一个包含消息 &quot;Hello world!&quot; 以及 <em>environ</em> 形参中提供的键/值对的文本页面。 它适用于验证 WSGI 服务器 (例如 <a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.simple_server</span></code></a>) 是否能够正确地运行一个简单的 WSGI 应用程序。</p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.simple_server.WSGIServer">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.simple_server.</code><code class="sig-name descname">WSGIServer</code><span class="sig-paren">(</span><em class="sig-param">server_address</em>, <em class="sig-param">RequestHandlerClass</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIServer" title="永久链接至目标"></a></dt>
<dd><p>创建一个 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a> 实例。 <em>server_address</em> 应当是一个 <code class="docutils literal notranslate"><span class="pre">(host,port)</span></code> 元组,而 <em>RequestHandlerClass</em> 应当是 <a class="reference internal" href="http.server.html#http.server.BaseHTTPRequestHandler" title="http.server.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.server.BaseHTTPRequestHandler</span></code></a> 的子类,它将被用来处理请求。</p>
<p>你通常不需要调用此构造器,因为 <a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_server()</span></code></a> 函数能为你处理所有的细节。</p>
<p><a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a><a class="reference internal" href="http.server.html#http.server.HTTPServer" title="http.server.HTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.server.HTTPServer</span></code></a> 的子类,因此它所有的方法 (例如 <code class="xref py py-meth docutils literal notranslate"><span class="pre">serve_forever()</span></code><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_request()</span></code>) 都是可用的。 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a> 还提供了以下 WSGI 专属的方法:</p>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIServer.set_app">
<code class="sig-name descname">set_app</code><span class="sig-paren">(</span><em class="sig-param">application</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIServer.set_app" title="永久链接至目标"></a></dt>
<dd><p>将可调用对象 <em>application</em> 设为将要接受请求的 WSGI 应用程序。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIServer.get_app">
<code class="sig-name descname">get_app</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIServer.get_app" title="永久链接至目标"></a></dt>
<dd><p>返回当前设置的应用程序可调用对象。</p>
</dd></dl>
<p>但是,你通常不需要使用这些附加的方法,因为 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer.set_app" title="wsgiref.simple_server.WSGIServer.set_app"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_app()</span></code></a> 通常会由 <a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_server()</span></code></a> 来调用,而 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer.get_app" title="wsgiref.simple_server.WSGIServer.get_app"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_app()</span></code></a> 主要是针对请求处理器实例的。</p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.simple_server.WSGIRequestHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.simple_server.</code><code class="sig-name descname">WSGIRequestHandler</code><span class="sig-paren">(</span><em class="sig-param">request</em>, <em class="sig-param">client_address</em>, <em class="sig-param">server</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler" title="永久链接至目标"></a></dt>
<dd><p>为给定的 <em>request</em> 创建一个 HTTP 处理器 (例如套接字)<em>client_address</em> (一个 <code class="docutils literal notranslate"><span class="pre">(host,port)</span></code> 元组),以及 <em>server</em> (<a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a> 实例)。</p>
<p>你不需要直接创建该类的实例;它们会根据 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a> 对象的需要自动创建。 但是,你可以子类化该类并将其作为 <em>handler_class</em> 提供给 <a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_server()</span></code></a> 函数。 一些可能在子类中重载的相关方法:</p>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.get_environ">
<code class="sig-name descname">get_environ</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.get_environ" title="永久链接至目标"></a></dt>
<dd><p>返回包含针对一个请求的 WSGI 环境的字典。 默认实现会拷贝 <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code></a> 对象的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">base_environ</span></code> 字典属性的内容并添加从 HTTP 请求获取的各种标头。 对此方法的每个调用应当返回一个 <span class="target" id="index-42"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所指明的包含所有相关 CGI 环境变量的新字典。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.get_stderr">
<code class="sig-name descname">get_stderr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.get_stderr" title="永久链接至目标"></a></dt>
<dd><p>返回应被用作 <code class="docutils literal notranslate"><span class="pre">wsgi.errors</span></code> 流的对象。 默认实现将只返回 <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code></p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.handle">
<code class="sig-name descname">handle</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.handle" title="永久链接至目标"></a></dt>
<dd><p>处理 HTTP 请求。 默认的实现会使用 <a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code></a> 类创建一个处理器实例来实现实际的 WSGI 应用程序接口。</p>
</dd></dl>
</dd></dl>
</section>
<section id="module-wsgiref.validate">
<span id="wsgiref-validate-wsgi-conformance-checker"></span><h2><a class="reference internal" href="#module-wsgiref.validate" title="wsgiref.validate: WSGI conformance checker."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.validate</span></code></a> --- WSGI 一致性检查器<a class="headerlink" href="#module-wsgiref.validate" title="永久链接至标题"></a></h2>
<p>当创建新的 WSGI 应用程序对象、框架、服务器或中间件时,使用 <a class="reference internal" href="#module-wsgiref.validate" title="wsgiref.validate: WSGI conformance checker."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.validate</span></code></a> 来验证新代码的一致性是很有用的。 此模块提供了一个创建 WSGI 应用程序对象的函数来验证 WSGI 服务器或网关与 WSGI 应用程序对象之间的通信,以便检查双方的协议一致性。</p>
<p>请注意这个工具并不保证完全符合 <span class="target" id="index-43"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a>;此模块没有报告错误并不一定表示不存在错误。 但是,如果此模块确实报告了错误,那么几乎可以肯定服务器或应用程序不是 100% 符合要求的。</p>
<p>此模块是基于 Ian Bicking 的 &quot;Python Paste&quot; 库的 <code class="xref py py-mod docutils literal notranslate"><span class="pre">paste.lint</span></code> 模块。</p>
<dl class="function">
<dt id="wsgiref.validate.validator">
<code class="sig-prename descclassname">wsgiref.validate.</code><code class="sig-name descname">validator</code><span class="sig-paren">(</span><em class="sig-param">application</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.validate.validator" title="永久链接至目标"></a></dt>
<dd><p>包装 <em>application</em> 并返回一个新的 WSGI 应用程序对象。 返回的应用程序将转发所有请求到原始的 <em>application</em>,并将检查 <em>application</em> 和发起调用它的服务器是否都符合 WSGI 规范和 <span class="target" id="index-44"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a></p>
<p>任何被检测到的不一致性都会导致引发 <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a>;但是请注意,如何对待这些错误取决于具体服务器。 例如,<a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.simple_server</span></code></a> 和其他基于 <a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code></a> 的服务器(它们没有重载错误处理方法来做其他操作)将简单地输出一条消息报告发生了错误,并将回溯转给 <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> 或者其他错误数据流。</p>
<p>这个包装器也可能会使用 <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> 模块生成输出来指明存在问题但实际上未被 <span class="target" id="index-45"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所禁止的行为。 除非它们被 Python 命令行选项或 <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> API 所屏蔽,否则任何此类警告都将被写入到 <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> (<em>不是</em> <code class="docutils literal notranslate"><span class="pre">wsgi.errors</span></code>,除非它们恰好是同一个对象)。</p>
<p>用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">wsgiref.validate</span> <span class="kn">import</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>
<span class="c1"># Our callable object which is intentionally not compliant to the</span>
<span class="c1"># standard, so the validator is going to break</span>
<span class="k">def</span> <span class="nf">simple_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="n">status</span> <span class="o">=</span> <span class="s1">&#39;200 OK&#39;</span> <span class="c1"># HTTP Status</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain&#39;</span><span class="p">)]</span> <span class="c1"># HTTP Headers</span>
<span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
<span class="c1"># This is going to break because we need to return a list, and</span>
<span class="c1"># the validator is going to inform us</span>
<span class="k">return</span> <span class="sa">b</span><span class="s2">&quot;Hello World&quot;</span>
<span class="c1"># This is the application wrapped in a validator</span>
<span class="n">validator_app</span> <span class="o">=</span> <span class="n">validator</span><span class="p">(</span><span class="n">simple_app</span><span class="p">)</span>
<span class="k">with</span> <span class="n">make_server</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">validator_app</span><span class="p">)</span> <span class="k">as</span> <span class="n">httpd</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Listening on port 8000....&quot;</span><span class="p">)</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>
</section>
<section id="module-wsgiref.handlers">
<span id="wsgiref-handlers-server-gateway-base-classes"></span><h2><a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code></a> -- 服务器/网关基类<a class="headerlink" href="#module-wsgiref.handlers" title="永久链接至标题"></a></h2>
<p>此模块提供了用于实现 WSGI 服务器和网关的处理器基类。 这些基类可处理大部分与 WSGI 应用程序通信的工作,只要给予它们一个带有输入、输出和错误流的 CGI 类环境。</p>
<dl class="class">
<dt id="wsgiref.handlers.CGIHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">CGIHandler</code><a class="headerlink" href="#wsgiref.handlers.CGIHandler" title="永久链接至目标"></a></dt>
<dd><p>通过 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code><code class="docutils literal notranslate"><span class="pre">os.environ</span></code> 发起基于 CGI 的调用。 这在当你有一个 WSGI 应用程序并想将其作为 CGI 脚本运行时很有用处。 只需发起调用 <code class="docutils literal notranslate"><span class="pre">CGIHandler().run(app)</span></code>,其中 <code class="docutils literal notranslate"><span class="pre">app</span></code> 是你想要发起调用的 WSGI 应用程序。</p>
<p>该类是 <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseCGIHandler</span></code></a> 的子类,它将设置 <code class="docutils literal notranslate"><span class="pre">wsgi.run_once</span></code> 为真值,<code class="docutils literal notranslate"><span class="pre">wsgi.multithread</span></code> 为假值,<code class="docutils literal notranslate"><span class="pre">wsgi.multiprocess</span></code> 为真值,并总是使用 <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a><a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> 来获取所需的 CGI 流和环境。</p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.handlers.IISCGIHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">IISCGIHandler</code><a class="headerlink" href="#wsgiref.handlers.IISCGIHandler" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a> 的一个专门化替代,用于在 Microsoft 的 IIS Web 服务器上部署,无需设置 config allowPathInfo 选项 (IIS&gt;=7) 或 metabase allowPathInfoForScriptMappings (IIS&lt;7)。</p>
<p>默认情况下IIS 给出的 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> 会与前面的 <code class="docutils literal notranslate"><span class="pre">SCRIPT_NAME</span></code> 重复,导致想要实现路由的 WSGI 应用程序出现问题。 这个处理器会去除任何这样的重复路径。</p>
<p>IIS 可被配置为传递正确的 <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code>,但这会导致另一个 <code class="docutils literal notranslate"><span class="pre">PATH_TRANSLATED</span></code> 出错的问题。 幸运的是这个变量很少被使用并且不被 WSGI 所保证。 但是在 IIS&lt;7 上,这个设置只能在 vhost 层级上进行,影响到所有其他脚本映射,其中许多在受 <code class="docutils literal notranslate"><span class="pre">PATH_TRANSLATED</span></code> 问题影响时都会中断运行。 因此 IIS&lt;7 的部署几乎从不附带这样的修正(即使 IIS7 也很少使用它,因为它仍然不带 UI</p>
<p>CGI 代码没有办法确定该选项是否已设置,因此提供了一个单独的处理器类。 它的用法与 <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a> 相同,即通过调用 <code class="docutils literal notranslate"><span class="pre">IISCGIHandler().run(app)</span></code>,其中 <code class="docutils literal notranslate"><span class="pre">app</span></code> 是你想要发起调用的 WSGI 应用程序。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
<dl class="class">
<dt id="wsgiref.handlers.BaseCGIHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">BaseCGIHandler</code><span class="sig-paren">(</span><em class="sig-param">stdin</em>, <em class="sig-param">stdout</em>, <em class="sig-param">stderr</em>, <em class="sig-param">environ</em>, <em class="sig-param">multithread=True</em>, <em class="sig-param">multiprocess=False</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseCGIHandler" title="永久链接至目标"></a></dt>
<dd><p>类似于 <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a>,但是改用 <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a><a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> 模块CGI 环境和 I/O 流会被显式地指定。 <em>multithread</em><em>multiprocess</em> 值被用来为处理器实例所运行的任何应用程序设置 <code class="docutils literal notranslate"><span class="pre">wsgi.multithread</span></code><code class="docutils literal notranslate"><span class="pre">wsgi.multiprocess</span></code> 旗标。</p>
<p>该类是 <a class="reference internal" href="#wsgiref.handlers.SimpleHandler" title="wsgiref.handlers.SimpleHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHandler</span></code></a> 的子类,旨在用于 HTTP &quot;原始服务器&quot; 以外的软件。 如果你在编写一个网关协议实现(例如 CGI, FastCGI, SCGI 等等),它使用 <code class="docutils literal notranslate"><span class="pre">Status:</span></code> 标头来发布 HTTP 状态,你可能会想要子类化该类而不是 <a class="reference internal" href="#wsgiref.handlers.SimpleHandler" title="wsgiref.handlers.SimpleHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHandler</span></code></a></p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.handlers.SimpleHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">SimpleHandler</code><span class="sig-paren">(</span><em class="sig-param">stdin</em>, <em class="sig-param">stdout</em>, <em class="sig-param">stderr</em>, <em class="sig-param">environ</em>, <em class="sig-param">multithread=True</em>, <em class="sig-param">multiprocess=False</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.SimpleHandler" title="永久链接至目标"></a></dt>
<dd><p>类似于 <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseCGIHandler</span></code></a>,但被设计用于 HTTP 原始服务器。 如果你在编写一个 HTTP 服务器实现,你可能会想要子类化该类而不是 <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseCGIHandler</span></code></a></p>
<p>该类是 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 的子类。 它重载了 <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_stdin()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_stderr()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_cgi_vars()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">_write()</span></code><code class="xref py py-meth docutils literal notranslate"><span class="pre">_flush()</span></code> 方法以支持通过构造器显式地设置环境和流。 所提供的环境和流存储在 <code class="xref py py-attr docutils literal notranslate"><span class="pre">stdin</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">stdout</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">stderr</span></code><code class="xref py py-attr docutils literal notranslate"><span class="pre">environ</span></code> 属性中。</p>
<p><em>stdout</em><a class="reference internal" href="io.html#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> 方法应该完整写入每个数据块,与 <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> 一样。</p>
</dd></dl>
<dl class="class">
<dt id="wsgiref.handlers.BaseHandler">
<em class="property">class </em><code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">BaseHandler</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler" title="永久链接至目标"></a></dt>
<dd><p>这是适用于运行 WSGI 应用程序的抽象基类。 每个实例将处理一个单独的 HTTP 请求,不过在原则上你也可以创建一个可针对多个请求重用的子类。</p>
<p><a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 实例只有一个方法提供给外部使用:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.run">
<code class="sig-name descname">run</code><span class="sig-paren">(</span><em class="sig-param">app</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.run" title="永久链接至目标"></a></dt>
<dd><p>运行指定的 WSGI 应用程序 <em>app</em></p>
</dd></dl>
<p>所有的 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 其他方法都是在运行应用程序过程中由该方法发起调用的,因此主要是为了允许定制运行过程。</p>
<p>以下方法必须在子类中被重载:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler._write">
<code class="sig-name descname">_write</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler._write" title="永久链接至目标"></a></dt>
<dd><p>缓冲字节数据 <em>data</em> 以便传输给客户端。 如果此方法真的传输了数据也是可以的;<a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 只有在底层系统真有这样的区分时才会区分写入和刷新操作以提高效率。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler._flush">
<code class="sig-name descname">_flush</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler._flush" title="永久链接至目标"></a></dt>
<dd><p>强制将缓冲的数据传输给客户端。 如果此方法无任何操作也是可以的(例如,<a class="reference internal" href="#wsgiref.handlers.BaseHandler._write" title="wsgiref.handlers.BaseHandler._write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_write()</span></code></a> 实际上已发送了数据)。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_stdin">
<code class="sig-name descname">get_stdin</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_stdin" title="永久链接至目标"></a></dt>
<dd><p>返回一个适合被用作当前所处理请求的 <code class="docutils literal notranslate"><span class="pre">wsgi.input</span></code> 的输入流对象。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_stderr">
<code class="sig-name descname">get_stderr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_stderr" title="永久链接至目标"></a></dt>
<dd><p>返回一个适合被用作当前所处理请求的 <code class="docutils literal notranslate"><span class="pre">wsgi.errors</span></code> 的输出流对象。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.add_cgi_vars">
<code class="sig-name descname">add_cgi_vars</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.add_cgi_vars" title="永久链接至目标"></a></dt>
<dd><p>将当前请求的 CGI 变量插入到 <code class="xref py py-attr docutils literal notranslate"><span class="pre">environ</span></code> 属性。</p>
</dd></dl>
<p>以下是一些你可能会想要重载的其他方法。 但这只是个简略的列表,它不包括每个可被重载的方法。 你应当在在尝试创建自定义的 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 子类之前参阅文档字符串和源代码来了解更多信息。</p>
<p>用于自定义 WSGI 环境的属性和方法:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_multithread">
<code class="sig-name descname">wsgi_multithread</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_multithread" title="永久链接至目标"></a></dt>
<dd><p>用于 <code class="docutils literal notranslate"><span class="pre">wsgi.multithread</span></code> 环境变量的值。 它在 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 中默认为真值,但在其他子类中可能有不同的默认值(或是由构造器来设置)。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_multiprocess">
<code class="sig-name descname">wsgi_multiprocess</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_multiprocess" title="永久链接至目标"></a></dt>
<dd><p>用于 <code class="docutils literal notranslate"><span class="pre">wsgi.multiprocess</span></code> 环境变量的值。 它在 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 中默认为真值,但在其他子类中可能有不同的默认值(或是由构造器来设置)。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_run_once">
<code class="sig-name descname">wsgi_run_once</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_run_once" title="永久链接至目标"></a></dt>
<dd><p>用于 <code class="docutils literal notranslate"><span class="pre">wsgi.run_once</span></code> 环境变量的值。 它在 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 中默认为假值,但在 <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a> 中默认为真值。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.os_environ">
<code class="sig-name descname">os_environ</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.os_environ" title="永久链接至目标"></a></dt>
<dd><p>要包括在每个请求的 WSGI 环境中的默认环境变量。 在默认情况下,这是当 <a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code></a> 被导入时的 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> 的一个副本,但也可以在类或实例层级上创建它们自己的子类。 请注意该字典应当被当作是只读的,因为默认值会在多个类和实际之间共享。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.server_software">
<code class="sig-name descname">server_software</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.server_software" title="永久链接至目标"></a></dt>
<dd><p>如果设置了 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><code class="xref py py-attr docutils literal notranslate"><span class="pre">origin_server</span></code></a> 属性,该属性的值会被用来设置默认的 <code class="docutils literal notranslate"><span class="pre">SERVER_SOFTWARE</span></code> WSGI 环境变量,并且还会被用来设置 HTTP 响应中默认的 <code class="docutils literal notranslate"><span class="pre">Server:</span></code> 标头。 它会被不是 HTTP 原始服务器的处理器所忽略 (例如 <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseCGIHandler</span></code></a><a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a>)。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>名称 &quot;Python&quot; 会被替换为实现专属的名称如 &quot;CPython&quot;, &quot;Jython&quot; 等等。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_scheme">
<code class="sig-name descname">get_scheme</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_scheme" title="永久链接至目标"></a></dt>
<dd><p>返回当前请求所使用的 URL 方案。 默认的实现会使用来自 <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code></a><code class="xref py py-func docutils literal notranslate"><span class="pre">guess_scheme()</span></code> 函数根据当前请求的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">environ</span></code> 变量来猜测方案应该是 &quot;http&quot; 还是 &quot;https&quot;</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.setup_environ">
<code class="sig-name descname">setup_environ</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.setup_environ" title="永久链接至目标"></a></dt>
<dd><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">environ</span></code> 属性设为填充了完整内容的 WSGI 环境。 默认的实现会使用上述的所有方法和属性,加上 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.get_stdin" title="wsgiref.handlers.BaseHandler.get_stdin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_stdin()</span></code></a>, <a class="reference internal" href="#wsgiref.handlers.BaseHandler.get_stderr" title="wsgiref.handlers.BaseHandler.get_stderr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_stderr()</span></code></a><a class="reference internal" href="#wsgiref.handlers.BaseHandler.add_cgi_vars" title="wsgiref.handlers.BaseHandler.add_cgi_vars"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_cgi_vars()</span></code></a> 方法以及 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="wsgiref.handlers.BaseHandler.wsgi_file_wrapper"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wsgi_file_wrapper</span></code></a> 属性。 它还会插入一个 <code class="docutils literal notranslate"><span class="pre">SERVER_SOFTWARE</span></code> 键,如果该键还不存在的话,只要 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><code class="xref py py-attr docutils literal notranslate"><span class="pre">origin_server</span></code></a> 属性为真值并且设置了 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.server_software" title="wsgiref.handlers.BaseHandler.server_software"><code class="xref py py-attr docutils literal notranslate"><span class="pre">server_software</span></code></a> 属性。</p>
</dd></dl>
<p>用于自定义异常处理的方法和属性:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.log_exception">
<code class="sig-name descname">log_exception</code><span class="sig-paren">(</span><em class="sig-param">exc_info</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.log_exception" title="永久链接至目标"></a></dt>
<dd><p><em>exc_info</em> 元素记录到服务器日志。 <em>exc_info</em> 是一个 <code class="docutils literal notranslate"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></code> 元组。 默认的实现会简单地将回溯信息写入请求的 <code class="docutils literal notranslate"><span class="pre">wsgi.errors</span></code> 流并刷新它。 子类可以重写此方法来修改格式或重设输出目标,通过邮件将回溯信息发给管理员,或执行任何其他符合要求的动作。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.traceback_limit">
<code class="sig-name descname">traceback_limit</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.traceback_limit" title="永久链接至目标"></a></dt>
<dd><p>要包括在默认 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.log_exception" title="wsgiref.handlers.BaseHandler.log_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">log_exception()</span></code></a> 方法的回溯信息输出中的最大帧数。 如果为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则会包括所有的帧。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.error_output">
<code class="sig-name descname">error_output</code><span class="sig-paren">(</span><em class="sig-param">environ</em>, <em class="sig-param">start_response</em><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_output" title="永久链接至目标"></a></dt>
<dd><p>此方法是一个为用户生成错误页面的 WSGI 应用程序。 它仅当将标头发送给客户端之前发生错误时会被发起调用。</p>
<p>此方法可使用 <code class="docutils literal notranslate"><span class="pre">sys.exc_info()</span></code> 来访问当前错误信息,并应当在调用 <em>start_response</em> 时将该信息传递给它(如 <span class="target" id="index-46"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a>&quot;Error Handling&quot; 部分所描述的)。</p>
<p>默认的实现只是使用 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_status" title="wsgiref.handlers.BaseHandler.error_status"><code class="xref py py-attr docutils literal notranslate"><span class="pre">error_status</span></code></a>, <a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_headers" title="wsgiref.handlers.BaseHandler.error_headers"><code class="xref py py-attr docutils literal notranslate"><span class="pre">error_headers</span></code></a>, 和 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_body" title="wsgiref.handlers.BaseHandler.error_body"><code class="xref py py-attr docutils literal notranslate"><span class="pre">error_body</span></code></a> 属性来生成一个输出页面。 子类可以重写此方法来产生更动态化的错误输出。</p>
<p>但是请注意,从安全角度看来是不建议将诊断信息暴露给任何用户的;理想的做法是你应当通过特别处理来启用诊断输出,因此默认的实现并不包括这样的内容。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_status">
<code class="sig-name descname">error_status</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_status" title="永久链接至目标"></a></dt>
<dd><p>用于错误响应的 HTTP 状态。 这应当是一个在 <span class="target" id="index-47"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 中定义的字符串;它默认为代码 500 以相应的消息。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_headers">
<code class="sig-name descname">error_headers</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_headers" title="永久链接至目标"></a></dt>
<dd><p>用于错误响应的 HTTP 标头。 这应当是由 WSGI 响应标头 (<code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> 元组) 构成的列表,如 <span class="target" id="index-48"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> 所定义的。 默认的列表只是将内容类型设为 <code class="docutils literal notranslate"><span class="pre">text/plain</span></code></p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_body">
<code class="sig-name descname">error_body</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_body" title="永久链接至目标"></a></dt>
<dd><p>错误响应体。 这应当是一个 HTTP 响应体字节串。 它默认为纯文本 &quot;A server error occurred. Please contact the administrator.&quot;</p>
</dd></dl>
<p>用于 <span class="target" id="index-49"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a>&quot;可选的平台专属文件处理&quot; 特性的方法和属性:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_file_wrapper">
<code class="sig-name descname">wsgi_file_wrapper</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="永久链接至目标"></a></dt>
<dd><p>一个 <code class="docutils literal notranslate"><span class="pre">wsgi.file_wrapper</span></code> 工厂对象,或为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 该属性的默认值是 <a class="reference internal" href="#wsgiref.util.FileWrapper" title="wsgiref.util.FileWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">wsgiref.util.FileWrapper</span></code></a> 类。</p>
</dd></dl>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.sendfile">
<code class="sig-name descname">sendfile</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.BaseHandler.sendfile" title="永久链接至目标"></a></dt>
<dd><p>重载以实现平台专属的文件传输。 此方法仅在应用程序的返回值是由 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="wsgiref.handlers.BaseHandler.wsgi_file_wrapper"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wsgi_file_wrapper</span></code></a> 属性指定的类的实例时会被调用。 如果它能够成功地传输文件则应当返回真值,以使得默认的传输代码将不会被执行。 此方法的默认实现只会返回假值。</p>
</dd></dl>
<p>杂项方法和属性:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.origin_server">
<code class="sig-name descname">origin_server</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.origin_server" title="永久链接至目标"></a></dt>
<dd><p>该属性在处理器的 <a class="reference internal" href="#wsgiref.handlers.BaseHandler._write" title="wsgiref.handlers.BaseHandler._write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_write()</span></code></a><a class="reference internal" href="#wsgiref.handlers.BaseHandler._flush" title="wsgiref.handlers.BaseHandler._flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_flush()</span></code></a> 被用于同客户端直接通信而不是通过需要 HTTP 状态为某种特殊 <code class="docutils literal notranslate"><span class="pre">Status:</span></code> 标头的 CGI 类网关协议时应当被设为真值</p>
<p>该属性在 <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> 中默认为真值,但在 <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseCGIHandler</span></code></a><a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a> 中则为假值。</p>
</dd></dl>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.http_version">
<code class="sig-name descname">http_version</code><a class="headerlink" href="#wsgiref.handlers.BaseHandler.http_version" title="永久链接至目标"></a></dt>
<dd><p>如果 <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><code class="xref py py-attr docutils literal notranslate"><span class="pre">origin_server</span></code></a> 为真值,则该字符串属性会被用来设置给客户端的响应的 HTTP 版本。 它的默认值为 <code class="docutils literal notranslate"><span class="pre">&quot;1.0&quot;</span></code></p>
</dd></dl>
</dd></dl>
<dl class="function">
<dt id="wsgiref.handlers.read_environ">
<code class="sig-prename descclassname">wsgiref.handlers.</code><code class="sig-name descname">read_environ</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#wsgiref.handlers.read_environ" title="永久链接至目标"></a></dt>
<dd><p>将来自 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> 的 CGI 变量转码为 <span class="target" id="index-50"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> &quot;bytes in unicode&quot; 字符串,返回一个新的字典。 此函数被 <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CGIHandler</span></code></a><a class="reference internal" href="#wsgiref.handlers.IISCGIHandler" title="wsgiref.handlers.IISCGIHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">IISCGIHandler</span></code></a> 用来替代直接使用 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>,后者不一定在所有使用 Python 3 的平台和 Web 服务器上都符合 WSGI 标准 -- 特别是当 OS 的实际环境为 Unicode 时 (例如 Windows),或者当环境为字节数据,但被 Python 用来解码它的系统编码格式不是 ISO-8859-1 时 (例如使用 UTF-8 的 Unix 系统)。</p>
<p>如果你要实现自己的基于 CGI 的处理器,你可能会想要使用此例程而不是简单地从 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code> 直接拷贝值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
</section>
<section id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题"></a></h2>
<p>这是一个可运行的 &quot;Hello World&quot; WSGI 应用程序:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>
<span class="c1"># Every WSGI application must have an application object - a callable</span>
<span class="c1"># object that accepts two arguments. For that purpose, we&#39;re going to</span>
<span class="c1"># use a function (note that you&#39;re not limited to a function, you can</span>
<span class="c1"># use a class for example). The first argument passed to the function</span>
<span class="c1"># is a dictionary containing CGI-style environment variables and the</span>
<span class="c1"># second variable is the callable object.</span>
<span class="k">def</span> <span class="nf">hello_world_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="n">status</span> <span class="o">=</span> <span class="s1">&#39;200 OK&#39;</span> <span class="c1"># HTTP Status</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain; charset=utf-8&#39;</span><span class="p">)]</span> <span class="c1"># HTTP Headers</span>
<span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
<span class="c1"># The returned object is going to be printed</span>
<span class="k">return</span> <span class="p">[</span><span class="sa">b</span><span class="s2">&quot;Hello World&quot;</span><span class="p">]</span>
<span class="k">with</span> <span class="n">make_server</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">hello_world_app</span><span class="p">)</span> <span class="k">as</span> <span class="n">httpd</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Serving on port 8000...&quot;</span><span class="p">)</span>
<span class="c1"># Serve until process is killed</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
<p>一个发布当前目录的 WSGI 应用程序示例,接受通过命令行指定可选的目录和端口号 (默认值: 8000):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">Small wsgiref based web server. Takes a path to serve from and an</span>
<span class="sd">optional port number (defaults to 8000), then tries to serve files.</span>
<span class="sd">Mime types are guessed from the file names, 404 errors are raised</span>
<span class="sd">if the file is not found. Used for the make serve target in Doc.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="kn">from</span> <span class="nn">wsgiref</span> <span class="kn">import</span> <span class="n">simple_server</span><span class="p">,</span> <span class="n">util</span>
<span class="k">def</span> <span class="nf">app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">respond</span><span class="p">):</span>
<span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">environ</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">][</span><span class="mi">1</span><span class="p">:])</span>
<span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fn</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">sep</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
<span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s1">&#39;index.html&#39;</span><span class="p">)</span>
<span class="nb">type</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="n">fn</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
<span class="n">respond</span><span class="p">(</span><span class="s1">&#39;200 OK&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="p">)])</span>
<span class="k">return</span> <span class="n">util</span><span class="o">.</span><span class="n">FileWrapper</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">respond</span><span class="p">(</span><span class="s1">&#39;404 Not Found&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain&#39;</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">[</span><span class="sa">b</span><span class="s1">&#39;not found&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
<span class="n">port</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">8000</span>
<span class="n">httpd</span> <span class="o">=</span> <span class="n">simple_server</span><span class="o">.</span><span class="n">make_server</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">app</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Serving </span><span class="si">{}</span><span class="s2"> on port </span><span class="si">{}</span><span class="s2">, control-C to stop&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">port</span><span class="p">))</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Shutting down.&quot;</span><span class="p">)</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">server_close</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code> --- WSGI 工具和参考实现</a><ul>
<li><a class="reference internal" href="#module-wsgiref.util"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.util</span></code> -- WSGI 环境工具</a></li>
<li><a class="reference internal" href="#module-wsgiref.headers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.headers</span></code> -- WSGI 响应标头工具</a></li>
<li><a class="reference internal" href="#module-wsgiref.simple_server"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.simple_server</span></code> -- 一个简单的 WSGI HTTP 服务器</a></li>
<li><a class="reference internal" href="#module-wsgiref.validate"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.validate</span></code> --- WSGI 一致性检查器</a></li>
<li><a class="reference internal" href="#module-wsgiref.handlers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref.handlers</span></code> -- 服务器/网关基类</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="cgitb.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cgitb</span></code> --- 用于 CGI 脚本的回溯管理器</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="urllib.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code> --- URL 处理模块</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/wsgiref.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="urllib.html" title="urllib --- URL 处理模块"
>下一页</a> |</li>
<li class="right" >
<a href="cgitb.html" title="cgitb --- 用于 CGI 脚本的回溯管理器"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="internet.html" >互联网协议和支持</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>