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

736 lines
72 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>http.client --- HTTP 协议客户端 &#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="ftplib --- FTP 协议客户端" href="ftplib.html" />
<link rel="prev" title="http --- HTTP 模块" href="http.html" />
<link rel="canonical" href="https://docs.python.org/3/library/http.client.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">http.client</span></code> --- HTTP 协议客户端</a><ul>
<li><a class="reference internal" href="#httpconnection-objects">HTTPConnection 对象</a></li>
<li><a class="reference internal" href="#httpresponse-objects">HTTPResponse 对象</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
<li><a class="reference internal" href="#httpmessage-objects">HTTPMessage 对象</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="http.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http</span></code> --- HTTP 模块</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="ftplib.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code> --- FTP 协议客户端</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/http.client.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/http.client.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="ftplib.html" title="ftplib --- FTP 协议客户端"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="http.html" title="http --- HTTP 模块"
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-http.client">
<span id="http-client-http-protocol-client"></span><h1><a class="reference internal" href="#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> --- HTTP 协议客户端<a class="headerlink" href="#module-http.client" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/http/client.py">Lib/http/client.py</a></p>
<span class="target" id="index-0"></span><hr class="docutils" id="index-1" />
<p>这个模块定义了实现 HTTP 和 HTTPS 协议客户端的类。 它通常不直接使用 --- 模块 <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a> 用它来处理使用 HTTP 和 HTTPS 的 URL。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p>对于更高级别的 HTTP 客户端接口,建议使用 <a class="reference external" href="https://requests.readthedocs.io/en/master/">Requests</a></p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>HTTPS 支持仅在编译 Python 时启用了 SSL 支持的情况下(通过 <a class="reference internal" href="ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> 模块)可用。</p>
</div>
<p>该模块支持以下类:</p>
<dl class="class">
<dt id="http.client.HTTPConnection">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPConnection</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <span class="optional">[</span><em class="sig-param">timeout</em>, <span class="optional">]</span><em class="sig-param">source_address=None</em>, <em class="sig-param">blocksize=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 的实例代表与 HTTP 的一个连接事务。 它的实例化应当传入一个主机和可选的端口号。 如果没有传入端口号,如果主机字符串的形式为 <code class="docutils literal notranslate"><span class="pre">主机:端口</span></code> 则会从中提取端口,否则将使用默认的 HTTP 端口80。 如果给出了可选的 <em>timeout</em> 参数,则阻塞操作(例如连接尝试)将在指定的秒数之后超时(如果未给出,则使用全局默认超时设置)。 可选的 <em>source_address</em> 参数可以为一个 (主机, 端口) 元组,用作进行 HTTP 连接的源地址。 可选的 <em>blocksize</em> 参数可以字节为单位设置缓冲区的大小,用来发送文件类消息体。</p>
<p>举个例子,以下调用都是创建连接到同一主机和端口的服务器的实例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">h1</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h2</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org:80&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h3</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h4</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了*source_address* 参数</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数,不再支持 HTTP 0.9 风格的“简单响应”。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加了 <em>blocksize</em> 参数。</p>
</div>
</dd></dl>
<dl class="class">
<dt id="http.client.HTTPSConnection">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPSConnection</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <em class="sig-param">key_file=None</em>, <em class="sig-param">cert_file=None</em>, <span class="optional">[</span><em class="sig-param">timeout</em>, <span class="optional">]</span><em class="sig-param">source_address=None</em>, <em class="sig-param">*</em>, <em class="sig-param">context=None</em>, <em class="sig-param">check_hostname=None</em>, <em class="sig-param">blocksize=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPSConnection" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 的子类,使用 SSL 与安全服务器进行通信。 默认端口为 <code class="docutils literal notranslate"><span class="pre">443</span></code>。 如果指定了 <em>context</em>,它必须为一个描述 SSL 各选项的 <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 实例。</p>
<p>请参阅 <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">安全考量</span></a> 了解有关最佳实践的更多信息。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了 <em>source_address</em>, <em>context</em><em>check_hostname</em></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>这个类目前会在可能的情况下(即如果 <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a> 为真值)支持 HTTPS 虚拟主机。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数,不再支持 HTTP 0.9 风格的“简单响应”。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4.3 版更改: </span>目前这个类在默认情况下会执行所有必要的证书和主机检查。 要回复到先前的非验证行为,可以将 <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl._create_unverified_context()</span></code> 传递给 <em>context</em> 参数。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.8 版更改: </span>该类现在对于默认的 <em>context</em> 或在传入 <em>cert_file</em> 并附带自定义 <em>context</em> 时会启用 TLS 1.3 <a class="reference internal" href="ssl.html#ssl.SSLContext.post_handshake_auth" title="ssl.SSLContext.post_handshake_auth"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.post_handshake_auth</span></code></a></p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.6 版后已移除: </span><em>key_file</em><em>cert_file</em> 已弃用并转而推荐 <em>context</em>。 请改用 <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> 或让 <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> 为你选择系统所信任的 CA 证书。</p>
<p><em>check_hostname</em> 参数也已弃用;应当改用 <em>context</em><a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> 属性。</p>
</div>
</dd></dl>
<dl class="class">
<dt id="http.client.HTTPResponse">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPResponse</code><span class="sig-paren">(</span><em class="sig-param">sock</em>, <em class="sig-param">debuglevel=0</em>, <em class="sig-param">method=None</em>, <em class="sig-param">url=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse" title="永久链接至目标"></a></dt>
<dd><p>在成功连接后返回类的实例,而不是由用户直接实例化。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数不再支持HTTP 0.9 风格的“简单响应”。</p>
</div>
</dd></dl>
<p>这个模块定义了以下函数:</p>
<dl class="function">
<dt id="http.client.parse_headers">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">parse_headers</code><span class="sig-paren">(</span><em class="sig-param">fp</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.parse_headers" title="永久链接至目标"></a></dt>
<dd><p>从文件指针 <em>fp</em> 中解析头部信息,该文件代表 HTTP 请求/响应。该文件必须是 <code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code> reader 对象(即不是文本),并且必须提供符合 <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a> 格式的头部。</p>
<p>该函数返回 <code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> 的实例,带有头部各个字段,但不带正文数据(与 <a class="reference internal" href="#http.client.HTTPResponse.msg" title="http.client.HTTPResponse.msg"><code class="xref py py-attr docutils literal notranslate"><span class="pre">HTTPResponse.msg</span></code></a><a class="reference internal" href="http.server.html#http.server.BaseHTTPRequestHandler.headers" title="http.server.BaseHTTPRequestHandler.headers"><code class="xref py py-attr docutils literal notranslate"><span class="pre">http.server.BaseHTTPRequestHandler.headers</span></code></a> 一样)。返回之后,文件指针 <em>fp</em> 已为读取 HTTP 正文做好准备了。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p><a class="reference internal" href="#http.client.parse_headers" title="http.client.parse_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_headers()</span></code></a> 不会解析 HTTP 消息的开始行;只会解析各 <code class="docutils literal notranslate"><span class="pre">Name:</span> <span class="pre">value</span></code> 行。文件必须为读取这些字段做好准备,所以在调用该函数之前,第一行应该已经被读取过了。</p>
</div>
</dd></dl>
<p>下列异常可以适当地被引发:</p>
<dl class="exception">
<dt id="http.client.HTTPException">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPException</code><a class="headerlink" href="#http.client.HTTPException" title="永久链接至目标"></a></dt>
<dd><p>此模块中其他异常的基类。 它是 <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.NotConnected">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">NotConnected</code><a class="headerlink" href="#http.client.NotConnected" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.InvalidURL">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">InvalidURL</code><a class="headerlink" href="#http.client.InvalidURL" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类,如果给出了一个非数字或为空值的端口就会被引发。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.UnknownProtocol">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnknownProtocol</code><a class="headerlink" href="#http.client.UnknownProtocol" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.UnknownTransferEncoding">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnknownTransferEncoding</code><a class="headerlink" href="#http.client.UnknownTransferEncoding" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.UnimplementedFileMode">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnimplementedFileMode</code><a class="headerlink" href="#http.client.UnimplementedFileMode" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.IncompleteRead">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">IncompleteRead</code><a class="headerlink" href="#http.client.IncompleteRead" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.ImproperConnectionState">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">ImproperConnectionState</code><a class="headerlink" href="#http.client.ImproperConnectionState" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.CannotSendRequest">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">CannotSendRequest</code><a class="headerlink" href="#http.client.CannotSendRequest" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.CannotSendHeader">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">CannotSendHeader</code><a class="headerlink" href="#http.client.CannotSendHeader" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.ResponseNotReady">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">ResponseNotReady</code><a class="headerlink" href="#http.client.ResponseNotReady" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.BadStatusLine">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">BadStatusLine</code><a class="headerlink" href="#http.client.BadStatusLine" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。 如果服务器反馈了一个我们不理解的 HTTP 状态码就会被引发。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.LineTooLong">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">LineTooLong</code><a class="headerlink" href="#http.client.LineTooLong" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。 如果在 HTTP 协议中从服务器接收到过长的行就会被引发。</p>
</dd></dl>
<dl class="exception">
<dt id="http.client.RemoteDisconnected">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">RemoteDisconnected</code><a class="headerlink" href="#http.client.RemoteDisconnected" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="exceptions.html#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a><a class="reference internal" href="#http.client.BadStatusLine" title="http.client.BadStatusLine"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadStatusLine</span></code></a> 的一个子类。 当尝试读取响应时的结果是未从连接读取到数据时由 <a class="reference internal" href="#http.client.HTTPConnection.getresponse" title="http.client.HTTPConnection.getresponse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">HTTPConnection.getresponse()</span></code></a> 引发,表明远端已关闭连接。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能: </span>在此之前引发的异常为 <a class="reference internal" href="#http.client.BadStatusLine" title="http.client.BadStatusLine"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadStatusLine</span></code></a><code class="docutils literal notranslate"><span class="pre">('')</span></code></p>
</div>
</dd></dl>
<p>此模块中定义的常量为:</p>
<dl class="data">
<dt id="http.client.HTTP_PORT">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTP_PORT</code><a class="headerlink" href="#http.client.HTTP_PORT" title="永久链接至目标"></a></dt>
<dd><p>HTTP 协议默认的端口号 (总是 <code class="docutils literal notranslate"><span class="pre">80</span></code>)。</p>
</dd></dl>
<dl class="data">
<dt id="http.client.HTTPS_PORT">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPS_PORT</code><a class="headerlink" href="#http.client.HTTPS_PORT" title="永久链接至目标"></a></dt>
<dd><p>HTTPS 协议默认的端口号 (总是 <code class="docutils literal notranslate"><span class="pre">443</span></code>)。</p>
</dd></dl>
<dl class="data">
<dt id="http.client.responses">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">responses</code><a class="headerlink" href="#http.client.responses" title="永久链接至目标"></a></dt>
<dd><p>这个字典把 HTTP 1.1 状态码映射到 W3C 名称。</p>
<p>例如:<code class="docutils literal notranslate"><span class="pre">http.client.responses[http.client.NOT_FOUND]</span></code><code class="docutils literal notranslate"><span class="pre">'NOT</span> <span class="pre">FOUND</span></code> (未发现)。</p>
</dd></dl>
<p>本模块中可用的 HTTP 状态码常量可以参见 <a class="reference internal" href="http.html#http-status-codes"><span class="std std-ref">HTTP 状态码</span></a></p>
<section id="httpconnection-objects">
<span id="id1"></span><h2>HTTPConnection 对象<a class="headerlink" href="#httpconnection-objects" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 实例拥有以下方法:</p>
<dl class="method">
<dt id="http.client.HTTPConnection.request">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">request</code><span class="sig-paren">(</span><em class="sig-param">method</em>, <em class="sig-param">url</em>, <em class="sig-param">body=None</em>, <em class="sig-param">headers={}</em>, <em class="sig-param">*</em>, <em class="sig-param">encode_chunked=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.request" title="永久链接至目标"></a></dt>
<dd><p>这会使用 HTTP 请求方法 <em>method</em> 和选择器 <em>url</em> 向服务器发送请求。</p>
<p>如果给定 <em>body</em>,那么给定的数据会在信息头完成之后发送。它可能是一个 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 、一个 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 、一个打开的 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>,或者 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 迭代器。如果 <em>body</em> 是字符串,它会按 HTTP 默认的 ISO-8859-1 编码;如果是一个字节类对象,它会按原样发送;如果是 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> ,文件的内容会被发送,这个文件对象应该支持 <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法。如果这个文件对象是一个 <a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> 实例, <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法返回的数据会按 ISO-8859-1 编码,否则 <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法返回的数据会按原样发送;如果 <em>body</em> 是一个迭代器,迭代器中的元素会被发送,直到迭代器耗尽。</p>
<p><em>headers</em> 参数应是额外的随请求发送的 HTTP 信息头的字典。</p>
<p>如果 <em>headers</em> 既不包含 Content-Length 也没有 Transfer-Encoding但存在请求正文那么这些头字段中的一个会自动设定。如果 <em>body</em><code class="docutils literal notranslate"><span class="pre">None</span></code>,那么对于要求正文的方法 (<code class="docutils literal notranslate"><span class="pre">PUT</span></code><code class="docutils literal notranslate"><span class="pre">POST</span></code>,和 <code class="docutils literal notranslate"><span class="pre">PATCH</span></code>)Content-Length 头会被设为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。如果 <em>body</em> 是字符串或者类似字节的对象,并且也不是 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件</span></a>Content-Length 头会设为正文的长度。任何其他类型的 <em>body</em> (一般是文件或迭代器)会按块编码,这时会自动设定 Transfer-Encoding 头以代替 Content-Length。</p>
<p><em>headers</em> 中指定 Transfer-Encoding 时, <em>encode_chunked</em> 是唯一相关的参数。如果 <em>encode_chunked</em><code class="docutils literal notranslate"><span class="pre">False</span></code>HTTPConnection 对象会假定所有的编码都由调用代码处理。如果为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,正文会按块编码。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>HTTP 协议在 1.1 版中添加了块传输编码。除非明确知道 HTTP 服务器可以处理 HTTP 1.1,调用者要么必须指定 Content-Length要么必须传入 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 或字节类对象,注意该对象不能是表达 body 的文件。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><em>body</em> 现在可以是可迭代对象了。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>如果 Content-Length 和 Transfer-Encoding 都没有在 <em>headers</em> 中设置,文件和可迭代的 <em>body</em> 对象现在会按块编码。添加了 <em>encode_chunked</em> 参数。不会尝试去确定文件对象的 Content-Length。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.getresponse">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">getresponse</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.getresponse" title="永久链接至目标"></a></dt>
<dd><p>应当在发送一个请求从服务器获取响应时被调用。 返回一个 <a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> 的实例。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>请注意你必须在读取了整个响应之后才能向服务器发送新的请求。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>如果引发了 <a class="reference internal" href="exceptions.html#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a> 或其子类, <a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 对象将在发送新的请求时准备好重新连接。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.set_debuglevel">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">set_debuglevel</code><span class="sig-paren">(</span><em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.set_debuglevel" title="永久链接至目标"></a></dt>
<dd><p>设置调试等级。 默认的调试等级为 <code class="docutils literal notranslate"><span class="pre">0</span></code>,意味着不会打印调试输出。 任何大于 <code class="docutils literal notranslate"><span class="pre">0</span></code> 的值将使得所有当前定义的调试输出被打印到 stdout。 <code class="docutils literal notranslate"><span class="pre">debuglevel</span></code> 会被传给任何新创建的 <a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> 对象。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.1 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.set_tunnel">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">set_tunnel</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <em class="sig-param">headers=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.set_tunnel" title="永久链接至目标"></a></dt>
<dd><p>为 HTTP 连接隧道设置主机和端口。 这将允许通过代理服务器运行连接。</p>
<p>host 和 port 参数指明隧道连接的位置(即 CONNECT 请求所包含的地址,而 <em>不是</em> 代理服务器的地址)。</p>
<p>headers 参数应为一个随 CONNECT 请求发送的额外 HTTP 标头的映射。</p>
<p>例如,要通过一个运行于本机 8080 端口的 HTTPS 代理服务器隧道,我们应当向 <a class="reference internal" href="#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a> 构造器传入代理的地址,并将我们最终想要访问的主机地址传给 <a class="reference internal" href="#http.client.HTTPConnection.set_tunnel" title="http.client.HTTPConnection.set_tunnel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_tunnel()</span></code></a> 方法:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;localhost&quot;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">set_tunnel</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;HEAD&quot;</span><span class="p">,</span><span class="s2">&quot;/index.html&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.connect">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">connect</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.connect" title="永久链接至目标"></a></dt>
<dd><p>当对象被创建后连接到指定的服务器。 默认情况下,如果客户端还未建立连接,此函数会在发送请求时自动被调用。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.close">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.close" title="永久链接至目标"></a></dt>
<dd><p>关闭到服务器的连接。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPConnection.blocksize">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">blocksize</code><a class="headerlink" href="#http.client.HTTPConnection.blocksize" title="永久链接至目标"></a></dt>
<dd><p>用于发送文件类消息体的缓冲区大小。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>
<p>作为对使用上述 <code class="xref py py-meth docutils literal notranslate"><span class="pre">request()</span></code> 方法的替代同,你也可以通过使用下面的四个函数,分步骤发送请的请求。</p>
<dl class="method">
<dt id="http.client.HTTPConnection.putrequest">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">putrequest</code><span class="sig-paren">(</span><em class="sig-param">method</em>, <em class="sig-param">url</em>, <em class="sig-param">skip_host=False</em>, <em class="sig-param">skip_accept_encoding=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.putrequest" title="永久链接至目标"></a></dt>
<dd><p>应为连接服务器之后首先调用的函数。将向服务器发送一行数据,包含 <em>method</em> 字符串、<em>url</em> 字符串和 HTTP 版本(<code class="docutils literal notranslate"><span class="pre">HTTP/1.1</span></code>)。若要禁止自动发送 <code class="docutils literal notranslate"><span class="pre">Host:</span></code><code class="docutils literal notranslate"><span class="pre">Accept-Encoding:</span></code> 头部信息(比如需要接受其他编码格式的内容),请将 <em>skip_host</em><em>skip_accept_encoding</em> 设为非 False 值。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.putheader">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">putheader</code><span class="sig-paren">(</span><em class="sig-param">header</em>, <em class="sig-param">argument</em><span class="optional">[</span>, <em class="sig-param">...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.putheader" title="永久链接至目标"></a></dt>
<dd><p>向服务器发送一个 <span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> 格式的头部。将向服务器发送一行由头、冒号和空格以及第一个参数组成的数据。 如果还给出了其他参数,将在后续行中发送,每行由一个制表符和一个参数组成。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.endheaders">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">endheaders</code><span class="sig-paren">(</span><em class="sig-param">message_body=None</em>, <em class="sig-param">*</em>, <em class="sig-param">encode_chunked=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.endheaders" title="永久链接至目标"></a></dt>
<dd><p>向服务器发送一个空行,表示头部文件结束。可选的 <em>message_body</em> 参数可用于传入一个与请求相关的消息体。</p>
<p>如果 <em>encode_chunked</em><code class="docutils literal notranslate"><span class="pre">True</span></code>,则对 <em>message_body</em> 的每次迭代结果将依照 <span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7230.html"><strong>RFC 7230</strong></a> 3.3.1 节的规范进行分块编码。数据如何编码取决于 <em>message_body</em> 的类型。 如果 <em>message_body</em> 实现了 <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer 接口</span></a>,编码将生成一个数据块。如果 <em>message_body</em><a class="reference internal" href="collections.abc.html#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Iterable</span></code></a>,则 <em>message_body</em> 的每次迭代都会产生一个块。 如果 <em>message_body</em><a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>,那么每次调用 <code class="docutils literal notranslate"><span class="pre">.read()</span></code> 都会产生一个数据块。在 <em>message_body</em> 结束后,本方法立即会自动标记块编码数据的结束。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>由于分块编码的规范要求,迭代器本身产生的空块将被分块编码器忽略。这是为了避免目标服务器因错误编码而过早终止对请求的读取。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>支持分块编码。加入了 <em>encode_chunked</em> 参数。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPConnection.send">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">send</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.send" title="永久链接至目标"></a></dt>
<dd><p>发送数据到服务器。本函数只应在调用 <a class="reference internal" href="#http.client.HTTPConnection.endheaders" title="http.client.HTTPConnection.endheaders"><code class="xref py py-meth docutils literal notranslate"><span class="pre">endheaders()</span></code></a> 方法之后且调用 <a class="reference internal" href="#http.client.HTTPConnection.getresponse" title="http.client.HTTPConnection.getresponse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getresponse()</span></code></a> 之前直接调用。</p>
</dd></dl>
</section>
<section id="httpresponse-objects">
<span id="id2"></span><h2>HTTPResponse 对象<a class="headerlink" href="#httpresponse-objects" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> 实例封装了来自服务器的 HTTP 响应。通过它可以访问请求头和响应体。响应是可迭代对象,可在 with 语句中使用。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>现在已实现了 <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>
</div>
<dl class="method">
<dt id="http.client.HTTPResponse.read">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">read</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">amt</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.read" title="永久链接至目标"></a></dt>
<dd><p>读取并返回响应体,或后续 <em>amt</em> 个字节。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPResponse.readinto">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">readinto</code><span class="sig-paren">(</span><em class="sig-param">b</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.readinto" title="永久链接至目标"></a></dt>
<dd><p>读取响应体的后续 len(b) 个字节到缓冲区 <em>b</em>。返回读取的字节数。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPResponse.getheader">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">getheader</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.getheader" title="永久链接至目标"></a></dt>
<dd><p>返回头部信息中的 <em>name</em> 值,如果没有与 <em>name</em> 匹配的字段,则返回*default*。如果名为 <em>name</em> 的字段不止一个,则返回所有字段,中间用 ', ' 连接。 如果 'default' 不是单个字符串,而是其他可迭代对象,则其元素同样以逗号连接并返回。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPResponse.getheaders">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">getheaders</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.getheaders" title="永久链接至目标"></a></dt>
<dd><p>返回 (header, value) 元组构成的列表。</p>
</dd></dl>
<dl class="method">
<dt id="http.client.HTTPResponse.fileno">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">fileno</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.fileno" title="永久链接至目标"></a></dt>
<dd><p>返回底层套接字的 <code class="docutils literal notranslate"><span class="pre">fileno</span></code></p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.msg">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">msg</code><a class="headerlink" href="#http.client.HTTPResponse.msg" title="永久链接至目标"></a></dt>
<dd><p>包含响应头的 <code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> 实例。<code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code><a class="reference internal" href="email.message.html#module-email.message" title="email.message: The base class representing email messages."><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message</span></code></a> 的子类。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.version">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">version</code><a class="headerlink" href="#http.client.HTTPResponse.version" title="永久链接至目标"></a></dt>
<dd><p>服务器使用的 HTTP 协议版本。10 代表 HTTP/1.011 代表 HTTP/1.1。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.status">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">status</code><a class="headerlink" href="#http.client.HTTPResponse.status" title="永久链接至目标"></a></dt>
<dd><p>由服务器返回的状态码。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.reason">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">reason</code><a class="headerlink" href="#http.client.HTTPResponse.reason" title="永久链接至目标"></a></dt>
<dd><p>服务器返回的原因短语。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.debuglevel">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">debuglevel</code><a class="headerlink" href="#http.client.HTTPResponse.debuglevel" title="永久链接至目标"></a></dt>
<dd><p>一个调试钩子。如果 <a class="reference internal" href="#http.client.HTTPResponse.debuglevel" title="http.client.HTTPResponse.debuglevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">debuglevel</span></code></a> 大于零,状态信息将在读取和解析响应数据时打印输出到 stdout。</p>
</dd></dl>
<dl class="attribute">
<dt id="http.client.HTTPResponse.closed">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">closed</code><a class="headerlink" href="#http.client.HTTPResponse.closed" title="永久链接至目标"></a></dt>
<dd><p>如果流被关闭,则为``True``。</p>
</dd></dl>
</section>
<section id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题"></a></h2>
<p>下面是使用 <code class="docutils literal notranslate"><span class="pre">GET</span></code> 方法的会话示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r1</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r1</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">r1</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data1</span> <span class="o">=</span> <span class="n">r1</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="c1"># This will return entire content.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># The following example demonstrates reading data in chunks.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r1</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">chunk</span> <span class="o">:=</span> <span class="n">r1</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">200</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">chunk</span><span class="p">))</span>
<span class="go">b&#39;&lt;!doctype html&gt;\n&lt;!--[if&quot;...</span>
<span class="go">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example of an invalid request</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;docs.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/parrot.spam&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r2</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r2</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">r2</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">404 Not Found</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data2</span> <span class="o">=</span> <span class="n">r2</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>以下是使用 <code class="docutils literal notranslate"><span class="pre">HEAD</span></code> 方法的会话示例。 请注意,<code class="docutils literal notranslate"><span class="pre">HEAD</span></code> 方法从不返回任何数据。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;HEAD&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">res</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
<span class="go">True</span>
</pre></div>
</div>
<p>下面是用 <code class="docutils literal notranslate"><span class="pre">POST</span></code> 发送请求的会话示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span><span class="o">,</span> <span class="nn">urllib.parse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">params</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s1">&#39;@number&#39;</span><span class="p">:</span> <span class="mi">12524</span><span class="p">,</span> <span class="s1">&#39;@type&#39;</span><span class="p">:</span> <span class="s1">&#39;issue&#39;</span><span class="p">,</span> <span class="s1">&#39;@action&#39;</span><span class="p">:</span> <span class="s1">&#39;show&#39;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;Content-type&quot;</span><span class="p">:</span> <span class="s2">&quot;application/x-www-form-urlencoded&quot;</span><span class="p">,</span>
<span class="gp">... </span> <span class="s2">&quot;Accept&quot;</span><span class="p">:</span> <span class="s2">&quot;text/plain&quot;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s2">&quot;bugs.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;POST&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">302 Found</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">b&#39;Redirecting to &lt;a href=&quot;http://bugs.python.org/issue12524&quot;&gt;http://bugs.python.org/issue12524&lt;/a&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>在客户端, <code class="docutils literal notranslate"><span class="pre">HTTP</span> <span class="pre">PUT</span></code> 请求与 <code class="docutils literal notranslate"><span class="pre">POST</span></code> 请求非常相似。区别只在于服务器端HTTP 服务器将允许通过 <code class="docutils literal notranslate"><span class="pre">PUT</span></code> 请求创建资源。应该注意的是,自定义的 HTTP 方法也可以在 <a class="reference internal" href="urllib.request.html#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.request.Request</span></code></a> 中通过设置适当的方法属性来进行处理。下面是一个会话示例,演示了如何利用 http.client 发送 <code class="docutils literal notranslate"><span class="pre">PUT</span></code> 请求。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># This creates an HTTP message</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># with the content of BODY as the enclosed representation</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># for the resource http://localhost:8080/file</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">BODY</span> <span class="o">=</span> <span class="s2">&quot;***filecontents***&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s2">&quot;localhost&quot;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;PUT&quot;</span><span class="p">,</span> <span class="s2">&quot;/file&quot;</span><span class="p">,</span> <span class="n">BODY</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200, OK</span>
</pre></div>
</div>
</section>
<section id="httpmessage-objects">
<span id="id3"></span><h2>HTTPMessage 对象<a class="headerlink" href="#httpmessage-objects" title="永久链接至标题"></a></h2>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> 的实例存有 HTTP 响应的头部信息。利用 <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.Message</span></code></a> 类实现。</p>
</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">http.client</span></code> --- HTTP 协议客户端</a><ul>
<li><a class="reference internal" href="#httpconnection-objects">HTTPConnection 对象</a></li>
<li><a class="reference internal" href="#httpresponse-objects">HTTPResponse 对象</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
<li><a class="reference internal" href="#httpmessage-objects">HTTPMessage 对象</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="http.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http</span></code> --- HTTP 模块</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="ftplib.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code> --- FTP 协议客户端</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/http.client.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="ftplib.html" title="ftplib --- FTP 协议客户端"
>下一页</a> |</li>
<li class="right" >
<a href="http.html" title="http --- HTTP 模块"
>上一页</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>