gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1971 lines
215KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="urllib.request — Extensible library for opening URLs" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/urllib.request.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/urllib/request.py The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirection..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/urllib/request.py The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirection..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>urllib.request — Extensible library for opening URLs &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="urllib.parse — Parse URLs into components" href="urllib.parse.html" />
  33. <link rel="prev" title="urllib — URL handling modules" href="urllib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/urllib.request.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> — Extensible library for opening URLs</a><ul>
  86. <li><a class="reference internal" href="#request-objects">Request Objects</a></li>
  87. <li><a class="reference internal" href="#openerdirector-objects">OpenerDirector Objects</a></li>
  88. <li><a class="reference internal" href="#basehandler-objects">BaseHandler Objects</a></li>
  89. <li><a class="reference internal" href="#httpredirecthandler-objects">HTTPRedirectHandler Objects</a></li>
  90. <li><a class="reference internal" href="#httpcookieprocessor-objects">HTTPCookieProcessor Objects</a></li>
  91. <li><a class="reference internal" href="#proxyhandler-objects">ProxyHandler Objects</a></li>
  92. <li><a class="reference internal" href="#httppasswordmgr-objects">HTTPPasswordMgr Objects</a></li>
  93. <li><a class="reference internal" href="#httppasswordmgrwithpriorauth-objects">HTTPPasswordMgrWithPriorAuth Objects</a></li>
  94. <li><a class="reference internal" href="#abstractbasicauthhandler-objects">AbstractBasicAuthHandler Objects</a></li>
  95. <li><a class="reference internal" href="#httpbasicauthhandler-objects">HTTPBasicAuthHandler Objects</a></li>
  96. <li><a class="reference internal" href="#proxybasicauthhandler-objects">ProxyBasicAuthHandler Objects</a></li>
  97. <li><a class="reference internal" href="#abstractdigestauthhandler-objects">AbstractDigestAuthHandler Objects</a></li>
  98. <li><a class="reference internal" href="#httpdigestauthhandler-objects">HTTPDigestAuthHandler Objects</a></li>
  99. <li><a class="reference internal" href="#proxydigestauthhandler-objects">ProxyDigestAuthHandler Objects</a></li>
  100. <li><a class="reference internal" href="#httphandler-objects">HTTPHandler Objects</a></li>
  101. <li><a class="reference internal" href="#httpshandler-objects">HTTPSHandler Objects</a></li>
  102. <li><a class="reference internal" href="#filehandler-objects">FileHandler Objects</a></li>
  103. <li><a class="reference internal" href="#datahandler-objects">DataHandler Objects</a></li>
  104. <li><a class="reference internal" href="#ftphandler-objects">FTPHandler Objects</a></li>
  105. <li><a class="reference internal" href="#cacheftphandler-objects">CacheFTPHandler Objects</a></li>
  106. <li><a class="reference internal" href="#unknownhandler-objects">UnknownHandler Objects</a></li>
  107. <li><a class="reference internal" href="#httperrorprocessor-objects">HTTPErrorProcessor Objects</a></li>
  108. <li><a class="reference internal" href="#examples">Examples</a></li>
  109. <li><a class="reference internal" href="#legacy-interface">Legacy interface</a></li>
  110. <li><a class="reference internal" href="#urllib-request-restrictions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> Restrictions</a></li>
  111. </ul>
  112. </li>
  113. <li><a class="reference internal" href="#module-urllib.response"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.response</span></code> — Response classes used by urllib</a></li>
  114. </ul>
  115. </div>
  116. <div>
  117. <h4>Previous topic</h4>
  118. <p class="topless"><a href="urllib.html"
  119. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code> — URL handling modules</a></p>
  120. </div>
  121. <div>
  122. <h4>Next topic</h4>
  123. <p class="topless"><a href="urllib.parse.html"
  124. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code> — Parse URLs into components</a></p>
  125. </div>
  126. <div role="note" aria-label="source link">
  127. <h3>This Page</h3>
  128. <ul class="this-page-menu">
  129. <li><a href="../bugs.html">Report a Bug</a></li>
  130. <li>
  131. <a href="https://github.com/python/cpython/blob/main/Doc/library/urllib.request.rst"
  132. rel="nofollow">Show Source
  133. </a>
  134. </li>
  135. </ul>
  136. </div>
  137. </nav>
  138. </div>
  139. </div>
  140. <div class="related" role="navigation" aria-label="related navigation">
  141. <h3>Navigation</h3>
  142. <ul>
  143. <li class="right" style="margin-right: 10px">
  144. <a href="../genindex.html" title="General Index"
  145. accesskey="I">index</a></li>
  146. <li class="right" >
  147. <a href="../py-modindex.html" title="Python Module Index"
  148. >modules</a> |</li>
  149. <li class="right" >
  150. <a href="urllib.parse.html" title="urllib.parse — Parse URLs into components"
  151. accesskey="N">next</a> |</li>
  152. <li class="right" >
  153. <a href="urllib.html" title="urllib — URL handling modules"
  154. accesskey="P">previous</a> |</li>
  155. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  156. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  157. <li class="switchers">
  158. <div class="language_switcher_placeholder"></div>
  159. <div class="version_switcher_placeholder"></div>
  160. </li>
  161. <li>
  162. </li>
  163. <li id="cpython-language-and-version">
  164. <a href="../index.html">3.12.3 Documentation</a> &#187;
  165. </li>
  166. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  167. <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> &#187;</li>
  168. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> — Extensible library for opening URLs</a></li>
  169. <li class="right">
  170. <div class="inline-search" role="search">
  171. <form class="inline-search" action="../search.html" method="get">
  172. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  173. <input type="submit" value="Go" />
  174. </form>
  175. </div>
  176. |
  177. </li>
  178. <li class="right">
  179. <label class="theme-selector-label">
  180. Theme
  181. <select class="theme-selector" oninput="activateTheme(this.value)">
  182. <option value="auto" selected>Auto</option>
  183. <option value="light">Light</option>
  184. <option value="dark">Dark</option>
  185. </select>
  186. </label> |</li>
  187. </ul>
  188. </div>
  189. <div class="document">
  190. <div class="documentwrapper">
  191. <div class="bodywrapper">
  192. <div class="body" role="main">
  193. <section id="module-urllib.request">
  194. <span id="urllib-request-extensible-library-for-opening-urls"></span><h1><a class="reference internal" href="#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> — Extensible library for opening URLs<a class="headerlink" href="#module-urllib.request" title="Link to this heading">¶</a></h1>
  195. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/urllib/request.py">Lib/urllib/request.py</a></p>
  196. <hr class="docutils" />
  197. <p>The <a class="reference internal" href="#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> module defines functions and classes which help in
  198. opening URLs (mostly HTTP) in a complex world — basic and digest
  199. authentication, redirections, cookies and more.</p>
  200. <div class="admonition seealso">
  201. <p class="admonition-title">See also</p>
  202. <p>The <a class="reference external" href="https://requests.readthedocs.io/en/master/">Requests package</a>
  203. is recommended for a higher-level HTTP client interface.</p>
  204. </div>
  205. <div class="admonition warning">
  206. <p class="admonition-title">Warning</p>
  207. <p>On macOS it is unsafe to use this module in programs using
  208. <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> because the <a class="reference internal" href="#urllib.request.getproxies" title="urllib.request.getproxies"><code class="xref py py-func docutils literal notranslate"><span class="pre">getproxies()</span></code></a> implementation for
  209. macOS uses a higher-level system API. Set the environment variable
  210. <code class="docutils literal notranslate"><span class="pre">no_proxy</span></code> to <code class="docutils literal notranslate"><span class="pre">*</span></code> to avoid this problem
  211. (e.g. <code class="docutils literal notranslate"><span class="pre">os.environ[&quot;no_proxy&quot;]</span> <span class="pre">=</span> <span class="pre">&quot;*&quot;</span></code>).</p>
  212. </div>
  213. <div class="availability docutils container">
  214. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  215. <p>This module does not work or is not available on WebAssembly platforms
  216. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  217. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  218. </div>
  219. <p>The <a class="reference internal" href="#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> module defines the following functions:</p>
  220. <dl class="py function">
  221. <dt class="sig sig-object py" id="urllib.request.urlopen">
  222. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">urlopen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data=None</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cafile=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capath=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cadefault=False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context=None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.urlopen" title="Link to this definition">¶</a></dt>
  223. <dd><p>Open <em>url</em>, which can be either a string containing a valid, properly
  224. encoded URL, or a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object.</p>
  225. <p><em>data</em> must be an object specifying additional data to be sent to the
  226. server, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no such data is needed. See <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a>
  227. for details.</p>
  228. <p>urllib.request module uses HTTP/1.1 and includes <code class="docutils literal notranslate"><span class="pre">Connection:close</span></code> header
  229. in its HTTP requests.</p>
  230. <p>The optional <em>timeout</em> parameter specifies a timeout in seconds for
  231. blocking operations like the connection attempt (if not specified,
  232. the global default timeout setting will be used). This actually
  233. only works for HTTP, HTTPS and FTP connections.</p>
  234. <p>If <em>context</em> is specified, it must be a <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> instance
  235. describing the various SSL options. See <a class="reference internal" href="http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a>
  236. for more details.</p>
  237. <p>The optional <em>cafile</em> and <em>capath</em> parameters specify a set of trusted
  238. CA certificates for HTTPS requests. <em>cafile</em> should point to a single
  239. file containing a bundle of CA certificates, whereas <em>capath</em> should
  240. point to a directory of hashed certificate files. More information can
  241. be found in <a class="reference internal" href="ssl.html#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_verify_locations()</span></code></a>.</p>
  242. <p>The <em>cadefault</em> parameter is ignored.</p>
  243. <p>This function always returns an object which can work as a
  244. <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> and has the properties <em>url</em>, <em>headers</em>, and <em>status</em>.
  245. See <a class="reference internal" href="#urllib.response.addinfourl" title="urllib.response.addinfourl"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.response.addinfourl</span></code></a> for more detail on these properties.</p>
  246. <p>For HTTP and HTTPS URLs, this function returns a
  247. <a class="reference internal" href="http.client.html#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPResponse</span></code></a> object slightly modified. In addition
  248. to the three new methods above, the msg attribute contains the
  249. same information as the <a class="reference internal" href="http.client.html#http.client.HTTPResponse.reason" title="http.client.HTTPResponse.reason"><code class="xref py py-attr docutils literal notranslate"><span class="pre">reason</span></code></a>
  250. attribute — the reason phrase returned by server — instead of
  251. the response headers as it is specified in the documentation for
  252. <a class="reference internal" href="http.client.html#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a>.</p>
  253. <p>For FTP, file, and data URLs and requests explicitly handled by legacy
  254. <a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a> and <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a> classes, this function
  255. returns a <a class="reference internal" href="#urllib.response.addinfourl" title="urllib.response.addinfourl"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.response.addinfourl</span></code></a> object.</p>
  256. <p>Raises <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> on protocol errors.</p>
  257. <p>Note that <code class="docutils literal notranslate"><span class="pre">None</span></code> may be returned if no handler handles the request (though
  258. the default installed global <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> uses
  259. <a class="reference internal" href="#urllib.request.UnknownHandler" title="urllib.request.UnknownHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnknownHandler</span></code></a> to ensure this never happens).</p>
  260. <p>In addition, if proxy settings are detected (for example, when a <code class="docutils literal notranslate"><span class="pre">*_proxy</span></code>
  261. environment variable like <code class="xref std std-envvar docutils literal notranslate"><span class="pre">http_proxy</span></code> is set),
  262. <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> is default installed and makes sure the requests are
  263. handled through the proxy.</p>
  264. <p>The legacy <code class="docutils literal notranslate"><span class="pre">urllib.urlopen</span></code> function from Python 2.6 and earlier has been
  265. discontinued; <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.request.urlopen()</span></code></a> corresponds to the old
  266. <code class="docutils literal notranslate"><span class="pre">urllib2.urlopen</span></code>. Proxy handling, which was done by passing a dictionary
  267. parameter to <code class="docutils literal notranslate"><span class="pre">urllib.urlopen</span></code>, can be obtained by using
  268. <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> objects.</p>
  269. <p class="audit-hook"><p>The default opener raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a>
  270. <code class="docutils literal notranslate"><span class="pre">urllib.Request</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fullurl</span></code>, <code class="docutils literal notranslate"><span class="pre">data</span></code>, <code class="docutils literal notranslate"><span class="pre">headers</span></code>,
  271. <code class="docutils literal notranslate"><span class="pre">method</span></code> taken from the request object.</p>
  272. </p>
  273. <div class="versionchanged">
  274. <p><span class="versionmodified changed">Changed in version 3.2: </span><em>cafile</em> and <em>capath</em> were added.</p>
  275. <p>HTTPS virtual hosts are now supported if possible (that is, if
  276. <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a> is true).</p>
  277. <p><em>data</em> can be an iterable object.</p>
  278. </div>
  279. <div class="versionchanged">
  280. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>cadefault</em> was added.</p>
  281. </div>
  282. <div class="versionchanged">
  283. <p><span class="versionmodified changed">Changed in version 3.4.3: </span><em>context</em> was added.</p>
  284. </div>
  285. <div class="versionchanged">
  286. <p><span class="versionmodified changed">Changed in version 3.10: </span>HTTPS connection now send an ALPN extension with protocol indicator
  287. <code class="docutils literal notranslate"><span class="pre">http/1.1</span></code> when no <em>context</em> is given. Custom <em>context</em> should set
  288. ALPN protocols with <a class="reference internal" href="ssl.html#ssl.SSLContext.set_alpn_protocols" title="ssl.SSLContext.set_alpn_protocols"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_alpn_protocols()</span></code></a>.</p>
  289. </div>
  290. <div class="deprecated">
  291. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span><em>cafile</em>, <em>capath</em> and <em>cadefault</em> are deprecated in favor of <em>context</em>.
  292. Please use <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> instead, or let
  293. <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> select the system’s trusted CA
  294. certificates for you.</p>
  295. </div>
  296. </dd></dl>
  297. <dl class="py function">
  298. <dt class="sig sig-object py" id="urllib.request.install_opener">
  299. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">install_opener</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">opener</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.install_opener" title="Link to this definition">¶</a></dt>
  300. <dd><p>Install an <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> instance as the default global opener.
  301. Installing an opener is only necessary if you want urlopen to use that
  302. opener; otherwise, simply call <a class="reference internal" href="#urllib.request.OpenerDirector.open" title="urllib.request.OpenerDirector.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OpenerDirector.open()</span></code></a> instead of
  303. <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>. The code does not check for a real
  304. <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>, and any class with the appropriate interface will
  305. work.</p>
  306. </dd></dl>
  307. <dl class="py function">
  308. <dt class="sig sig-object py" id="urllib.request.build_opener">
  309. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">build_opener</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">handler</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.build_opener" title="Link to this definition">¶</a></dt>
  310. <dd><p>Return an <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> instance, which chains the handlers in the
  311. order given. <em>handler</em>s can be either instances of <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, or
  312. subclasses of <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> (in which case it must be possible to call
  313. the constructor without any parameters). Instances of the following classes
  314. will be in front of the <em>handler</em>s, unless the <em>handler</em>s contain them,
  315. instances of them or subclasses of them: <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> (if proxy
  316. settings are detected), <a class="reference internal" href="#urllib.request.UnknownHandler" title="urllib.request.UnknownHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnknownHandler</span></code></a>, <a class="reference internal" href="#urllib.request.HTTPHandler" title="urllib.request.HTTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPHandler</span></code></a>,
  317. <a class="reference internal" href="#urllib.request.HTTPDefaultErrorHandler" title="urllib.request.HTTPDefaultErrorHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPDefaultErrorHandler</span></code></a>, <a class="reference internal" href="#urllib.request.HTTPRedirectHandler" title="urllib.request.HTTPRedirectHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPRedirectHandler</span></code></a>,
  318. <a class="reference internal" href="#urllib.request.FTPHandler" title="urllib.request.FTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTPHandler</span></code></a>, <a class="reference internal" href="#urllib.request.FileHandler" title="urllib.request.FileHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileHandler</span></code></a>, <a class="reference internal" href="#urllib.request.HTTPErrorProcessor" title="urllib.request.HTTPErrorProcessor"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPErrorProcessor</span></code></a>.</p>
  319. <p>If the Python installation has SSL support (i.e., if the <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> module
  320. can be imported), <a class="reference internal" href="#urllib.request.HTTPSHandler" title="urllib.request.HTTPSHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSHandler</span></code></a> will also be added.</p>
  321. <p>A <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> subclass may also change its <code class="xref py py-attr docutils literal notranslate"><span class="pre">handler_order</span></code>
  322. attribute to modify its position in the handlers list.</p>
  323. </dd></dl>
  324. <dl class="py function">
  325. <dt class="sig sig-object py" id="urllib.request.pathname2url">
  326. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">pathname2url</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.pathname2url" title="Link to this definition">¶</a></dt>
  327. <dd><p>Convert the pathname <em>path</em> from the local syntax for a path to the form used in
  328. the path component of a URL. This does not produce a complete URL. The return
  329. value will already be quoted using the <a class="reference internal" href="urllib.parse.html#urllib.parse.quote" title="urllib.parse.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a> function.</p>
  330. </dd></dl>
  331. <dl class="py function">
  332. <dt class="sig sig-object py" id="urllib.request.url2pathname">
  333. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">url2pathname</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.url2pathname" title="Link to this definition">¶</a></dt>
  334. <dd><p>Convert the path component <em>path</em> from a percent-encoded URL to the local syntax for a
  335. path. This does not accept a complete URL. This function uses
  336. <a class="reference internal" href="urllib.parse.html#urllib.parse.unquote" title="urllib.parse.unquote"><code class="xref py py-func docutils literal notranslate"><span class="pre">unquote()</span></code></a> to decode <em>path</em>.</p>
  337. </dd></dl>
  338. <dl class="py function">
  339. <dt class="sig sig-object py" id="urllib.request.getproxies">
  340. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">getproxies</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.getproxies" title="Link to this definition">¶</a></dt>
  341. <dd><p>This helper function returns a dictionary of scheme to proxy server URL
  342. mappings. It scans the environment for variables named <code class="docutils literal notranslate"><span class="pre">&lt;scheme&gt;_proxy</span></code>,
  343. in a case insensitive approach, for all operating systems first, and when it
  344. cannot find it, looks for proxy information from System
  345. Configuration for macOS and Windows Systems Registry for Windows.
  346. If both lowercase and uppercase environment variables exist (and disagree),
  347. lowercase is preferred.</p>
  348. <div class="admonition note">
  349. <p class="admonition-title">Note</p>
  350. <p>If the environment variable <code class="docutils literal notranslate"><span class="pre">REQUEST_METHOD</span></code> is set, which usually
  351. indicates your script is running in a CGI environment, the environment
  352. variable <code class="docutils literal notranslate"><span class="pre">HTTP_PROXY</span></code> (uppercase <code class="docutils literal notranslate"><span class="pre">_PROXY</span></code>) will be ignored. This is
  353. because that variable can be injected by a client using the “Proxy:” HTTP
  354. header. If you need to use an HTTP proxy in a CGI environment, either use
  355. <code class="docutils literal notranslate"><span class="pre">ProxyHandler</span></code> explicitly, or make sure the variable name is in
  356. lowercase (or at least the <code class="docutils literal notranslate"><span class="pre">_proxy</span></code> suffix).</p>
  357. </div>
  358. </dd></dl>
  359. <p>The following classes are provided:</p>
  360. <dl class="py class">
  361. <dt class="sig sig-object py" id="urllib.request.Request">
  362. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">Request</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">origin_req_host</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unverifiable</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request" title="Link to this definition">¶</a></dt>
  363. <dd><p>This class is an abstraction of a URL request.</p>
  364. <p><em>url</em> should be a string containing a valid, properly encoded URL.</p>
  365. <p><em>data</em> must be an object specifying additional data to send to the
  366. server, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no such data is needed. Currently HTTP
  367. requests are the only ones that use <em>data</em>. The supported object
  368. types include bytes, file-like objects, and iterables of bytes-like objects.
  369. If no <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> nor <code class="docutils literal notranslate"><span class="pre">Transfer-Encoding</span></code> header field
  370. has been provided, <a class="reference internal" href="#urllib.request.HTTPHandler" title="urllib.request.HTTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPHandler</span></code></a> will set these headers according
  371. to the type of <em>data</em>. <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> will be used to send
  372. bytes objects, while <code class="docutils literal notranslate"><span class="pre">Transfer-Encoding:</span> <span class="pre">chunked</span></code> as specified in
  373. <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7230.html"><strong>RFC 7230</strong></a>, Section 3.3.1 will be used to send files and other iterables.</p>
  374. <p>For an HTTP POST request method, <em>data</em> should be a buffer in the
  375. standard <em class="mimetype">application/x-www-form-urlencoded</em> format. The
  376. <a class="reference internal" href="urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.urlencode()</span></code></a> function takes a mapping or sequence
  377. of 2-tuples and returns an ASCII string in this format. It should
  378. be encoded to bytes before being used as the <em>data</em> parameter.</p>
  379. <p><em>headers</em> should be a dictionary, and will be treated as if
  380. <a class="reference internal" href="#urllib.request.Request.add_header" title="urllib.request.Request.add_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_header()</span></code></a> was called with each key and value as arguments.
  381. This is often used to “spoof” the <code class="docutils literal notranslate"><span class="pre">User-Agent</span></code> header value, which is
  382. used by a browser to identify itself – some HTTP servers only
  383. allow requests coming from common browsers as opposed to scripts.
  384. For example, Mozilla Firefox may identify itself as <code class="docutils literal notranslate"><span class="pre">&quot;Mozilla/5.0</span>
  385. <span class="pre">(X11;</span> <span class="pre">U;</span> <span class="pre">Linux</span> <span class="pre">i686)</span> <span class="pre">Gecko/20071127</span> <span class="pre">Firefox/2.0.0.11&quot;</span></code>, while
  386. <a class="reference internal" href="urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a>’s default user agent string is
  387. <code class="docutils literal notranslate"><span class="pre">&quot;Python-urllib/2.6&quot;</span></code> (on Python 2.6).
  388. All header keys are sent in camel case.</p>
  389. <p>An appropriate <code class="docutils literal notranslate"><span class="pre">Content-Type</span></code> header should be included if the <em>data</em>
  390. argument is present. If this header has not been provided and <em>data</em>
  391. is not None, <code class="docutils literal notranslate"><span class="pre">Content-Type:</span> <span class="pre">application/x-www-form-urlencoded</span></code> will
  392. be added as a default.</p>
  393. <p>The next two arguments are only of interest for correct handling
  394. of third-party HTTP cookies:</p>
  395. <p><em>origin_req_host</em> should be the request-host of the origin
  396. transaction, as defined by <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>. It defaults to
  397. <code class="docutils literal notranslate"><span class="pre">http.cookiejar.request_host(self)</span></code>. This is the host name or IP
  398. address of the original request that was initiated by the user.
  399. For example, if the request is for an image in an HTML document,
  400. this should be the request-host of the request for the page
  401. containing the image.</p>
  402. <p><em>unverifiable</em> should indicate whether the request is unverifiable,
  403. as defined by <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>. It defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>. An unverifiable
  404. request is one whose URL the user did not have the option to
  405. approve. For example, if the request is for an image in an HTML
  406. document, and the user had no option to approve the automatic
  407. fetching of the image, this should be true.</p>
  408. <p><em>method</em> should be a string that indicates the HTTP request method that
  409. will be used (e.g. <code class="docutils literal notranslate"><span class="pre">'HEAD'</span></code>). If provided, its value is stored in the
  410. <a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">method</span></code></a> attribute and is used by <a class="reference internal" href="#urllib.request.Request.get_method" title="urllib.request.Request.get_method"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_method()</span></code></a>.
  411. The default is <code class="docutils literal notranslate"><span class="pre">'GET'</span></code> if <em>data</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or <code class="docutils literal notranslate"><span class="pre">'POST'</span></code> otherwise.
  412. Subclasses may indicate a different default method by setting the
  413. <a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">method</span></code></a> attribute in the class itself.</p>
  414. <div class="admonition note">
  415. <p class="admonition-title">Note</p>
  416. <p>The request will not work as expected if the data object is unable
  417. to deliver its content more than once (e.g. a file or an iterable
  418. that can produce the content only once) and the request is retried
  419. for HTTP redirects or authentication. The <em>data</em> is sent to the
  420. HTTP server right away after the headers. There is no support for
  421. a 100-continue expectation in the library.</p>
  422. </div>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.method</span></code></a> argument is added to the Request class.</p>
  425. </div>
  426. <div class="versionchanged">
  427. <p><span class="versionmodified changed">Changed in version 3.4: </span>Default <a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.method</span></code></a> may be indicated at the class level.</p>
  428. </div>
  429. <div class="versionchanged">
  430. <p><span class="versionmodified changed">Changed in version 3.6: </span>Do not raise an error if the <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> has not been
  431. provided and <em>data</em> is neither <code class="docutils literal notranslate"><span class="pre">None</span></code> nor a bytes object.
  432. Fall back to use chunked transfer encoding instead.</p>
  433. </div>
  434. </dd></dl>
  435. <dl class="py class">
  436. <dt class="sig sig-object py" id="urllib.request.OpenerDirector">
  437. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">OpenerDirector</span></span><a class="headerlink" href="#urllib.request.OpenerDirector" title="Link to this definition">¶</a></dt>
  438. <dd><p>The <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> class opens URLs via <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>s chained
  439. together. It manages the chaining of handlers, and recovery from errors.</p>
  440. </dd></dl>
  441. <dl class="py class">
  442. <dt class="sig sig-object py" id="urllib.request.BaseHandler">
  443. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">BaseHandler</span></span><a class="headerlink" href="#urllib.request.BaseHandler" title="Link to this definition">¶</a></dt>
  444. <dd><p>This is the base class for all registered handlers — and handles only the
  445. simple mechanics of registration.</p>
  446. </dd></dl>
  447. <dl class="py class">
  448. <dt class="sig sig-object py" id="urllib.request.HTTPDefaultErrorHandler">
  449. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPDefaultErrorHandler</span></span><a class="headerlink" href="#urllib.request.HTTPDefaultErrorHandler" title="Link to this definition">¶</a></dt>
  450. <dd><p>A class which defines a default handler for HTTP error responses; all responses
  451. are turned into <a class="reference internal" href="urllib.error.html#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> exceptions.</p>
  452. </dd></dl>
  453. <dl class="py class">
  454. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler">
  455. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPRedirectHandler</span></span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler" title="Link to this definition">¶</a></dt>
  456. <dd><p>A class to handle redirections.</p>
  457. </dd></dl>
  458. <dl class="py class">
  459. <dt class="sig sig-object py" id="urllib.request.HTTPCookieProcessor">
  460. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPCookieProcessor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cookiejar</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPCookieProcessor" title="Link to this definition">¶</a></dt>
  461. <dd><p>A class to handle HTTP Cookies.</p>
  462. </dd></dl>
  463. <dl class="py class">
  464. <dt class="sig sig-object py" id="urllib.request.ProxyHandler">
  465. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">ProxyHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">proxies</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.ProxyHandler" title="Link to this definition">¶</a></dt>
  466. <dd><p>Cause requests to go through a proxy. If <em>proxies</em> is given, it must be a
  467. dictionary mapping protocol names to URLs of proxies. The default is to read
  468. the list of proxies from the environment variables
  469. <code class="docutils literal notranslate"><span class="pre">&lt;protocol&gt;_proxy</span></code>. If no proxy environment variables are set, then
  470. in a Windows environment proxy settings are obtained from the registry’s
  471. Internet Settings section, and in a macOS environment proxy information
  472. is retrieved from the System Configuration Framework.</p>
  473. <p>To disable autodetected proxy pass an empty dictionary.</p>
  474. <p>The <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">no_proxy</span></code> environment variable can be used to specify hosts
  475. which shouldn’t be reached via proxy; if set, it should be a comma-separated
  476. list of hostname suffixes, optionally with <code class="docutils literal notranslate"><span class="pre">:port</span></code> appended, for example
  477. <code class="docutils literal notranslate"><span class="pre">cern.ch,ncsa.uiuc.edu,some.host:8080</span></code>.</p>
  478. <div class="admonition note">
  479. <p class="admonition-title">Note</p>
  480. <p><code class="docutils literal notranslate"><span class="pre">HTTP_PROXY</span></code> will be ignored if a variable <code class="docutils literal notranslate"><span class="pre">REQUEST_METHOD</span></code> is set;
  481. see the documentation on <a class="reference internal" href="#urllib.request.getproxies" title="urllib.request.getproxies"><code class="xref py py-func docutils literal notranslate"><span class="pre">getproxies()</span></code></a>.</p>
  482. </div>
  483. </dd></dl>
  484. <dl class="py class">
  485. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgr">
  486. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPPasswordMgr</span></span><a class="headerlink" href="#urllib.request.HTTPPasswordMgr" title="Link to this definition">¶</a></dt>
  487. <dd><p>Keep a database of <code class="docutils literal notranslate"><span class="pre">(realm,</span> <span class="pre">uri)</span> <span class="pre">-&gt;</span> <span class="pre">(user,</span> <span class="pre">password)</span></code> mappings.</p>
  488. </dd></dl>
  489. <dl class="py class">
  490. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithDefaultRealm">
  491. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="Link to this definition">¶</a></dt>
  492. <dd><p>Keep a database of <code class="docutils literal notranslate"><span class="pre">(realm,</span> <span class="pre">uri)</span> <span class="pre">-&gt;</span> <span class="pre">(user,</span> <span class="pre">password)</span></code> mappings. A realm of
  493. <code class="docutils literal notranslate"><span class="pre">None</span></code> is considered a catch-all realm, which is searched if no other realm
  494. fits.</p>
  495. </dd></dl>
  496. <dl class="py class">
  497. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth">
  498. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPPasswordMgrWithPriorAuth</span></span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithPriorAuth" title="Link to this definition">¶</a></dt>
  499. <dd><p>A variant of <a class="reference internal" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="urllib.request.HTTPPasswordMgrWithDefaultRealm"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></code></a> that also has a
  500. database of <code class="docutils literal notranslate"><span class="pre">uri</span> <span class="pre">-&gt;</span> <span class="pre">is_authenticated</span></code> mappings. Can be used by a
  501. BasicAuth handler to determine when to send authentication credentials
  502. immediately instead of waiting for a <code class="docutils literal notranslate"><span class="pre">401</span></code> response first.</p>
  503. <div class="versionadded">
  504. <p><span class="versionmodified added">New in version 3.5.</span></p>
  505. </div>
  506. </dd></dl>
  507. <dl class="py class">
  508. <dt class="sig sig-object py" id="urllib.request.AbstractBasicAuthHandler">
  509. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">AbstractBasicAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.AbstractBasicAuthHandler" title="Link to this definition">¶</a></dt>
  510. <dd><p>This is a mixin class that helps with HTTP authentication, both to the remote
  511. host and to a proxy. <em>password_mgr</em>, if given, should be something that is
  512. compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to section
  513. <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be
  514. supported. If <em>passwd_mgr</em> also provides <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> and
  515. <code class="docutils literal notranslate"><span class="pre">update_authenticated</span></code> methods (see
  516. <a class="reference internal" href="#http-password-mgr-with-prior-auth"><span class="std std-ref">HTTPPasswordMgrWithPriorAuth Objects</span></a>), then the handler will use the
  517. <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> result for a given URI to determine whether or not to
  518. send authentication credentials with the request. If <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code>
  519. returns <code class="docutils literal notranslate"><span class="pre">True</span></code> for the URI, credentials are sent. If <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code>
  520. is <code class="docutils literal notranslate"><span class="pre">False</span></code>, credentials are not sent, and then if a <code class="docutils literal notranslate"><span class="pre">401</span></code> response is
  521. received the request is re-sent with the authentication credentials. If
  522. authentication succeeds, <code class="docutils literal notranslate"><span class="pre">update_authenticated</span></code> is called to set
  523. <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> <code class="docutils literal notranslate"><span class="pre">True</span></code> for the URI, so that subsequent requests to
  524. the URI or any of its super-URIs will automatically include the
  525. authentication credentials.</p>
  526. <div class="versionadded">
  527. <p><span class="versionmodified added">New in version 3.5: </span>Added <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> support.</p>
  528. </div>
  529. </dd></dl>
  530. <dl class="py class">
  531. <dt class="sig sig-object py" id="urllib.request.HTTPBasicAuthHandler">
  532. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPBasicAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPBasicAuthHandler" title="Link to this definition">¶</a></dt>
  533. <dd><p>Handle authentication with the remote host. <em>password_mgr</em>, if given, should
  534. be something that is compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to
  535. section <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must
  536. be supported. HTTPBasicAuthHandler will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> when
  537. presented with a wrong Authentication scheme.</p>
  538. </dd></dl>
  539. <dl class="py class">
  540. <dt class="sig sig-object py" id="urllib.request.ProxyBasicAuthHandler">
  541. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">ProxyBasicAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.ProxyBasicAuthHandler" title="Link to this definition">¶</a></dt>
  542. <dd><p>Handle authentication with the proxy. <em>password_mgr</em>, if given, should be
  543. something that is compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to section
  544. <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be
  545. supported.</p>
  546. </dd></dl>
  547. <dl class="py class">
  548. <dt class="sig sig-object py" id="urllib.request.AbstractDigestAuthHandler">
  549. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">AbstractDigestAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.AbstractDigestAuthHandler" title="Link to this definition">¶</a></dt>
  550. <dd><p>This is a mixin class that helps with HTTP authentication, both to the remote
  551. host and to a proxy. <em>password_mgr</em>, if given, should be something that is
  552. compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to section
  553. <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be
  554. supported.</p>
  555. </dd></dl>
  556. <dl class="py class">
  557. <dt class="sig sig-object py" id="urllib.request.HTTPDigestAuthHandler">
  558. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPDigestAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPDigestAuthHandler" title="Link to this definition">¶</a></dt>
  559. <dd><p>Handle authentication with the remote host. <em>password_mgr</em>, if given, should
  560. be something that is compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to
  561. section <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must
  562. be supported. When both Digest Authentication Handler and Basic
  563. Authentication Handler are both added, Digest Authentication is always tried
  564. first. If the Digest Authentication returns a 40x response again, it is sent
  565. to Basic Authentication handler to Handle. This Handler method will raise a
  566. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> when presented with an authentication scheme other than
  567. Digest or Basic.</p>
  568. <div class="versionchanged">
  569. <p><span class="versionmodified changed">Changed in version 3.3: </span>Raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on unsupported Authentication Scheme.</p>
  570. </div>
  571. </dd></dl>
  572. <dl class="py class">
  573. <dt class="sig sig-object py" id="urllib.request.ProxyDigestAuthHandler">
  574. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">ProxyDigestAuthHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password_mgr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.ProxyDigestAuthHandler" title="Link to this definition">¶</a></dt>
  575. <dd><p>Handle authentication with the proxy. <em>password_mgr</em>, if given, should be
  576. something that is compatible with <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a>; refer to section
  577. <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be
  578. supported.</p>
  579. </dd></dl>
  580. <dl class="py class">
  581. <dt class="sig sig-object py" id="urllib.request.HTTPHandler">
  582. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPHandler</span></span><a class="headerlink" href="#urllib.request.HTTPHandler" title="Link to this definition">¶</a></dt>
  583. <dd><p>A class to handle opening of HTTP URLs.</p>
  584. </dd></dl>
  585. <dl class="py class">
  586. <dt class="sig sig-object py" id="urllib.request.HTTPSHandler">
  587. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPSHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">debuglevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_hostname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPSHandler" title="Link to this definition">¶</a></dt>
  588. <dd><p>A class to handle opening of HTTPS URLs. <em>context</em> and <em>check_hostname</em>
  589. have the same meaning as in <a class="reference internal" href="http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPSConnection</span></code></a>.</p>
  590. <div class="versionchanged">
  591. <p><span class="versionmodified changed">Changed in version 3.2: </span><em>context</em> and <em>check_hostname</em> were added.</p>
  592. </div>
  593. </dd></dl>
  594. <dl class="py class">
  595. <dt class="sig sig-object py" id="urllib.request.FileHandler">
  596. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">FileHandler</span></span><a class="headerlink" href="#urllib.request.FileHandler" title="Link to this definition">¶</a></dt>
  597. <dd><p>Open local files.</p>
  598. </dd></dl>
  599. <dl class="py class">
  600. <dt class="sig sig-object py" id="urllib.request.DataHandler">
  601. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">DataHandler</span></span><a class="headerlink" href="#urllib.request.DataHandler" title="Link to this definition">¶</a></dt>
  602. <dd><p>Open data URLs.</p>
  603. <div class="versionadded">
  604. <p><span class="versionmodified added">New in version 3.4.</span></p>
  605. </div>
  606. </dd></dl>
  607. <dl class="py class">
  608. <dt class="sig sig-object py" id="urllib.request.FTPHandler">
  609. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">FTPHandler</span></span><a class="headerlink" href="#urllib.request.FTPHandler" title="Link to this definition">¶</a></dt>
  610. <dd><p>Open FTP URLs.</p>
  611. </dd></dl>
  612. <dl class="py class">
  613. <dt class="sig sig-object py" id="urllib.request.CacheFTPHandler">
  614. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">CacheFTPHandler</span></span><a class="headerlink" href="#urllib.request.CacheFTPHandler" title="Link to this definition">¶</a></dt>
  615. <dd><p>Open FTP URLs, keeping a cache of open FTP connections to minimize delays.</p>
  616. </dd></dl>
  617. <dl class="py class">
  618. <dt class="sig sig-object py" id="urllib.request.UnknownHandler">
  619. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">UnknownHandler</span></span><a class="headerlink" href="#urllib.request.UnknownHandler" title="Link to this definition">¶</a></dt>
  620. <dd><p>A catch-all class to handle unknown URLs.</p>
  621. </dd></dl>
  622. <dl class="py class">
  623. <dt class="sig sig-object py" id="urllib.request.HTTPErrorProcessor">
  624. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">HTTPErrorProcessor</span></span><a class="headerlink" href="#urllib.request.HTTPErrorProcessor" title="Link to this definition">¶</a></dt>
  625. <dd><p>Process HTTP error responses.</p>
  626. </dd></dl>
  627. <section id="request-objects">
  628. <span id="id1"></span><h2>Request Objects<a class="headerlink" href="#request-objects" title="Link to this heading">¶</a></h2>
  629. <p>The following methods describe <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a>’s public interface,
  630. and so all may be overridden in subclasses. It also defines several
  631. public attributes that can be used by clients to inspect the parsed
  632. request.</p>
  633. <dl class="py attribute">
  634. <dt class="sig sig-object py" id="urllib.request.Request.full_url">
  635. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">full_url</span></span><a class="headerlink" href="#urllib.request.Request.full_url" title="Link to this definition">¶</a></dt>
  636. <dd><p>The original URL passed to the constructor.</p>
  637. <div class="versionchanged">
  638. <p><span class="versionmodified changed">Changed in version 3.4.</span></p>
  639. </div>
  640. <p>Request.full_url is a property with setter, getter and a deleter. Getting
  641. <a class="reference internal" href="#urllib.request.Request.full_url" title="urllib.request.Request.full_url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">full_url</span></code></a> returns the original request URL with the
  642. fragment, if it was present.</p>
  643. </dd></dl>
  644. <dl class="py attribute">
  645. <dt class="sig sig-object py" id="urllib.request.Request.type">
  646. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">type</span></span><a class="headerlink" href="#urllib.request.Request.type" title="Link to this definition">¶</a></dt>
  647. <dd><p>The URI scheme.</p>
  648. </dd></dl>
  649. <dl class="py attribute">
  650. <dt class="sig sig-object py" id="urllib.request.Request.host">
  651. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">host</span></span><a class="headerlink" href="#urllib.request.Request.host" title="Link to this definition">¶</a></dt>
  652. <dd><p>The URI authority, typically a host, but may also contain a port
  653. separated by a colon.</p>
  654. </dd></dl>
  655. <dl class="py attribute">
  656. <dt class="sig sig-object py" id="urllib.request.Request.origin_req_host">
  657. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">origin_req_host</span></span><a class="headerlink" href="#urllib.request.Request.origin_req_host" title="Link to this definition">¶</a></dt>
  658. <dd><p>The original host for the request, without port.</p>
  659. </dd></dl>
  660. <dl class="py attribute">
  661. <dt class="sig sig-object py" id="urllib.request.Request.selector">
  662. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">selector</span></span><a class="headerlink" href="#urllib.request.Request.selector" title="Link to this definition">¶</a></dt>
  663. <dd><p>The URI path. If the <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> uses a proxy, then selector
  664. will be the full URL that is passed to the proxy.</p>
  665. </dd></dl>
  666. <dl class="py attribute">
  667. <dt class="sig sig-object py" id="urllib.request.Request.data">
  668. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">data</span></span><a class="headerlink" href="#urllib.request.Request.data" title="Link to this definition">¶</a></dt>
  669. <dd><p>The entity body for the request, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if not specified.</p>
  670. <div class="versionchanged">
  671. <p><span class="versionmodified changed">Changed in version 3.4: </span>Changing value of <a class="reference internal" href="#urllib.request.Request.data" title="urllib.request.Request.data"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.data</span></code></a> now deletes “Content-Length”
  672. header if it was previously set or calculated.</p>
  673. </div>
  674. </dd></dl>
  675. <dl class="py attribute">
  676. <dt class="sig sig-object py" id="urllib.request.Request.unverifiable">
  677. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">unverifiable</span></span><a class="headerlink" href="#urllib.request.Request.unverifiable" title="Link to this definition">¶</a></dt>
  678. <dd><p>boolean, indicates whether the request is unverifiable as defined
  679. by <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>.</p>
  680. </dd></dl>
  681. <dl class="py attribute">
  682. <dt class="sig sig-object py" id="urllib.request.Request.method">
  683. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">method</span></span><a class="headerlink" href="#urllib.request.Request.method" title="Link to this definition">¶</a></dt>
  684. <dd><p>The HTTP request method to use. By default its value is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>,
  685. which means that <a class="reference internal" href="#urllib.request.Request.get_method" title="urllib.request.Request.get_method"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_method()</span></code></a> will do its normal computation
  686. of the method to be used. Its value can be set (thus overriding the default
  687. computation in <a class="reference internal" href="#urllib.request.Request.get_method" title="urllib.request.Request.get_method"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_method()</span></code></a>) either by providing a default
  688. value by setting it at the class level in a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> subclass, or by
  689. passing a value in to the <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> constructor via the <em>method</em>
  690. argument.</p>
  691. <div class="versionadded">
  692. <p><span class="versionmodified added">New in version 3.3.</span></p>
  693. </div>
  694. <div class="versionchanged">
  695. <p><span class="versionmodified changed">Changed in version 3.4: </span>A default value can now be set in subclasses; previously it could only
  696. be set via the constructor argument.</p>
  697. </div>
  698. </dd></dl>
  699. <dl class="py method">
  700. <dt class="sig sig-object py" id="urllib.request.Request.get_method">
  701. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">get_method</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.get_method" title="Link to this definition">¶</a></dt>
  702. <dd><p>Return a string indicating the HTTP request method. If
  703. <a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.method</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, return its value, otherwise return
  704. <code class="docutils literal notranslate"><span class="pre">'GET'</span></code> if <a class="reference internal" href="#urllib.request.Request.data" title="urllib.request.Request.data"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.data</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, or <code class="docutils literal notranslate"><span class="pre">'POST'</span></code> if it’s not.
  705. This is only meaningful for HTTP requests.</p>
  706. <div class="versionchanged">
  707. <p><span class="versionmodified changed">Changed in version 3.3: </span>get_method now looks at the value of <a class="reference internal" href="#urllib.request.Request.method" title="urllib.request.Request.method"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.method</span></code></a>.</p>
  708. </div>
  709. </dd></dl>
  710. <dl class="py method">
  711. <dt class="sig sig-object py" id="urllib.request.Request.add_header">
  712. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">add_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">val</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.add_header" title="Link to this definition">¶</a></dt>
  713. <dd><p>Add another header to the request. Headers are currently ignored by all
  714. handlers except HTTP handlers, where they are added to the list of headers sent
  715. to the server. Note that there cannot be more than one header with the same
  716. name, and later calls will overwrite previous calls in case the <em>key</em> collides.
  717. Currently, this is no loss of HTTP functionality, since all headers which have
  718. meaning when used more than once have a (header-specific) way of gaining the
  719. same functionality using only one header. Note that headers added using
  720. this method are also added to redirected requests.</p>
  721. </dd></dl>
  722. <dl class="py method">
  723. <dt class="sig sig-object py" id="urllib.request.Request.add_unredirected_header">
  724. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">add_unredirected_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.add_unredirected_header" title="Link to this definition">¶</a></dt>
  725. <dd><p>Add a header that will not be added to a redirected request.</p>
  726. </dd></dl>
  727. <dl class="py method">
  728. <dt class="sig sig-object py" id="urllib.request.Request.has_header">
  729. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">has_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">header</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.has_header" title="Link to this definition">¶</a></dt>
  730. <dd><p>Return whether the instance has the named header (checks both regular and
  731. unredirected).</p>
  732. </dd></dl>
  733. <dl class="py method">
  734. <dt class="sig sig-object py" id="urllib.request.Request.remove_header">
  735. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">remove_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">header</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.remove_header" title="Link to this definition">¶</a></dt>
  736. <dd><p>Remove named header from the request instance (both from regular and
  737. unredirected headers).</p>
  738. <div class="versionadded">
  739. <p><span class="versionmodified added">New in version 3.4.</span></p>
  740. </div>
  741. </dd></dl>
  742. <dl class="py method">
  743. <dt class="sig sig-object py" id="urllib.request.Request.get_full_url">
  744. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">get_full_url</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.get_full_url" title="Link to this definition">¶</a></dt>
  745. <dd><p>Return the URL given in the constructor.</p>
  746. <div class="versionchanged">
  747. <p><span class="versionmodified changed">Changed in version 3.4.</span></p>
  748. </div>
  749. <p>Returns <a class="reference internal" href="#urllib.request.Request.full_url" title="urllib.request.Request.full_url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.full_url</span></code></a></p>
  750. </dd></dl>
  751. <dl class="py method">
  752. <dt class="sig sig-object py" id="urllib.request.Request.set_proxy">
  753. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">set_proxy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">type</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.set_proxy" title="Link to this definition">¶</a></dt>
  754. <dd><p>Prepare the request by connecting to a proxy server. The <em>host</em> and <em>type</em> will
  755. replace those of the instance, and the instance’s selector will be the original
  756. URL given in the constructor.</p>
  757. </dd></dl>
  758. <dl class="py method">
  759. <dt class="sig sig-object py" id="urllib.request.Request.get_header">
  760. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">get_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">header_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.get_header" title="Link to this definition">¶</a></dt>
  761. <dd><p>Return the value of the given header. If the header is not present, return
  762. the default value.</p>
  763. </dd></dl>
  764. <dl class="py method">
  765. <dt class="sig sig-object py" id="urllib.request.Request.header_items">
  766. <span class="sig-prename descclassname"><span class="pre">Request.</span></span><span class="sig-name descname"><span class="pre">header_items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.Request.header_items" title="Link to this definition">¶</a></dt>
  767. <dd><p>Return a list of tuples (header_name, header_value) of the Request headers.</p>
  768. </dd></dl>
  769. <div class="versionchanged">
  770. <p><span class="versionmodified changed">Changed in version 3.4: </span>The request methods add_data, has_data, get_data, get_type, get_host,
  771. get_selector, get_origin_req_host and is_unverifiable that were deprecated
  772. since 3.3 have been removed.</p>
  773. </div>
  774. </section>
  775. <section id="openerdirector-objects">
  776. <span id="opener-director-objects"></span><h2>OpenerDirector Objects<a class="headerlink" href="#openerdirector-objects" title="Link to this heading">¶</a></h2>
  777. <p><a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> instances have the following methods:</p>
  778. <dl class="py method">
  779. <dt class="sig sig-object py" id="urllib.request.OpenerDirector.add_handler">
  780. <span class="sig-prename descclassname"><span class="pre">OpenerDirector.</span></span><span class="sig-name descname"><span class="pre">add_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.OpenerDirector.add_handler" title="Link to this definition">¶</a></dt>
  781. <dd><p><em>handler</em> should be an instance of <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>. The following methods
  782. are searched, and added to the possible chains (note that HTTP errors are a
  783. special case). Note that, in the following, <em>protocol</em> should be replaced
  784. with the actual protocol to handle, for example <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_response()</span></code> would
  785. be the HTTP protocol response handler. Also <em>type</em> should be replaced with
  786. the actual HTTP code, for example <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_404()</span></code> would handle HTTP
  787. 404 errors.</p>
  788. <ul>
  789. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_open()</span></code> — signal that the handler knows how to open <em>protocol</em>
  790. URLs.</p>
  791. <p>See <a class="reference internal" href="#protocol-open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.&lt;protocol&gt;_open()</span></code></a> for more information.</p>
  792. </li>
  793. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_&lt;type&gt;()</span></code> — signal that the handler knows how to handle HTTP
  794. errors with HTTP error code <em>type</em>.</p>
  795. <p>See <a class="reference internal" href="#http-error-nnn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.http_error_&lt;nnn&gt;()</span></code></a> for more information.</p>
  796. </li>
  797. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_error()</span></code> — signal that the handler knows how to handle errors
  798. from (non-<code class="docutils literal notranslate"><span class="pre">http</span></code>) <em>protocol</em>.</p></li>
  799. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_request()</span></code> — signal that the handler knows how to pre-process
  800. <em>protocol</em> requests.</p>
  801. <p>See <a class="reference internal" href="#protocol-request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.&lt;protocol&gt;_request()</span></code></a> for more information.</p>
  802. </li>
  803. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_response()</span></code> — signal that the handler knows how to
  804. post-process <em>protocol</em> responses.</p>
  805. <p>See <a class="reference internal" href="#protocol-response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.&lt;protocol&gt;_response()</span></code></a> for more information.</p>
  806. </li>
  807. </ul>
  808. </dd></dl>
  809. <dl class="py method">
  810. <dt class="sig sig-object py" id="urllib.request.OpenerDirector.open">
  811. <span class="sig-prename descclassname"><span class="pre">OpenerDirector.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data=None</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.OpenerDirector.open" title="Link to this definition">¶</a></dt>
  812. <dd><p>Open the given <em>url</em> (which can be a request object or a string), optionally
  813. passing the given <em>data</em>. Arguments, return values and exceptions raised are
  814. the same as those of <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> (which simply calls the <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a>
  815. method on the currently installed global <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>). The
  816. optional <em>timeout</em> parameter specifies a timeout in seconds for blocking
  817. operations like the connection attempt (if not specified, the global default
  818. timeout setting will be used). The timeout feature actually works only for
  819. HTTP, HTTPS and FTP connections.</p>
  820. </dd></dl>
  821. <dl class="py method">
  822. <dt class="sig sig-object py" id="urllib.request.OpenerDirector.error">
  823. <span class="sig-prename descclassname"><span class="pre">OpenerDirector.</span></span><span class="sig-name descname"><span class="pre">error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">proto</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.OpenerDirector.error" title="Link to this definition">¶</a></dt>
  824. <dd><p>Handle an error of the given protocol. This will call the registered error
  825. handlers for the given protocol with the given arguments (which are protocol
  826. specific). The HTTP protocol is a special case which uses the HTTP response
  827. code to determine the specific error handler; refer to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_&lt;type&gt;()</span></code>
  828. methods of the handler classes.</p>
  829. <p>Return values and exceptions raised are the same as those of <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>.</p>
  830. </dd></dl>
  831. <p>OpenerDirector objects open URLs in three stages:</p>
  832. <p>The order in which these methods are called within each stage is determined by
  833. sorting the handler instances.</p>
  834. <ol class="arabic">
  835. <li><p>Every handler with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_request()</span></code> has that
  836. method called to pre-process the request.</p></li>
  837. <li><p>Handlers with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_open()</span></code> are called to handle
  838. the request. This stage ends when a handler either returns a non-<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>
  839. value (ie. a response), or raises an exception (usually
  840. <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>). Exceptions are allowed to propagate.</p>
  841. <p>In fact, the above algorithm is first tried for methods named
  842. <a class="reference internal" href="#urllib.request.BaseHandler.default_open" title="urllib.request.BaseHandler.default_open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default_open()</span></code></a>. If all such methods return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the algorithm
  843. is repeated for methods named like <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_open()</span></code>. If all such methods
  844. return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the algorithm is repeated for methods named
  845. <a class="reference internal" href="#urllib.request.BaseHandler.unknown_open" title="urllib.request.BaseHandler.unknown_open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unknown_open()</span></code></a>.</p>
  846. <p>Note that the implementation of these methods may involve calls of the parent
  847. <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> instance’s <a class="reference internal" href="#urllib.request.OpenerDirector.open" title="urllib.request.OpenerDirector.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a> and
  848. <a class="reference internal" href="#urllib.request.OpenerDirector.error" title="urllib.request.OpenerDirector.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code></a> methods.</p>
  849. </li>
  850. <li><p>Every handler with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_response()</span></code> has that
  851. method called to post-process the response.</p></li>
  852. </ol>
  853. </section>
  854. <section id="basehandler-objects">
  855. <span id="base-handler-objects"></span><h2>BaseHandler Objects<a class="headerlink" href="#basehandler-objects" title="Link to this heading">¶</a></h2>
  856. <p><a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a> objects provide a couple of methods that are directly
  857. useful, and others that are meant to be used by derived classes. These are
  858. intended for direct use:</p>
  859. <dl class="py method">
  860. <dt class="sig sig-object py" id="urllib.request.BaseHandler.add_parent">
  861. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">add_parent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">director</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.BaseHandler.add_parent" title="Link to this definition">¶</a></dt>
  862. <dd><p>Add a director as parent.</p>
  863. </dd></dl>
  864. <dl class="py method">
  865. <dt class="sig sig-object py" id="urllib.request.BaseHandler.close">
  866. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.BaseHandler.close" title="Link to this definition">¶</a></dt>
  867. <dd><p>Remove any parents.</p>
  868. </dd></dl>
  869. <p>The following attribute and methods should only be used by classes derived from
  870. <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>.</p>
  871. <div class="admonition note">
  872. <p class="admonition-title">Note</p>
  873. <p>The convention has been adopted that subclasses defining
  874. <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_request()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_response()</span></code> methods are named
  875. <code class="xref py py-class docutils literal notranslate"><span class="pre">*Processor</span></code>; all others are named <code class="xref py py-class docutils literal notranslate"><span class="pre">*Handler</span></code>.</p>
  876. </div>
  877. <dl class="py attribute">
  878. <dt class="sig sig-object py" id="urllib.request.BaseHandler.parent">
  879. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">parent</span></span><a class="headerlink" href="#urllib.request.BaseHandler.parent" title="Link to this definition">¶</a></dt>
  880. <dd><p>A valid <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>, which can be used to open using a different
  881. protocol, or handle errors.</p>
  882. </dd></dl>
  883. <dl class="py method">
  884. <dt class="sig sig-object py" id="urllib.request.BaseHandler.default_open">
  885. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">default_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.BaseHandler.default_open" title="Link to this definition">¶</a></dt>
  886. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  887. define it if they want to catch all URLs.</p>
  888. <p>This method, if implemented, will be called by the parent
  889. <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>. It should return a file-like object as described in
  890. the return value of the <a class="reference internal" href="#urllib.request.OpenerDirector.open" title="urllib.request.OpenerDirector.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a> method of <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.
  891. It should raise <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>, unless a truly exceptional
  892. thing happens (for example, <a class="reference internal" href="exceptions.html#MemoryError" title="MemoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MemoryError</span></code></a> should not be mapped to
  893. <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>).</p>
  894. <p>This method will be called before any protocol-specific open method.</p>
  895. </dd></dl>
  896. <dl class="py method" id="protocol-open">
  897. <dt class="sig sig-object py">
  898. <span class="sig-name descname"><span class="pre">BaseHandler.&lt;protocol&gt;_open(req)</span></span></dt>
  899. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  900. define it if they want to handle URLs with the given protocol.</p>
  901. <p>This method, if defined, will be called by the parent <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>.
  902. Return values should be the same as for <a class="reference internal" href="#urllib.request.BaseHandler.default_open" title="urllib.request.BaseHandler.default_open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default_open()</span></code></a>.</p>
  903. </dd></dl>
  904. <dl class="py method">
  905. <dt class="sig sig-object py" id="urllib.request.BaseHandler.unknown_open">
  906. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">unknown_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.BaseHandler.unknown_open" title="Link to this definition">¶</a></dt>
  907. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  908. define it if they want to catch all URLs with no specific registered handler to
  909. open it.</p>
  910. <p>This method, if implemented, will be called by the <a class="reference internal" href="#urllib.request.BaseHandler.parent" title="urllib.request.BaseHandler.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">parent</span></code></a>
  911. <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>. Return values should be the same as for
  912. <a class="reference internal" href="#urllib.request.BaseHandler.default_open" title="urllib.request.BaseHandler.default_open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default_open()</span></code></a>.</p>
  913. </dd></dl>
  914. <dl class="py method">
  915. <dt class="sig sig-object py" id="urllib.request.BaseHandler.http_error_default">
  916. <span class="sig-prename descclassname"><span class="pre">BaseHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_default</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.BaseHandler.http_error_default" title="Link to this definition">¶</a></dt>
  917. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  918. override it if they intend to provide a catch-all for otherwise unhandled HTTP
  919. errors. It will be called automatically by the <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> getting
  920. the error, and should not normally be called in other circumstances.</p>
  921. <p><em>req</em> will be a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object, <em>fp</em> will be a file-like object with
  922. the HTTP error body, <em>code</em> will be the three-digit code of the error, <em>msg</em>
  923. will be the user-visible explanation of the code and <em>hdrs</em> will be a mapping
  924. object with the headers of the error.</p>
  925. <p>Return values and exceptions raised should be the same as those of
  926. <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>.</p>
  927. </dd></dl>
  928. <dl class="py method" id="http-error-nnn">
  929. <dt class="sig sig-object py">
  930. <span class="sig-name descname"><span class="pre">BaseHandler.http_error_&lt;nnn&gt;(req,</span> <span class="pre">fp,</span> <span class="pre">code,</span> <span class="pre">msg,</span> <span class="pre">hdrs)</span></span></dt>
  931. <dd><p><em>nnn</em> should be a three-digit HTTP error code. This method is also not defined
  932. in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but will be called, if it exists, on an instance of a
  933. subclass, when an HTTP error with code <em>nnn</em> occurs.</p>
  934. <p>Subclasses should override this method to handle specific HTTP errors.</p>
  935. <p>Arguments, return values and exceptions raised should be the same as for
  936. <a class="reference internal" href="#urllib.request.BaseHandler.http_error_default" title="urllib.request.BaseHandler.http_error_default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_default()</span></code></a>.</p>
  937. </dd></dl>
  938. <dl class="py method" id="protocol-request">
  939. <dt class="sig sig-object py">
  940. <span class="sig-name descname"><span class="pre">BaseHandler.&lt;protocol&gt;_request(req)</span></span></dt>
  941. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  942. define it if they want to pre-process requests of the given protocol.</p>
  943. <p>This method, if defined, will be called by the parent <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>.
  944. <em>req</em> will be a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object. The return value should be a
  945. <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object.</p>
  946. </dd></dl>
  947. <dl class="py method" id="protocol-response">
  948. <dt class="sig sig-object py">
  949. <span class="sig-name descname"><span class="pre">BaseHandler.&lt;protocol&gt;_response(req,</span> <span class="pre">response)</span></span></dt>
  950. <dd><p>This method is <em>not</em> defined in <a class="reference internal" href="#urllib.request.BaseHandler" title="urllib.request.BaseHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code></a>, but subclasses should
  951. define it if they want to post-process responses of the given protocol.</p>
  952. <p>This method, if defined, will be called by the parent <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a>.
  953. <em>req</em> will be a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object. <em>response</em> will be an object
  954. implementing the same interface as the return value of <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>. The
  955. return value should implement the same interface as the return value of
  956. <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>.</p>
  957. </dd></dl>
  958. </section>
  959. <section id="httpredirecthandler-objects">
  960. <span id="http-redirect-handler"></span><h2>HTTPRedirectHandler Objects<a class="headerlink" href="#httpredirecthandler-objects" title="Link to this heading">¶</a></h2>
  961. <div class="admonition note">
  962. <p class="admonition-title">Note</p>
  963. <p>Some HTTP redirections require action from this module’s client code. If this
  964. is the case, <a class="reference internal" href="urllib.error.html#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> is raised. See <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a> for
  965. details of the precise meanings of the various redirection codes.</p>
  966. <p>An <a class="reference internal" href="urllib.error.html#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> exception raised as a security consideration if the
  967. HTTPRedirectHandler is presented with a redirected URL which is not an HTTP,
  968. HTTPS or FTP URL.</p>
  969. </div>
  970. <dl class="py method">
  971. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.redirect_request">
  972. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">redirect_request</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newurl</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.redirect_request" title="Link to this definition">¶</a></dt>
  973. <dd><p>Return a <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> or <code class="docutils literal notranslate"><span class="pre">None</span></code> in response to a redirect. This is called
  974. by the default implementations of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_30*()</span></code> methods when a
  975. redirection is received from the server. If a redirection should take place,
  976. return a new <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> to allow <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_30*()</span></code> to perform the
  977. redirect to <em>newurl</em>. Otherwise, raise <a class="reference internal" href="urllib.error.html#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> if
  978. no other handler should try to handle this URL, or return <code class="docutils literal notranslate"><span class="pre">None</span></code> if you
  979. can’t but another handler might.</p>
  980. <div class="admonition note">
  981. <p class="admonition-title">Note</p>
  982. <p>The default implementation of this method does not strictly follow <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a>,
  983. which says that 301 and 302 responses to <code class="docutils literal notranslate"><span class="pre">POST</span></code> requests must not be
  984. automatically redirected without confirmation by the user. In reality, browsers
  985. do allow automatic redirection of these responses, changing the POST to a
  986. <code class="docutils literal notranslate"><span class="pre">GET</span></code>, and the default implementation reproduces this behavior.</p>
  987. </div>
  988. </dd></dl>
  989. <dl class="py method">
  990. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_301">
  991. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_301</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.http_error_301" title="Link to this definition">¶</a></dt>
  992. <dd><p>Redirect to the <code class="docutils literal notranslate"><span class="pre">Location:</span></code> or <code class="docutils literal notranslate"><span class="pre">URI:</span></code> URL. This method is called by the
  993. parent <a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> when getting an HTTP ‘moved permanently’ response.</p>
  994. </dd></dl>
  995. <dl class="py method">
  996. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_302">
  997. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_302</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.http_error_302" title="Link to this definition">¶</a></dt>
  998. <dd><p>The same as <a class="reference internal" href="#urllib.request.HTTPRedirectHandler.http_error_301" title="urllib.request.HTTPRedirectHandler.http_error_301"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_301()</span></code></a>, but called for the ‘found’ response.</p>
  999. </dd></dl>
  1000. <dl class="py method">
  1001. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_303">
  1002. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_303</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.http_error_303" title="Link to this definition">¶</a></dt>
  1003. <dd><p>The same as <a class="reference internal" href="#urllib.request.HTTPRedirectHandler.http_error_301" title="urllib.request.HTTPRedirectHandler.http_error_301"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_301()</span></code></a>, but called for the ‘see other’ response.</p>
  1004. </dd></dl>
  1005. <dl class="py method">
  1006. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_307">
  1007. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_307</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.http_error_307" title="Link to this definition">¶</a></dt>
  1008. <dd><p>The same as <a class="reference internal" href="#urllib.request.HTTPRedirectHandler.http_error_301" title="urllib.request.HTTPRedirectHandler.http_error_301"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_301()</span></code></a>, but called for the ‘temporary redirect’
  1009. response. It does not allow changing the request method from <code class="docutils literal notranslate"><span class="pre">POST</span></code>
  1010. to <code class="docutils literal notranslate"><span class="pre">GET</span></code>.</p>
  1011. </dd></dl>
  1012. <dl class="py method">
  1013. <dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_308">
  1014. <span class="sig-prename descclassname"><span class="pre">HTTPRedirectHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_308</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPRedirectHandler.http_error_308" title="Link to this definition">¶</a></dt>
  1015. <dd><p>The same as <a class="reference internal" href="#urllib.request.HTTPRedirectHandler.http_error_301" title="urllib.request.HTTPRedirectHandler.http_error_301"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_301()</span></code></a>, but called for the ‘permanent redirect’
  1016. response. It does not allow changing the request method from <code class="docutils literal notranslate"><span class="pre">POST</span></code>
  1017. to <code class="docutils literal notranslate"><span class="pre">GET</span></code>.</p>
  1018. <div class="versionadded">
  1019. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1020. </div>
  1021. </dd></dl>
  1022. </section>
  1023. <section id="httpcookieprocessor-objects">
  1024. <span id="http-cookie-processor"></span><h2>HTTPCookieProcessor Objects<a class="headerlink" href="#httpcookieprocessor-objects" title="Link to this heading">¶</a></h2>
  1025. <p><a class="reference internal" href="#urllib.request.HTTPCookieProcessor" title="urllib.request.HTTPCookieProcessor"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPCookieProcessor</span></code></a> instances have one attribute:</p>
  1026. <dl class="py attribute">
  1027. <dt class="sig sig-object py" id="urllib.request.HTTPCookieProcessor.cookiejar">
  1028. <span class="sig-prename descclassname"><span class="pre">HTTPCookieProcessor.</span></span><span class="sig-name descname"><span class="pre">cookiejar</span></span><a class="headerlink" href="#urllib.request.HTTPCookieProcessor.cookiejar" title="Link to this definition">¶</a></dt>
  1029. <dd><p>The <a class="reference internal" href="http.cookiejar.html#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.cookiejar.CookieJar</span></code></a> in which cookies are stored.</p>
  1030. </dd></dl>
  1031. </section>
  1032. <section id="proxyhandler-objects">
  1033. <span id="proxy-handler"></span><h2>ProxyHandler Objects<a class="headerlink" href="#proxyhandler-objects" title="Link to this heading">¶</a></h2>
  1034. <dl class="py method">
  1035. <dt class="sig sig-object py">
  1036. <span class="sig-name descname"><span class="pre">ProxyHandler.&lt;protocol&gt;_open(request)</span></span></dt>
  1037. <dd><p>The <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> will have a method <code class="xref py py-meth docutils literal notranslate"><span class="pre">&lt;protocol&gt;_open()</span></code> for every
  1038. <em>protocol</em> which has a proxy in the <em>proxies</em> dictionary given in the
  1039. constructor. The method will modify requests to go through the proxy, by
  1040. calling <code class="docutils literal notranslate"><span class="pre">request.set_proxy()</span></code>, and call the next handler in the chain to
  1041. actually execute the protocol.</p>
  1042. </dd></dl>
  1043. </section>
  1044. <section id="httppasswordmgr-objects">
  1045. <span id="http-password-mgr"></span><h2>HTTPPasswordMgr Objects<a class="headerlink" href="#httppasswordmgr-objects" title="Link to this heading">¶</a></h2>
  1046. <p>These methods are available on <a class="reference internal" href="#urllib.request.HTTPPasswordMgr" title="urllib.request.HTTPPasswordMgr"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgr</span></code></a> and
  1047. <a class="reference internal" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="urllib.request.HTTPPasswordMgrWithDefaultRealm"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></code></a> objects.</p>
  1048. <dl class="py method">
  1049. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgr.add_password">
  1050. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgr.</span></span><span class="sig-name descname"><span class="pre">add_password</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">realm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">user</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">passwd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgr.add_password" title="Link to this definition">¶</a></dt>
  1051. <dd><p><em>uri</em> can be either a single URI, or a sequence of URIs. <em>realm</em>, <em>user</em> and
  1052. <em>passwd</em> must be strings. This causes <code class="docutils literal notranslate"><span class="pre">(user,</span> <span class="pre">passwd)</span></code> to be used as
  1053. authentication tokens when authentication for <em>realm</em> and a super-URI of any of
  1054. the given URIs is given.</p>
  1055. </dd></dl>
  1056. <dl class="py method">
  1057. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgr.find_user_password">
  1058. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgr.</span></span><span class="sig-name descname"><span class="pre">find_user_password</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">realm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authuri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgr.find_user_password" title="Link to this definition">¶</a></dt>
  1059. <dd><p>Get user/password for given realm and URI, if any. This method will return
  1060. <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">None)</span></code> if there is no matching user/password.</p>
  1061. <p>For <a class="reference internal" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="urllib.request.HTTPPasswordMgrWithDefaultRealm"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></code></a> objects, the realm <code class="docutils literal notranslate"><span class="pre">None</span></code> will be
  1062. searched if the given <em>realm</em> has no matching user/password.</p>
  1063. </dd></dl>
  1064. </section>
  1065. <section id="httppasswordmgrwithpriorauth-objects">
  1066. <span id="http-password-mgr-with-prior-auth"></span><h2>HTTPPasswordMgrWithPriorAuth Objects<a class="headerlink" href="#httppasswordmgrwithpriorauth-objects" title="Link to this heading">¶</a></h2>
  1067. <p>This password manager extends <a class="reference internal" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="urllib.request.HTTPPasswordMgrWithDefaultRealm"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></code></a> to support
  1068. tracking URIs for which authentication credentials should always be sent.</p>
  1069. <dl class="py method">
  1070. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth.add_password">
  1071. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgrWithPriorAuth.</span></span><span class="sig-name descname"><span class="pre">add_password</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">realm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">user</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">passwd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">is_authenticated</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithPriorAuth.add_password" title="Link to this definition">¶</a></dt>
  1072. <dd><p><em>realm</em>, <em>uri</em>, <em>user</em>, <em>passwd</em> are as for
  1073. <a class="reference internal" href="#urllib.request.HTTPPasswordMgr.add_password" title="urllib.request.HTTPPasswordMgr.add_password"><code class="xref py py-meth docutils literal notranslate"><span class="pre">HTTPPasswordMgr.add_password()</span></code></a>. <em>is_authenticated</em> sets the initial
  1074. value of the <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> flag for the given URI or list of URIs.
  1075. If <em>is_authenticated</em> is specified as <code class="docutils literal notranslate"><span class="pre">True</span></code>, <em>realm</em> is ignored.</p>
  1076. </dd></dl>
  1077. <dl class="py method">
  1078. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth.find_user_password">
  1079. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgrWithPriorAuth.</span></span><span class="sig-name descname"><span class="pre">find_user_password</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">realm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authuri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithPriorAuth.find_user_password" title="Link to this definition">¶</a></dt>
  1080. <dd><p>Same as for <a class="reference internal" href="#urllib.request.HTTPPasswordMgrWithDefaultRealm" title="urllib.request.HTTPPasswordMgrWithDefaultRealm"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></code></a> objects</p>
  1081. </dd></dl>
  1082. <dl class="py method">
  1083. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth.update_authenticated">
  1084. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgrWithPriorAuth.</span></span><span class="sig-name descname"><span class="pre">update_authenticated</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">is_authenticated</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithPriorAuth.update_authenticated" title="Link to this definition">¶</a></dt>
  1085. <dd><p>Update the <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> flag for the given <em>uri</em> or list
  1086. of URIs.</p>
  1087. </dd></dl>
  1088. <dl class="py method">
  1089. <dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth.is_authenticated">
  1090. <span class="sig-prename descclassname"><span class="pre">HTTPPasswordMgrWithPriorAuth.</span></span><span class="sig-name descname"><span class="pre">is_authenticated</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authuri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPPasswordMgrWithPriorAuth.is_authenticated" title="Link to this definition">¶</a></dt>
  1091. <dd><p>Returns the current state of the <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> flag for
  1092. the given URI.</p>
  1093. </dd></dl>
  1094. </section>
  1095. <section id="abstractbasicauthhandler-objects">
  1096. <span id="abstract-basic-auth-handler"></span><h2>AbstractBasicAuthHandler Objects<a class="headerlink" href="#abstractbasicauthhandler-objects" title="Link to this heading">¶</a></h2>
  1097. <dl class="py method">
  1098. <dt class="sig sig-object py" id="urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed">
  1099. <span class="sig-prename descclassname"><span class="pre">AbstractBasicAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_auth_reqed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">authreq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed" title="Link to this definition">¶</a></dt>
  1100. <dd><p>Handle an authentication request by getting a user/password pair, and re-trying
  1101. the request. <em>authreq</em> should be the name of the header where the information
  1102. about the realm is included in the request, <em>host</em> specifies the URL and path to
  1103. authenticate for, <em>req</em> should be the (failed) <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object, and
  1104. <em>headers</em> should be the error headers.</p>
  1105. <p><em>host</em> is either an authority (e.g. <code class="docutils literal notranslate"><span class="pre">&quot;python.org&quot;</span></code>) or a URL containing an
  1106. authority component (e.g. <code class="docutils literal notranslate"><span class="pre">&quot;http://python.org/&quot;</span></code>). In either case, the
  1107. authority must not contain a userinfo component (so, <code class="docutils literal notranslate"><span class="pre">&quot;python.org&quot;</span></code> and
  1108. <code class="docutils literal notranslate"><span class="pre">&quot;python.org:80&quot;</span></code> are fine, <code class="docutils literal notranslate"><span class="pre">&quot;joe:password&#64;python.org&quot;</span></code> is not).</p>
  1109. </dd></dl>
  1110. </section>
  1111. <section id="httpbasicauthhandler-objects">
  1112. <span id="http-basic-auth-handler"></span><h2>HTTPBasicAuthHandler Objects<a class="headerlink" href="#httpbasicauthhandler-objects" title="Link to this heading">¶</a></h2>
  1113. <dl class="py method">
  1114. <dt class="sig sig-object py" id="urllib.request.HTTPBasicAuthHandler.http_error_401">
  1115. <span class="sig-prename descclassname"><span class="pre">HTTPBasicAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_401</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPBasicAuthHandler.http_error_401" title="Link to this definition">¶</a></dt>
  1116. <dd><p>Retry the request with authentication information, if available.</p>
  1117. </dd></dl>
  1118. </section>
  1119. <section id="proxybasicauthhandler-objects">
  1120. <span id="proxy-basic-auth-handler"></span><h2>ProxyBasicAuthHandler Objects<a class="headerlink" href="#proxybasicauthhandler-objects" title="Link to this heading">¶</a></h2>
  1121. <dl class="py method">
  1122. <dt class="sig sig-object py" id="urllib.request.ProxyBasicAuthHandler.http_error_407">
  1123. <span class="sig-prename descclassname"><span class="pre">ProxyBasicAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_407</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.ProxyBasicAuthHandler.http_error_407" title="Link to this definition">¶</a></dt>
  1124. <dd><p>Retry the request with authentication information, if available.</p>
  1125. </dd></dl>
  1126. </section>
  1127. <section id="abstractdigestauthhandler-objects">
  1128. <span id="abstract-digest-auth-handler"></span><h2>AbstractDigestAuthHandler Objects<a class="headerlink" href="#abstractdigestauthhandler-objects" title="Link to this heading">¶</a></h2>
  1129. <dl class="py method">
  1130. <dt class="sig sig-object py" id="urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed">
  1131. <span class="sig-prename descclassname"><span class="pre">AbstractDigestAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_auth_reqed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">authreq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed" title="Link to this definition">¶</a></dt>
  1132. <dd><p><em>authreq</em> should be the name of the header where the information about the realm
  1133. is included in the request, <em>host</em> should be the host to authenticate to, <em>req</em>
  1134. should be the (failed) <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> object, and <em>headers</em> should be the
  1135. error headers.</p>
  1136. </dd></dl>
  1137. </section>
  1138. <section id="httpdigestauthhandler-objects">
  1139. <span id="http-digest-auth-handler"></span><h2>HTTPDigestAuthHandler Objects<a class="headerlink" href="#httpdigestauthhandler-objects" title="Link to this heading">¶</a></h2>
  1140. <dl class="py method">
  1141. <dt class="sig sig-object py" id="urllib.request.HTTPDigestAuthHandler.http_error_401">
  1142. <span class="sig-prename descclassname"><span class="pre">HTTPDigestAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_401</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPDigestAuthHandler.http_error_401" title="Link to this definition">¶</a></dt>
  1143. <dd><p>Retry the request with authentication information, if available.</p>
  1144. </dd></dl>
  1145. </section>
  1146. <section id="proxydigestauthhandler-objects">
  1147. <span id="proxy-digest-auth-handler"></span><h2>ProxyDigestAuthHandler Objects<a class="headerlink" href="#proxydigestauthhandler-objects" title="Link to this heading">¶</a></h2>
  1148. <dl class="py method">
  1149. <dt class="sig sig-object py" id="urllib.request.ProxyDigestAuthHandler.http_error_407">
  1150. <span class="sig-prename descclassname"><span class="pre">ProxyDigestAuthHandler.</span></span><span class="sig-name descname"><span class="pre">http_error_407</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hdrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.ProxyDigestAuthHandler.http_error_407" title="Link to this definition">¶</a></dt>
  1151. <dd><p>Retry the request with authentication information, if available.</p>
  1152. </dd></dl>
  1153. </section>
  1154. <section id="httphandler-objects">
  1155. <span id="http-handler-objects"></span><h2>HTTPHandler Objects<a class="headerlink" href="#httphandler-objects" title="Link to this heading">¶</a></h2>
  1156. <dl class="py method">
  1157. <dt class="sig sig-object py" id="urllib.request.HTTPHandler.http_open">
  1158. <span class="sig-prename descclassname"><span class="pre">HTTPHandler.</span></span><span class="sig-name descname"><span class="pre">http_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPHandler.http_open" title="Link to this definition">¶</a></dt>
  1159. <dd><p>Send an HTTP request, which can be either GET or POST, depending on
  1160. <code class="docutils literal notranslate"><span class="pre">req.has_data()</span></code>.</p>
  1161. </dd></dl>
  1162. </section>
  1163. <section id="httpshandler-objects">
  1164. <span id="https-handler-objects"></span><h2>HTTPSHandler Objects<a class="headerlink" href="#httpshandler-objects" title="Link to this heading">¶</a></h2>
  1165. <dl class="py method">
  1166. <dt class="sig sig-object py" id="urllib.request.HTTPSHandler.https_open">
  1167. <span class="sig-prename descclassname"><span class="pre">HTTPSHandler.</span></span><span class="sig-name descname"><span class="pre">https_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPSHandler.https_open" title="Link to this definition">¶</a></dt>
  1168. <dd><p>Send an HTTPS request, which can be either GET or POST, depending on
  1169. <code class="docutils literal notranslate"><span class="pre">req.has_data()</span></code>.</p>
  1170. </dd></dl>
  1171. </section>
  1172. <section id="filehandler-objects">
  1173. <span id="file-handler-objects"></span><h2>FileHandler Objects<a class="headerlink" href="#filehandler-objects" title="Link to this heading">¶</a></h2>
  1174. <dl class="py method">
  1175. <dt class="sig sig-object py" id="urllib.request.FileHandler.file_open">
  1176. <span class="sig-prename descclassname"><span class="pre">FileHandler.</span></span><span class="sig-name descname"><span class="pre">file_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.FileHandler.file_open" title="Link to this definition">¶</a></dt>
  1177. <dd><p>Open the file locally, if there is no host name, or the host name is
  1178. <code class="docutils literal notranslate"><span class="pre">'localhost'</span></code>.</p>
  1179. <div class="versionchanged">
  1180. <p><span class="versionmodified changed">Changed in version 3.2: </span>This method is applicable only for local hostnames. When a remote
  1181. hostname is given, an <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> is raised.</p>
  1182. </div>
  1183. </dd></dl>
  1184. </section>
  1185. <section id="datahandler-objects">
  1186. <span id="data-handler-objects"></span><h2>DataHandler Objects<a class="headerlink" href="#datahandler-objects" title="Link to this heading">¶</a></h2>
  1187. <dl class="py method">
  1188. <dt class="sig sig-object py" id="urllib.request.DataHandler.data_open">
  1189. <span class="sig-prename descclassname"><span class="pre">DataHandler.</span></span><span class="sig-name descname"><span class="pre">data_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.DataHandler.data_open" title="Link to this definition">¶</a></dt>
  1190. <dd><p>Read a data URL. This kind of URL contains the content encoded in the URL
  1191. itself. The data URL syntax is specified in <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2397.html"><strong>RFC 2397</strong></a>. This implementation
  1192. ignores white spaces in base64 encoded data URLs so the URL may be wrapped
  1193. in whatever source file it comes from. But even though some browsers don’t
  1194. mind about a missing padding at the end of a base64 encoded data URL, this
  1195. implementation will raise an <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> in that case.</p>
  1196. </dd></dl>
  1197. </section>
  1198. <section id="ftphandler-objects">
  1199. <span id="ftp-handler-objects"></span><h2>FTPHandler Objects<a class="headerlink" href="#ftphandler-objects" title="Link to this heading">¶</a></h2>
  1200. <dl class="py method">
  1201. <dt class="sig sig-object py" id="urllib.request.FTPHandler.ftp_open">
  1202. <span class="sig-prename descclassname"><span class="pre">FTPHandler.</span></span><span class="sig-name descname"><span class="pre">ftp_open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">req</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.FTPHandler.ftp_open" title="Link to this definition">¶</a></dt>
  1203. <dd><p>Open the FTP file indicated by <em>req</em>. The login is always done with empty
  1204. username and password.</p>
  1205. </dd></dl>
  1206. </section>
  1207. <section id="cacheftphandler-objects">
  1208. <span id="cacheftp-handler-objects"></span><h2>CacheFTPHandler Objects<a class="headerlink" href="#cacheftphandler-objects" title="Link to this heading">¶</a></h2>
  1209. <p><a class="reference internal" href="#urllib.request.CacheFTPHandler" title="urllib.request.CacheFTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">CacheFTPHandler</span></code></a> objects are <a class="reference internal" href="#urllib.request.FTPHandler" title="urllib.request.FTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTPHandler</span></code></a> objects with the
  1210. following additional methods:</p>
  1211. <dl class="py method">
  1212. <dt class="sig sig-object py" id="urllib.request.CacheFTPHandler.setTimeout">
  1213. <span class="sig-prename descclassname"><span class="pre">CacheFTPHandler.</span></span><span class="sig-name descname"><span class="pre">setTimeout</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.CacheFTPHandler.setTimeout" title="Link to this definition">¶</a></dt>
  1214. <dd><p>Set timeout of connections to <em>t</em> seconds.</p>
  1215. </dd></dl>
  1216. <dl class="py method">
  1217. <dt class="sig sig-object py" id="urllib.request.CacheFTPHandler.setMaxConns">
  1218. <span class="sig-prename descclassname"><span class="pre">CacheFTPHandler.</span></span><span class="sig-name descname"><span class="pre">setMaxConns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.CacheFTPHandler.setMaxConns" title="Link to this definition">¶</a></dt>
  1219. <dd><p>Set maximum number of cached connections to <em>m</em>.</p>
  1220. </dd></dl>
  1221. </section>
  1222. <section id="unknownhandler-objects">
  1223. <span id="unknown-handler-objects"></span><h2>UnknownHandler Objects<a class="headerlink" href="#unknownhandler-objects" title="Link to this heading">¶</a></h2>
  1224. <dl class="py method">
  1225. <dt class="sig sig-object py" id="urllib.request.UnknownHandler.unknown_open">
  1226. <span class="sig-prename descclassname"><span class="pre">UnknownHandler.</span></span><span class="sig-name descname"><span class="pre">unknown_open</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.UnknownHandler.unknown_open" title="Link to this definition">¶</a></dt>
  1227. <dd><p>Raise a <a class="reference internal" href="urllib.error.html#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> exception.</p>
  1228. </dd></dl>
  1229. </section>
  1230. <section id="httperrorprocessor-objects">
  1231. <span id="http-error-processor-objects"></span><h2>HTTPErrorProcessor Objects<a class="headerlink" href="#httperrorprocessor-objects" title="Link to this heading">¶</a></h2>
  1232. <dl class="py method">
  1233. <dt class="sig sig-object py" id="urllib.request.HTTPErrorProcessor.http_response">
  1234. <span class="sig-prename descclassname"><span class="pre">HTTPErrorProcessor.</span></span><span class="sig-name descname"><span class="pre">http_response</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">response</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPErrorProcessor.http_response" title="Link to this definition">¶</a></dt>
  1235. <dd><p>Process HTTP error responses.</p>
  1236. <p>For 200 error codes, the response object is returned immediately.</p>
  1237. <p>For non-200 error codes, this simply passes the job on to the
  1238. <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_&lt;type&gt;()</span></code> handler methods, via <a class="reference internal" href="#urllib.request.OpenerDirector.error" title="urllib.request.OpenerDirector.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OpenerDirector.error()</span></code></a>.
  1239. Eventually, <a class="reference internal" href="#urllib.request.HTTPDefaultErrorHandler" title="urllib.request.HTTPDefaultErrorHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPDefaultErrorHandler</span></code></a> will raise an
  1240. <a class="reference internal" href="urllib.error.html#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> if no other handler handles the error.</p>
  1241. </dd></dl>
  1242. <dl class="py method">
  1243. <dt class="sig sig-object py" id="urllib.request.HTTPErrorProcessor.https_response">
  1244. <span class="sig-prename descclassname"><span class="pre">HTTPErrorProcessor.</span></span><span class="sig-name descname"><span class="pre">https_response</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">response</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.HTTPErrorProcessor.https_response" title="Link to this definition">¶</a></dt>
  1245. <dd><p>Process HTTPS error responses.</p>
  1246. <p>The behavior is same as <a class="reference internal" href="#urllib.request.HTTPErrorProcessor.http_response" title="urllib.request.HTTPErrorProcessor.http_response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_response()</span></code></a>.</p>
  1247. </dd></dl>
  1248. </section>
  1249. <section id="examples">
  1250. <span id="urllib-request-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  1251. <p>In addition to the examples below, more examples are given in
  1252. <a class="reference internal" href="../howto/urllib2.html#urllib-howto"><span class="std std-ref">HOWTO Fetch Internet Resources Using The urllib Package</span></a>.</p>
  1253. <p>This example gets the python.org main page and displays the first 300 bytes of
  1254. it.</p>
  1255. <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">urllib.request</span>
  1256. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;http://www.python.org/&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1257. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">300</span><span class="p">))</span>
  1258. <span class="gp">...</span>
  1259. <span class="go">b&#39;&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
  1260. <span class="go">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;\n\n\n&lt;html</span>
  1261. <span class="go">xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;\n\n&lt;head&gt;\n</span>
  1262. <span class="go">&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;\n</span>
  1263. <span class="go">&lt;title&gt;Python Programming &#39;</span>
  1264. </pre></div>
  1265. </div>
  1266. <p>Note that urlopen returns a bytes object. This is because there is no way
  1267. for urlopen to automatically determine the encoding of the byte stream
  1268. it receives from the HTTP server. In general, a program will decode
  1269. the returned bytes object to string once it determines or guesses
  1270. the appropriate encoding.</p>
  1271. <p>The following W3C document, <a class="reference external" href="https://www.w3.org/International/O-charset">https://www.w3.org/International/O-charset</a>, lists
  1272. the various ways in which an (X)HTML or an XML document could have specified its
  1273. encoding information.</p>
  1274. <p>As the python.org website uses <em>utf-8</em> encoding as specified in its meta tag, we
  1275. will use the same for decoding the bytes object.</p>
  1276. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;http://www.python.org/&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1277. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  1278. <span class="gp">...</span>
  1279. <span class="go">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
  1280. <span class="go">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtm</span>
  1281. </pre></div>
  1282. </div>
  1283. <p>It is also possible to achieve the same result without using the
  1284. <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> approach.</p>
  1285. <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">urllib.request</span>
  1286. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;http://www.python.org/&#39;</span><span class="p">)</span>
  1287. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  1288. <span class="go">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
  1289. <span class="go">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtm</span>
  1290. </pre></div>
  1291. </div>
  1292. <p>In the following example, we are sending a data-stream to the stdin of a CGI
  1293. and reading the data it returns to us. Note that this example will only work
  1294. when the Python installation supports SSL.</p>
  1295. <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">urllib.request</span>
  1296. <span class="gp">&gt;&gt;&gt; </span><span class="n">req</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s1">&#39;https://localhost/cgi-bin/test.cgi&#39;</span><span class="p">,</span>
  1297. <span class="gp">... </span> <span class="n">data</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;This data is passed to stdin of the CGI&#39;</span><span class="p">)</span>
  1298. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1299. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  1300. <span class="gp">...</span>
  1301. <span class="go">Got Data: &quot;This data is passed to stdin of the CGI&quot;</span>
  1302. </pre></div>
  1303. </div>
  1304. <p>The code for the sample CGI used in the above example is:</p>
  1305. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  1306. <span class="kn">import</span> <span class="nn">sys</span>
  1307. <span class="n">data</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  1308. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Content-type: text/plain</span><span class="se">\n\n</span><span class="s1">Got Data: &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">data</span><span class="p">)</span>
  1309. </pre></div>
  1310. </div>
  1311. <p>Here is an example of doing a <code class="docutils literal notranslate"><span class="pre">PUT</span></code> request using <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a>:</p>
  1312. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib.request</span>
  1313. <span class="n">DATA</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;some data&#39;</span>
  1314. <span class="n">req</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s1">&#39;http://localhost:8080&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">DATA</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;PUT&#39;</span><span class="p">)</span>
  1315. <span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1316. <span class="k">pass</span>
  1317. <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>
  1318. <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
  1319. </pre></div>
  1320. </div>
  1321. <p>Use of Basic HTTP Authentication:</p>
  1322. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib.request</span>
  1323. <span class="c1"># Create an OpenerDirector with support for Basic HTTP Authentication...</span>
  1324. <span class="n">auth_handler</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">HTTPBasicAuthHandler</span><span class="p">()</span>
  1325. <span class="n">auth_handler</span><span class="o">.</span><span class="n">add_password</span><span class="p">(</span><span class="n">realm</span><span class="o">=</span><span class="s1">&#39;PDQ Application&#39;</span><span class="p">,</span>
  1326. <span class="n">uri</span><span class="o">=</span><span class="s1">&#39;https://mahler:8092/site-updates.py&#39;</span><span class="p">,</span>
  1327. <span class="n">user</span><span class="o">=</span><span class="s1">&#39;klem&#39;</span><span class="p">,</span>
  1328. <span class="n">passwd</span><span class="o">=</span><span class="s1">&#39;kadidd!ehopper&#39;</span><span class="p">)</span>
  1329. <span class="n">opener</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">auth_handler</span><span class="p">)</span>
  1330. <span class="c1"># ...and install it globally so it can be used with urlopen.</span>
  1331. <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">install_opener</span><span class="p">(</span><span class="n">opener</span><span class="p">)</span>
  1332. <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/login.html&#39;</span><span class="p">)</span>
  1333. </pre></div>
  1334. </div>
  1335. <p><a class="reference internal" href="#urllib.request.build_opener" title="urllib.request.build_opener"><code class="xref py py-func docutils literal notranslate"><span class="pre">build_opener()</span></code></a> provides many handlers by default, including a
  1336. <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a>. By default, <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> uses the environment
  1337. variables named <code class="docutils literal notranslate"><span class="pre">&lt;scheme&gt;_proxy</span></code>, where <code class="docutils literal notranslate"><span class="pre">&lt;scheme&gt;</span></code> is the URL scheme
  1338. involved. For example, the <code class="xref std std-envvar docutils literal notranslate"><span class="pre">http_proxy</span></code> environment variable is read to
  1339. obtain the HTTP proxy’s URL.</p>
  1340. <p>This example replaces the default <a class="reference internal" href="#urllib.request.ProxyHandler" title="urllib.request.ProxyHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyHandler</span></code></a> with one that uses
  1341. programmatically supplied proxy URLs, and adds proxy authorization support with
  1342. <a class="reference internal" href="#urllib.request.ProxyBasicAuthHandler" title="urllib.request.ProxyBasicAuthHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyBasicAuthHandler</span></code></a>.</p>
  1343. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">proxy_handler</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">ProxyHandler</span><span class="p">({</span><span class="s1">&#39;http&#39;</span><span class="p">:</span> <span class="s1">&#39;http://www.example.com:3128/&#39;</span><span class="p">})</span>
  1344. <span class="n">proxy_auth_handler</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">ProxyBasicAuthHandler</span><span class="p">()</span>
  1345. <span class="n">proxy_auth_handler</span><span class="o">.</span><span class="n">add_password</span><span class="p">(</span><span class="s1">&#39;realm&#39;</span><span class="p">,</span> <span class="s1">&#39;host&#39;</span><span class="p">,</span> <span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">)</span>
  1346. <span class="n">opener</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">proxy_handler</span><span class="p">,</span> <span class="n">proxy_auth_handler</span><span class="p">)</span>
  1347. <span class="c1"># This time, rather than install the OpenerDirector, we use it directly:</span>
  1348. <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/login.html&#39;</span><span class="p">)</span>
  1349. </pre></div>
  1350. </div>
  1351. <p>Adding HTTP headers:</p>
  1352. <p>Use the <em>headers</em> argument to the <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> constructor, or:</p>
  1353. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib.request</span>
  1354. <span class="n">req</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/&#39;</span><span class="p">)</span>
  1355. <span class="n">req</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;Referer&#39;</span><span class="p">,</span> <span class="s1">&#39;http://www.python.org/&#39;</span><span class="p">)</span>
  1356. <span class="c1"># Customize the default User-Agent header value:</span>
  1357. <span class="n">req</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;User-Agent&#39;</span><span class="p">,</span> <span class="s1">&#39;urllib-example/0.1 (Contact: . . .)&#39;</span><span class="p">)</span>
  1358. <span class="n">r</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span>
  1359. </pre></div>
  1360. </div>
  1361. <p><a class="reference internal" href="#urllib.request.OpenerDirector" title="urllib.request.OpenerDirector"><code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code></a> automatically adds a <em class="mailheader">User-Agent</em> header to
  1362. every <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a>. To change this:</p>
  1363. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib.request</span>
  1364. <span class="n">opener</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">()</span>
  1365. <span class="n">opener</span><span class="o">.</span><span class="n">addheaders</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;User-agent&#39;</span><span class="p">,</span> <span class="s1">&#39;Mozilla/5.0&#39;</span><span class="p">)]</span>
  1366. <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/&#39;</span><span class="p">)</span>
  1367. </pre></div>
  1368. </div>
  1369. <p>Also, remember that a few standard headers (<em class="mailheader">Content-Length</em>,
  1370. <em class="mailheader">Content-Type</em> and <em class="mailheader">Host</em>)
  1371. are added when the <a class="reference internal" href="#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> is passed to <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> (or
  1372. <a class="reference internal" href="#urllib.request.OpenerDirector.open" title="urllib.request.OpenerDirector.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OpenerDirector.open()</span></code></a>).</p>
  1373. <p id="urllib-examples">Here is an example session that uses the <code class="docutils literal notranslate"><span class="pre">GET</span></code> method to retrieve a URL
  1374. containing parameters:</p>
  1375. <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">urllib.request</span>
  1376. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
  1377. <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;spam&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;bacon&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">})</span>
  1378. <span class="gp">&gt;&gt;&gt; </span><span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://www.musi-cal.com/cgi-bin/query?</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">params</span>
  1379. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1380. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  1381. <span class="gp">...</span>
  1382. </pre></div>
  1383. </div>
  1384. <p>The following example uses the <code class="docutils literal notranslate"><span class="pre">POST</span></code> method instead. Note that params output
  1385. from urlencode is encoded to bytes before it is sent to urlopen as data:</p>
  1386. <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">urllib.request</span>
  1387. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
  1388. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</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;spam&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;bacon&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">})</span>
  1389. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
  1390. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s2">&quot;http://requestb.in/xrbl82xr&quot;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1391. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  1392. <span class="gp">...</span>
  1393. </pre></div>
  1394. </div>
  1395. <p>The following example uses an explicitly specified HTTP proxy, overriding
  1396. environment settings:</p>
  1397. <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">urllib.request</span>
  1398. <span class="gp">&gt;&gt;&gt; </span><span class="n">proxies</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;http&#39;</span><span class="p">:</span> <span class="s1">&#39;http://proxy.example.com:8080/&#39;</span><span class="p">}</span>
  1399. <span class="gp">&gt;&gt;&gt; </span><span class="n">opener</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">FancyURLopener</span><span class="p">(</span><span class="n">proxies</span><span class="p">)</span>
  1400. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;http://www.python.org&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1401. <span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
  1402. <span class="gp">...</span>
  1403. </pre></div>
  1404. </div>
  1405. <p>The following example uses no proxies at all, overriding environment settings:</p>
  1406. <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">urllib.request</span>
  1407. <span class="gp">&gt;&gt;&gt; </span><span class="n">opener</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">FancyURLopener</span><span class="p">({})</span>
  1408. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;http://www.python.org/&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1409. <span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
  1410. <span class="gp">...</span>
  1411. </pre></div>
  1412. </div>
  1413. </section>
  1414. <section id="legacy-interface">
  1415. <h2>Legacy interface<a class="headerlink" href="#legacy-interface" title="Link to this heading">¶</a></h2>
  1416. <p>The following functions and classes are ported from the Python 2 module
  1417. <code class="docutils literal notranslate"><span class="pre">urllib</span></code> (as opposed to <code class="docutils literal notranslate"><span class="pre">urllib2</span></code>). They might become deprecated at
  1418. some point in the future.</p>
  1419. <dl class="py function">
  1420. <dt class="sig sig-object py" id="urllib.request.urlretrieve">
  1421. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">urlretrieve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reporthook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.urlretrieve" title="Link to this definition">¶</a></dt>
  1422. <dd><p>Copy a network object denoted by a URL to a local file. If the URL
  1423. points to a local file, the object will not be copied unless filename is supplied.
  1424. Return a tuple <code class="docutils literal notranslate"><span class="pre">(filename,</span> <span class="pre">headers)</span></code> where <em>filename</em> is the
  1425. local file name under which the object can be found, and <em>headers</em> is whatever
  1426. the <code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code> method of the object returned by <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> returned (for
  1427. a remote object). Exceptions are the same as for <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>.</p>
  1428. <p>The second argument, if present, specifies the file location to copy to (if
  1429. absent, the location will be a tempfile with a generated name). The third
  1430. argument, if present, is a callable that will be called once on
  1431. establishment of the network connection and once after each block read
  1432. thereafter. The callable will be passed three arguments; a count of blocks
  1433. transferred so far, a block size in bytes, and the total size of the file. The
  1434. third argument may be <code class="docutils literal notranslate"><span class="pre">-1</span></code> on older FTP servers which do not return a file
  1435. size in response to a retrieval request.</p>
  1436. <p>The following example illustrates the most common usage scenario:</p>
  1437. <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">urllib.request</span>
  1438. <span class="gp">&gt;&gt;&gt; </span><span class="n">local_filename</span><span class="p">,</span> <span class="n">headers</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="s1">&#39;http://python.org/&#39;</span><span class="p">)</span>
  1439. <span class="gp">&gt;&gt;&gt; </span><span class="n">html</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">local_filename</span><span class="p">)</span>
  1440. <span class="gp">&gt;&gt;&gt; </span><span class="n">html</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1441. </pre></div>
  1442. </div>
  1443. <p>If the <em>url</em> uses the <code class="file docutils literal notranslate"><span class="pre">http:</span></code> scheme identifier, the optional <em>data</em>
  1444. argument may be given to specify a <code class="docutils literal notranslate"><span class="pre">POST</span></code> request (normally the request
  1445. type is <code class="docutils literal notranslate"><span class="pre">GET</span></code>). The <em>data</em> argument must be a bytes object in standard
  1446. <em class="mimetype">application/x-www-form-urlencoded</em> format; see the
  1447. <a class="reference internal" href="urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.urlencode()</span></code></a> function.</p>
  1448. <p><a class="reference internal" href="#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a> will raise <a class="reference internal" href="urllib.error.html#urllib.error.ContentTooShortError" title="urllib.error.ContentTooShortError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ContentTooShortError</span></code></a> when it detects that
  1449. the amount of data available was less than the expected amount (which is the
  1450. size reported by a <em>Content-Length</em> header). This can occur, for example, when
  1451. the download is interrupted.</p>
  1452. <p>The <em>Content-Length</em> is treated as a lower bound: if there’s more data to read,
  1453. urlretrieve reads more data, but if less data is available, it raises the
  1454. exception.</p>
  1455. <p>You can still retrieve the downloaded data in this case, it is stored in the
  1456. <code class="xref py py-attr docutils literal notranslate"><span class="pre">content</span></code> attribute of the exception instance.</p>
  1457. <p>If no <em>Content-Length</em> header was supplied, urlretrieve can not check the size
  1458. of the data it has downloaded, and just returns it. In this case you just have
  1459. to assume that the download was successful.</p>
  1460. </dd></dl>
  1461. <dl class="py function">
  1462. <dt class="sig sig-object py" id="urllib.request.urlcleanup">
  1463. <span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">urlcleanup</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.urlcleanup" title="Link to this definition">¶</a></dt>
  1464. <dd><p>Cleans up temporary files that may have been left behind by previous
  1465. calls to <a class="reference internal" href="#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a>.</p>
  1466. </dd></dl>
  1467. <dl class="py class">
  1468. <dt class="sig sig-object py" id="urllib.request.URLopener">
  1469. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">URLopener</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">proxies</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">x509</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.URLopener" title="Link to this definition">¶</a></dt>
  1470. <dd><div class="deprecated">
  1471. <p><span class="versionmodified deprecated">Deprecated since version 3.3.</span></p>
  1472. </div>
  1473. <p>Base class for opening and reading URLs. Unless you need to support opening
  1474. objects using schemes other than <code class="file docutils literal notranslate"><span class="pre">http:</span></code>, <code class="file docutils literal notranslate"><span class="pre">ftp:</span></code>, or <code class="file docutils literal notranslate"><span class="pre">file:</span></code>,
  1475. you probably want to use <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a>.</p>
  1476. <p>By default, the <a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a> class sends a <em class="mailheader">User-Agent</em> header
  1477. of <code class="docutils literal notranslate"><span class="pre">urllib/VVV</span></code>, where <em>VVV</em> is the <a class="reference internal" href="urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> version number.
  1478. Applications can define their own <em class="mailheader">User-Agent</em> header by subclassing
  1479. <a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a> or <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a> and setting the class attribute
  1480. <a class="reference internal" href="#urllib.request.URLopener.version" title="urllib.request.URLopener.version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">version</span></code></a> to an appropriate string value in the subclass definition.</p>
  1481. <p>The optional <em>proxies</em> parameter should be a dictionary mapping scheme names to
  1482. proxy URLs, where an empty dictionary turns proxies off completely. Its default
  1483. value is <code class="docutils literal notranslate"><span class="pre">None</span></code>, in which case environmental proxy settings will be used if
  1484. present, as discussed in the definition of <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>, above.</p>
  1485. <p>Additional keyword parameters, collected in <em>x509</em>, may be used for
  1486. authentication of the client when using the <code class="file docutils literal notranslate"><span class="pre">https:</span></code> scheme. The keywords
  1487. <em>key_file</em> and <em>cert_file</em> are supported to provide an SSL key and certificate;
  1488. both are needed to support client authentication.</p>
  1489. <p><a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a> objects will raise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exception if the server
  1490. returns an error code.</p>
  1491. <dl class="py method">
  1492. <dt class="sig sig-object py" id="urllib.request.URLopener.open">
  1493. <span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullurl</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.URLopener.open" title="Link to this definition">¶</a></dt>
  1494. <dd><p>Open <em>fullurl</em> using the appropriate protocol. This method sets up cache and
  1495. proxy information, then calls the appropriate open method with its input
  1496. arguments. If the scheme is not recognized, <a class="reference internal" href="#urllib.request.URLopener.open_unknown" title="urllib.request.URLopener.open_unknown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open_unknown()</span></code></a> is called.
  1497. The <em>data</em> argument has the same meaning as the <em>data</em> argument of
  1498. <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a>.</p>
  1499. <p>This method always quotes <em>fullurl</em> using <a class="reference internal" href="urllib.parse.html#urllib.parse.quote" title="urllib.parse.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a>.</p>
  1500. </dd></dl>
  1501. <dl class="py method">
  1502. <dt class="sig sig-object py" id="urllib.request.URLopener.open_unknown">
  1503. <span class="sig-name descname"><span class="pre">open_unknown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullurl</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.URLopener.open_unknown" title="Link to this definition">¶</a></dt>
  1504. <dd><p>Overridable interface to open unknown URL types.</p>
  1505. </dd></dl>
  1506. <dl class="py method">
  1507. <dt class="sig sig-object py" id="urllib.request.URLopener.retrieve">
  1508. <span class="sig-name descname"><span class="pre">retrieve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reporthook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.URLopener.retrieve" title="Link to this definition">¶</a></dt>
  1509. <dd><p>Retrieves the contents of <em>url</em> and places it in <em>filename</em>. The return value
  1510. is a tuple consisting of a local filename and either an
  1511. <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> object containing the response headers (for remote
  1512. URLs) or <code class="docutils literal notranslate"><span class="pre">None</span></code> (for local URLs). The caller must then open and read the
  1513. contents of <em>filename</em>. If <em>filename</em> is not given and the URL refers to a
  1514. local file, the input filename is returned. If the URL is non-local and
  1515. <em>filename</em> is not given, the filename is the output of <a class="reference internal" href="tempfile.html#tempfile.mktemp" title="tempfile.mktemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempfile.mktemp()</span></code></a>
  1516. with a suffix that matches the suffix of the last path component of the input
  1517. URL. If <em>reporthook</em> is given, it must be a function accepting three numeric
  1518. parameters: A chunk number, the maximum size chunks are read in and the total size of the download
  1519. (-1 if unknown). It will be called once at the start and after each chunk of data is read from the
  1520. network. <em>reporthook</em> is ignored for local URLs.</p>
  1521. <p>If the <em>url</em> uses the <code class="file docutils literal notranslate"><span class="pre">http:</span></code> scheme identifier, the optional <em>data</em>
  1522. argument may be given to specify a <code class="docutils literal notranslate"><span class="pre">POST</span></code> request (normally the request type
  1523. is <code class="docutils literal notranslate"><span class="pre">GET</span></code>). The <em>data</em> argument must in standard
  1524. <em class="mimetype">application/x-www-form-urlencoded</em> format; see the
  1525. <a class="reference internal" href="urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.urlencode()</span></code></a> function.</p>
  1526. </dd></dl>
  1527. <dl class="py attribute">
  1528. <dt class="sig sig-object py" id="urllib.request.URLopener.version">
  1529. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#urllib.request.URLopener.version" title="Link to this definition">¶</a></dt>
  1530. <dd><p>Variable that specifies the user agent of the opener object. To get
  1531. <a class="reference internal" href="urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> to tell servers that it is a particular user agent, set this in a
  1532. subclass as a class variable or in the constructor before calling the base
  1533. constructor.</p>
  1534. </dd></dl>
  1535. </dd></dl>
  1536. <dl class="py class">
  1537. <dt class="sig sig-object py" id="urllib.request.FancyURLopener">
  1538. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.request.</span></span><span class="sig-name descname"><span class="pre">FancyURLopener</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.FancyURLopener" title="Link to this definition">¶</a></dt>
  1539. <dd><div class="deprecated">
  1540. <p><span class="versionmodified deprecated">Deprecated since version 3.3.</span></p>
  1541. </div>
  1542. <p><a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a> subclasses <a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a> providing default handling
  1543. for the following HTTP response codes: 301, 302, 303, 307 and 401. For the 30x
  1544. response codes listed above, the <em class="mailheader">Location</em> header is used to fetch
  1545. the actual URL. For 401 response codes (authentication required), basic HTTP
  1546. authentication is performed. For the 30x response codes, recursion is bounded
  1547. by the value of the <em>maxtries</em> attribute, which defaults to 10.</p>
  1548. <p>For all other response codes, the method <a class="reference internal" href="#urllib.request.BaseHandler.http_error_default" title="urllib.request.BaseHandler.http_error_default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_default()</span></code></a> is called
  1549. which you can override in subclasses to handle the error appropriately.</p>
  1550. <div class="admonition note">
  1551. <p class="admonition-title">Note</p>
  1552. <p>According to the letter of <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a>, 301 and 302 responses to POST requests
  1553. must not be automatically redirected without confirmation by the user. In
  1554. reality, browsers do allow automatic redirection of these responses, changing
  1555. the POST to a GET, and <a class="reference internal" href="urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> reproduces this behaviour.</p>
  1556. </div>
  1557. <p>The parameters to the constructor are the same as those for <a class="reference internal" href="#urllib.request.URLopener" title="urllib.request.URLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">URLopener</span></code></a>.</p>
  1558. <div class="admonition note">
  1559. <p class="admonition-title">Note</p>
  1560. <p>When performing basic authentication, a <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a> instance calls
  1561. its <a class="reference internal" href="#urllib.request.FancyURLopener.prompt_user_passwd" title="urllib.request.FancyURLopener.prompt_user_passwd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">prompt_user_passwd()</span></code></a> method. The default implementation asks the
  1562. users for the required information on the controlling terminal. A subclass may
  1563. override this method to support more appropriate behavior if needed.</p>
  1564. </div>
  1565. <p>The <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a> class offers one additional method that should be
  1566. overloaded to provide the appropriate behavior:</p>
  1567. <dl class="py method">
  1568. <dt class="sig sig-object py" id="urllib.request.FancyURLopener.prompt_user_passwd">
  1569. <span class="sig-name descname"><span class="pre">prompt_user_passwd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">realm</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.FancyURLopener.prompt_user_passwd" title="Link to this definition">¶</a></dt>
  1570. <dd><p>Return information needed to authenticate the user at the given host in the
  1571. specified security realm. The return value should be a tuple, <code class="docutils literal notranslate"><span class="pre">(user,</span>
  1572. <span class="pre">password)</span></code>, which can be used for basic authentication.</p>
  1573. <p>The implementation prompts for this information on the terminal; an application
  1574. should override this method to use an appropriate interaction model in the local
  1575. environment.</p>
  1576. </dd></dl>
  1577. </dd></dl>
  1578. </section>
  1579. <section id="urllib-request-restrictions">
  1580. <h2><a class="reference internal" href="#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> Restrictions<a class="headerlink" href="#urllib-request-restrictions" title="Link to this heading">¶</a></h2>
  1581. <ul id="index-9">
  1582. <li><p>Currently, only the following protocols are supported: HTTP (versions 0.9 and
  1583. 1.0), FTP, local files, and data URLs.</p>
  1584. <div class="versionchanged">
  1585. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for data URLs.</p>
  1586. </div>
  1587. </li>
  1588. <li><p>The caching feature of <a class="reference internal" href="#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a> has been disabled until someone
  1589. finds the time to hack proper processing of Expiration time headers.</p></li>
  1590. <li><p>There should be a function to query whether a particular URL is in the cache.</p></li>
  1591. <li><p>For backward compatibility, if a URL appears to point to a local file but the
  1592. file can’t be opened, the URL is re-interpreted using the FTP protocol. This
  1593. can sometimes cause confusing error messages.</p></li>
  1594. <li><p>The <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> and <a class="reference internal" href="#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a> functions can cause arbitrarily
  1595. long delays while waiting for a network connection to be set up. This means
  1596. that it is difficult to build an interactive web client using these functions
  1597. without using threads.</p>
  1598. </li>
  1599. <li id="index-10"><p>The data returned by <a class="reference internal" href="#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> or <a class="reference internal" href="#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a> is the raw data
  1600. returned by the server. This may be binary data (such as an image), plain text
  1601. or (for example) HTML. The HTTP protocol provides type information in the reply
  1602. header, which can be inspected by looking at the <em class="mailheader">Content-Type</em>
  1603. header. If the returned data is HTML, you can use the module
  1604. <a class="reference internal" href="html.parser.html#module-html.parser" title="html.parser: A simple parser that can handle HTML and XHTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html.parser</span></code></a> to parse it.</p>
  1605. </li>
  1606. <li id="index-11"><p>The code handling the FTP protocol cannot differentiate between a file and a
  1607. directory. This can lead to unexpected behavior when attempting to read a URL
  1608. that points to a file that is not accessible. If the URL ends in a <code class="docutils literal notranslate"><span class="pre">/</span></code>, it is
  1609. assumed to refer to a directory and will be handled accordingly. But if an
  1610. attempt to read a file leads to a 550 error (meaning the URL cannot be found or
  1611. is not accessible, often for permission reasons), then the path is treated as a
  1612. directory in order to handle the case when a directory is specified by a URL but
  1613. the trailing <code class="docutils literal notranslate"><span class="pre">/</span></code> has been left off. This can cause misleading results when
  1614. you try to fetch a file whose read permissions make it inaccessible; the FTP
  1615. code will try to read it, fail with a 550 error, and then perform a directory
  1616. listing for the unreadable file. If fine-grained control is needed, consider
  1617. using the <a class="reference internal" href="ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> module, subclassing <a class="reference internal" href="#urllib.request.FancyURLopener" title="urllib.request.FancyURLopener"><code class="xref py py-class docutils literal notranslate"><span class="pre">FancyURLopener</span></code></a>, or changing
  1618. <em>_urlopener</em> to meet your needs.</p></li>
  1619. </ul>
  1620. </section>
  1621. </section>
  1622. <section id="module-urllib.response">
  1623. <span id="urllib-response-response-classes-used-by-urllib"></span><h1><a class="reference internal" href="#module-urllib.response" title="urllib.response: Response classes used by urllib."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.response</span></code></a> — Response classes used by urllib<a class="headerlink" href="#module-urllib.response" title="Link to this heading">¶</a></h1>
  1624. <p>The <a class="reference internal" href="#module-urllib.response" title="urllib.response: Response classes used by urllib."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.response</span></code></a> module defines functions and classes which define a
  1625. minimal file-like interface, including <code class="docutils literal notranslate"><span class="pre">read()</span></code> and <code class="docutils literal notranslate"><span class="pre">readline()</span></code>.
  1626. Functions defined by this module are used internally by the <a class="reference internal" href="#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> module.
  1627. The typical response object is a <a class="reference internal" href="#urllib.response.addinfourl" title="urllib.response.addinfourl"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.response.addinfourl</span></code></a> instance:</p>
  1628. <dl class="py class">
  1629. <dt class="sig sig-object py" id="urllib.response.addinfourl">
  1630. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">urllib.response.</span></span><span class="sig-name descname"><span class="pre">addinfourl</span></span><a class="headerlink" href="#urllib.response.addinfourl" title="Link to this definition">¶</a></dt>
  1631. <dd><dl class="py attribute">
  1632. <dt class="sig sig-object py" id="urllib.response.addinfourl.url">
  1633. <span class="sig-name descname"><span class="pre">url</span></span><a class="headerlink" href="#urllib.response.addinfourl.url" title="Link to this definition">¶</a></dt>
  1634. <dd><p>URL of the resource retrieved, commonly used to determine if a redirect was followed.</p>
  1635. </dd></dl>
  1636. <dl class="py attribute">
  1637. <dt class="sig sig-object py" id="urllib.response.addinfourl.headers">
  1638. <span class="sig-name descname"><span class="pre">headers</span></span><a class="headerlink" href="#urllib.response.addinfourl.headers" title="Link to this definition">¶</a></dt>
  1639. <dd><p>Returns the headers of the response in the form of an <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> instance.</p>
  1640. </dd></dl>
  1641. <dl class="py attribute">
  1642. <dt class="sig sig-object py" id="urllib.response.addinfourl.status">
  1643. <span class="sig-name descname"><span class="pre">status</span></span><a class="headerlink" href="#urllib.response.addinfourl.status" title="Link to this definition">¶</a></dt>
  1644. <dd><div class="versionadded">
  1645. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1646. </div>
  1647. <p>Status code returned by server.</p>
  1648. </dd></dl>
  1649. <dl class="py method">
  1650. <dt class="sig sig-object py" id="urllib.response.addinfourl.geturl">
  1651. <span class="sig-name descname"><span class="pre">geturl</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.response.addinfourl.geturl" title="Link to this definition">¶</a></dt>
  1652. <dd><div class="deprecated">
  1653. <p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Deprecated in favor of <a class="reference internal" href="#urllib.response.addinfourl.url" title="urllib.response.addinfourl.url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code></a>.</p>
  1654. </div>
  1655. </dd></dl>
  1656. <dl class="py method">
  1657. <dt class="sig sig-object py" id="urllib.response.addinfourl.info">
  1658. <span class="sig-name descname"><span class="pre">info</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.response.addinfourl.info" title="Link to this definition">¶</a></dt>
  1659. <dd><div class="deprecated">
  1660. <p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Deprecated in favor of <a class="reference internal" href="#urllib.response.addinfourl.headers" title="urllib.response.addinfourl.headers"><code class="xref py py-attr docutils literal notranslate"><span class="pre">headers</span></code></a>.</p>
  1661. </div>
  1662. </dd></dl>
  1663. <dl class="py attribute">
  1664. <dt class="sig sig-object py" id="urllib.response.addinfourl.code">
  1665. <span class="sig-name descname"><span class="pre">code</span></span><a class="headerlink" href="#urllib.response.addinfourl.code" title="Link to this definition">¶</a></dt>
  1666. <dd><div class="deprecated">
  1667. <p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Deprecated in favor of <a class="reference internal" href="#urllib.response.addinfourl.status" title="urllib.response.addinfourl.status"><code class="xref py py-attr docutils literal notranslate"><span class="pre">status</span></code></a>.</p>
  1668. </div>
  1669. </dd></dl>
  1670. <dl class="py method">
  1671. <dt class="sig sig-object py" id="urllib.response.addinfourl.getcode">
  1672. <span class="sig-name descname"><span class="pre">getcode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#urllib.response.addinfourl.getcode" title="Link to this definition">¶</a></dt>
  1673. <dd><div class="deprecated">
  1674. <p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Deprecated in favor of <a class="reference internal" href="#urllib.response.addinfourl.status" title="urllib.response.addinfourl.status"><code class="xref py py-attr docutils literal notranslate"><span class="pre">status</span></code></a>.</p>
  1675. </div>
  1676. </dd></dl>
  1677. </dd></dl>
  1678. </section>
  1679. <div class="clearer"></div>
  1680. </div>
  1681. </div>
  1682. </div>
  1683. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1684. <div class="sphinxsidebarwrapper">
  1685. <div>
  1686. <h3><a href="../contents.html">Table of Contents</a></h3>
  1687. <ul>
  1688. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> — Extensible library for opening URLs</a><ul>
  1689. <li><a class="reference internal" href="#request-objects">Request Objects</a></li>
  1690. <li><a class="reference internal" href="#openerdirector-objects">OpenerDirector Objects</a></li>
  1691. <li><a class="reference internal" href="#basehandler-objects">BaseHandler Objects</a></li>
  1692. <li><a class="reference internal" href="#httpredirecthandler-objects">HTTPRedirectHandler Objects</a></li>
  1693. <li><a class="reference internal" href="#httpcookieprocessor-objects">HTTPCookieProcessor Objects</a></li>
  1694. <li><a class="reference internal" href="#proxyhandler-objects">ProxyHandler Objects</a></li>
  1695. <li><a class="reference internal" href="#httppasswordmgr-objects">HTTPPasswordMgr Objects</a></li>
  1696. <li><a class="reference internal" href="#httppasswordmgrwithpriorauth-objects">HTTPPasswordMgrWithPriorAuth Objects</a></li>
  1697. <li><a class="reference internal" href="#abstractbasicauthhandler-objects">AbstractBasicAuthHandler Objects</a></li>
  1698. <li><a class="reference internal" href="#httpbasicauthhandler-objects">HTTPBasicAuthHandler Objects</a></li>
  1699. <li><a class="reference internal" href="#proxybasicauthhandler-objects">ProxyBasicAuthHandler Objects</a></li>
  1700. <li><a class="reference internal" href="#abstractdigestauthhandler-objects">AbstractDigestAuthHandler Objects</a></li>
  1701. <li><a class="reference internal" href="#httpdigestauthhandler-objects">HTTPDigestAuthHandler Objects</a></li>
  1702. <li><a class="reference internal" href="#proxydigestauthhandler-objects">ProxyDigestAuthHandler Objects</a></li>
  1703. <li><a class="reference internal" href="#httphandler-objects">HTTPHandler Objects</a></li>
  1704. <li><a class="reference internal" href="#httpshandler-objects">HTTPSHandler Objects</a></li>
  1705. <li><a class="reference internal" href="#filehandler-objects">FileHandler Objects</a></li>
  1706. <li><a class="reference internal" href="#datahandler-objects">DataHandler Objects</a></li>
  1707. <li><a class="reference internal" href="#ftphandler-objects">FTPHandler Objects</a></li>
  1708. <li><a class="reference internal" href="#cacheftphandler-objects">CacheFTPHandler Objects</a></li>
  1709. <li><a class="reference internal" href="#unknownhandler-objects">UnknownHandler Objects</a></li>
  1710. <li><a class="reference internal" href="#httperrorprocessor-objects">HTTPErrorProcessor Objects</a></li>
  1711. <li><a class="reference internal" href="#examples">Examples</a></li>
  1712. <li><a class="reference internal" href="#legacy-interface">Legacy interface</a></li>
  1713. <li><a class="reference internal" href="#urllib-request-restrictions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> Restrictions</a></li>
  1714. </ul>
  1715. </li>
  1716. <li><a class="reference internal" href="#module-urllib.response"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.response</span></code> — Response classes used by urllib</a></li>
  1717. </ul>
  1718. </div>
  1719. <div>
  1720. <h4>Previous topic</h4>
  1721. <p class="topless"><a href="urllib.html"
  1722. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code> — URL handling modules</a></p>
  1723. </div>
  1724. <div>
  1725. <h4>Next topic</h4>
  1726. <p class="topless"><a href="urllib.parse.html"
  1727. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code> — Parse URLs into components</a></p>
  1728. </div>
  1729. <div role="note" aria-label="source link">
  1730. <h3>This Page</h3>
  1731. <ul class="this-page-menu">
  1732. <li><a href="../bugs.html">Report a Bug</a></li>
  1733. <li>
  1734. <a href="https://github.com/python/cpython/blob/main/Doc/library/urllib.request.rst"
  1735. rel="nofollow">Show Source
  1736. </a>
  1737. </li>
  1738. </ul>
  1739. </div>
  1740. </div>
  1741. <div id="sidebarbutton" title="Collapse sidebar">
  1742. <span>«</span>
  1743. </div>
  1744. </div>
  1745. <div class="clearer"></div>
  1746. </div>
  1747. <div class="related" role="navigation" aria-label="related navigation">
  1748. <h3>Navigation</h3>
  1749. <ul>
  1750. <li class="right" style="margin-right: 10px">
  1751. <a href="../genindex.html" title="General Index"
  1752. >index</a></li>
  1753. <li class="right" >
  1754. <a href="../py-modindex.html" title="Python Module Index"
  1755. >modules</a> |</li>
  1756. <li class="right" >
  1757. <a href="urllib.parse.html" title="urllib.parse — Parse URLs into components"
  1758. >next</a> |</li>
  1759. <li class="right" >
  1760. <a href="urllib.html" title="urllib — URL handling modules"
  1761. >previous</a> |</li>
  1762. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1763. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1764. <li class="switchers">
  1765. <div class="language_switcher_placeholder"></div>
  1766. <div class="version_switcher_placeholder"></div>
  1767. </li>
  1768. <li>
  1769. </li>
  1770. <li id="cpython-language-and-version">
  1771. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1772. </li>
  1773. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1774. <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> &#187;</li>
  1775. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> — Extensible library for opening URLs</a></li>
  1776. <li class="right">
  1777. <div class="inline-search" role="search">
  1778. <form class="inline-search" action="../search.html" method="get">
  1779. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1780. <input type="submit" value="Go" />
  1781. </form>
  1782. </div>
  1783. |
  1784. </li>
  1785. <li class="right">
  1786. <label class="theme-selector-label">
  1787. Theme
  1788. <select class="theme-selector" oninput="activateTheme(this.value)">
  1789. <option value="auto" selected>Auto</option>
  1790. <option value="light">Light</option>
  1791. <option value="dark">Dark</option>
  1792. </select>
  1793. </label> |</li>
  1794. </ul>
  1795. </div>
  1796. <div class="footer">
  1797. &copy;
  1798. <a href="../copyright.html">
  1799. Copyright
  1800. </a>
  1801. 2001-2024, Python Software Foundation.
  1802. <br />
  1803. This page is licensed under the Python Software Foundation License Version 2.
  1804. <br />
  1805. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1806. <br />
  1807. See <a href="/license.html">History and License</a> for more information.<br />
  1808. <br />
  1809. The Python Software Foundation is a non-profit corporation.
  1810. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1811. <br />
  1812. <br />
  1813. Last updated on Apr 09, 2024 (13:47 UTC).
  1814. <a href="/bugs.html">Found a bug</a>?
  1815. <br />
  1816. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1817. </div>
  1818. </body>
  1819. </html>
上海开阖软件有限公司 沪ICP备12045867号-1