mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
1 line
6.7 KiB
HTML
1 line
6.7 KiB
HTML
<div class="body" role="main"><div class="section" id="module-urllib.error"><h1><span class="yiyi-st" id="yiyi-10">21.9。 <a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal"><span class="pre">urllib.error</span></code></a> - 由urllib.request引发的异常类</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/urllib/error.py">Lib / urllib / error.py</a></span></p><p><span class="yiyi-st" id="yiyi-12"><a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal"><span class="pre">urllib.error</span></code></a>模块定义<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"><span class="pre">urllib.request</span></code></a>引发的异常的异常类。</span><span class="yiyi-st" id="yiyi-13">基本异常类是<a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal"><span class="pre">URLError</span></code></a>。</span></p><p><span class="yiyi-st" id="yiyi-14">以下异常由<a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal"><span class="pre">urllib.error</span></code></a>引发:</span></p><dl class="exception"><dt id="urllib.error.URLError"><span class="yiyi-st" id="yiyi-15"> <em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">URLError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-16">处理程序在遇到问题时引导此异常(或派生异常)。</span><span class="yiyi-st" id="yiyi-17">它是<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>的子类。</span></p><dl class="attribute"><dt id="urllib.error.URLError.reason"><span class="yiyi-st" id="yiyi-18"> <code class="descname">reason</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-19">这个错误的原因。</span><span class="yiyi-st" id="yiyi-20">它可以是一个消息字符串或另一个异常实例。</span></p></dd></dl><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-21"><span class="versionmodified">在版本3.3中已更改:</span> <a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal"><span class="pre">URLError</span></code></a>已成为<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>的子类,而不是<a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>。</span></p></div></dd></dl><dl class="exception"><dt id="urllib.error.HTTPError"><span class="yiyi-st" id="yiyi-22"> <em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">HTTPError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-23">虽然作为一个例外(<a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal"><span class="pre">URLError</span></code></a>的子类),一个<a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></code></a>也可以作为一个非特殊的类文件返回值(与<a class="reference internal" href="urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal"><span class="pre">urlopen()</span></code></a>返回)。</span><span class="yiyi-st" id="yiyi-24">这在处理异常HTTP错误(例如认证请求)时非常有用。</span></p><dl class="attribute"><dt id="urllib.error.HTTPError.code"><span class="yiyi-st" id="yiyi-25"> <code class="descname">code</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-26"><a class="reference external" href="http://www.faqs.org/rfcs/rfc2616.html">RFC 2616</a>中定义的HTTP状态代码。</span><span class="yiyi-st" id="yiyi-27">此数值对应于在<a class="reference internal" href="http.server.html#http.server.BaseHTTPRequestHandler.responses" title="http.server.BaseHTTPRequestHandler.responses"><code class="xref py py-attr docutils literal"><span class="pre">http.server.BaseHTTPRequestHandler.responses</span></code></a>中找到的代码字典中找到的值。</span></p></dd></dl><dl class="attribute"><dt id="urllib.error.HTTPError.reason"><span class="yiyi-st" id="yiyi-28"> <code class="descname">reason</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-29">这通常是解释此错误的原因的字符串。</span></p></dd></dl><dl class="attribute"><dt id="urllib.error.HTTPError.headers"><span class="yiyi-st" id="yiyi-30"> <code class="descname">headers</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-31">导致<a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></code></a>的HTTP请求的HTTP响应标头。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-32"><span class="versionmodified">版本3.4中的新功能。</span></span></p></div></dd></dl></dd></dl><dl class="exception"><dt id="urllib.error.ContentTooShortError"><span class="yiyi-st" id="yiyi-33"> <em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">ContentTooShortError</code><span class="sig-paren">(</span><em>msg</em>, <em>content</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-34">当<a class="reference internal" href="urllib.request.html#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal"><span class="pre">urlretrieve()</span></code></a>函数检测到下载的数据量小于预期量(由<em>Content-Length</em>头指定)时,引发此异常。</span><span class="yiyi-st" id="yiyi-35"><code class="xref py py-attr docutils literal"><span class="pre">content</span></code>属性存储下载(以及据称截断的)数据。</span></p></dd></dl></div></div> |