parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="urllib.request — Extensible library for opening URLs" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/urllib.request.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_urllib.request_afe1bc11.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>urllib.request — Extensible library for opening URLs — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="urllib.parse — Parse URLs into components" href="urllib.parse.html" />43 <link rel="prev" title="urllib — URL handling modules" href="urllib.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/urllib.request.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#request-objects">Request Objects</a></li>109<li><a class="reference internal" href="#openerdirector-objects">OpenerDirector Objects</a></li>110<li><a class="reference internal" href="#basehandler-objects">BaseHandler Objects</a></li>111<li><a class="reference internal" href="#httpredirecthandler-objects">HTTPRedirectHandler Objects</a></li>112<li><a class="reference internal" href="#httpcookieprocessor-objects">HTTPCookieProcessor Objects</a></li>113<li><a class="reference internal" href="#proxyhandler-objects">ProxyHandler Objects</a></li>114<li><a class="reference internal" href="#httppasswordmgr-objects">HTTPPasswordMgr Objects</a></li>115<li><a class="reference internal" href="#httppasswordmgrwithpriorauth-objects">HTTPPasswordMgrWithPriorAuth Objects</a></li>116<li><a class="reference internal" href="#abstractbasicauthhandler-objects">AbstractBasicAuthHandler Objects</a></li>117<li><a class="reference internal" href="#httpbasicauthhandler-objects">HTTPBasicAuthHandler Objects</a></li>118<li><a class="reference internal" href="#proxybasicauthhandler-objects">ProxyBasicAuthHandler Objects</a></li>119<li><a class="reference internal" href="#abstractdigestauthhandler-objects">AbstractDigestAuthHandler Objects</a></li>120<li><a class="reference internal" href="#httpdigestauthhandler-objects">HTTPDigestAuthHandler Objects</a></li>121<li><a class="reference internal" href="#proxydigestauthhandler-objects">ProxyDigestAuthHandler Objects</a></li>122<li><a class="reference internal" href="#httphandler-objects">HTTPHandler Objects</a></li>123<li><a class="reference internal" href="#httpshandler-objects">HTTPSHandler Objects</a></li>124<li><a class="reference internal" href="#filehandler-objects">FileHandler Objects</a></li>125<li><a class="reference internal" href="#datahandler-objects">DataHandler Objects</a></li>126<li><a class="reference internal" href="#ftphandler-objects">FTPHandler Objects</a></li>127<li><a class="reference internal" href="#cacheftphandler-objects">CacheFTPHandler Objects</a></li>128<li><a class="reference internal" href="#unknownhandler-objects">UnknownHandler Objects</a></li>129<li><a class="reference internal" href="#httperrorprocessor-objects">HTTPErrorProcessor Objects</a></li>130<li><a class="reference internal" href="#examples">Examples</a></li>131<li><a class="reference internal" href="#legacy-interface">Legacy interface</a></li>132<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>133</ul>134</li>135<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>136</ul>137 138 </div>139 <div>140 <h4>Previous topic</h4>141 <p class="topless"><a href="urllib.html"142 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code> — URL handling modules</a></p>143 </div>144 <div>145 <h4>Next topic</h4>146 <p class="topless"><a href="urllib.parse.html"147 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>148 </div>149 <script>150 document.addEventListener('DOMContentLoaded', () => {151 const title = document.querySelector('meta[property="og:title"]').content;152 const elements = document.querySelectorAll('.improvepage');153 const pageurl = window.location.href.split('?')[0];154 elements.forEach(element => {155 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));156 url.searchParams.set('pagetitle', title);157 url.searchParams.set('pageurl', pageurl);158 url.searchParams.set('pagesource', "library/urllib.request.rst");159 element.href = url.toString();160 });161 });162 </script>163 <div role="note" aria-label="source link">164 <h3>This page</h3>165 <ul class="this-page-menu">166 <li><a href="../bugs.html">Report a bug</a></li>167 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>168 <li>169 <a href="https://github.com/python/cpython/blob/main/Doc/library/urllib.request.rst?plain=1"170 rel="nofollow">Show source171 </a>172 </li>173 174 </ul>175 </div>176 </nav>177 </div>178</div>179 180 181 <div class="related" role="navigation" aria-label="Related">182 <h3>Navigation</h3>183 <ul>184 <li class="right" style="margin-right: 10px">185 <a href="../genindex.html" title="General Index"186 accesskey="I">index</a></li>187 <li class="right" >188 <a href="../py-modindex.html" title="Python Module Index"189 >modules</a> |</li>190 <li class="right" >191 <a href="urllib.parse.html" title="urllib.parse — Parse URLs into components"192 accesskey="N">next</a> |</li>193 <li class="right" >194 <a href="urllib.html" title="urllib — URL handling modules"195 accesskey="P">previous</a> |</li>196 197 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>198 <li><a href="https://www.python.org/">Python</a> »</li>199 <li class="switchers">200 <div class="language_switcher_placeholder"></div>201 <div class="version_switcher_placeholder"></div>202 </li>203 <li>204 205 </li>206 <li id="cpython-language-and-version">207 <a href="../index.html">3.15.0a6 Documentation</a> »208 </li>209 210 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>211 <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> »</li>212 <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>213 <li class="right">214 215 216 <div class="inline-search" role="search">217 <form class="inline-search" action="../search.html" method="get">218 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">219 <input type="submit" value="Go">220 </form>221 </div>222 |223 </li>224 <li class="right">225<label class="theme-selector-label">226 Theme227 <select class="theme-selector" oninput="activateTheme(this.value)">228 <option value="auto" selected>Auto</option>229 <option value="light">Light</option>230 <option value="dark">Dark</option>231 </select>232</label> |</li>233 234 </ul>235 </div> 236 237 <div class="document">238 <div class="documentwrapper">239 <div class="bodywrapper">240 <div class="body" role="main">241 242 <section id="module-urllib.request">243<span id="urllib-request-extensible-library-for-opening-urls"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> — Extensible library for opening URLs<a class="headerlink" href="#module-urllib.request" title="Link to this heading">¶</a></h1>244<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/urllib/request.py">Lib/urllib/request.py</a></p>245<hr class="docutils" />246<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> module defines functions and classes which help in247opening URLs (mostly HTTP) in a complex world — basic and digest248authentication, redirections, cookies and more.</p>249<div class="admonition seealso">250<p class="admonition-title">See also</p>251<p>The <a class="reference external" href="https://requests.readthedocs.io/en/master/">Requests package</a>252is recommended for a higher-level HTTP client interface.</p>253</div>254<div class="admonition warning">255<p class="admonition-title">Warning</p>256<p>On macOS it is unsafe to use this module in programs using257<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 for258macOS uses a higher-level system API. Set the environment variable259<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 problem260(e.g. <code class="docutils literal notranslate"><span class="pre">os.environ["no_proxy"]</span> <span class="pre">=</span> <span class="pre">"*"</span></code>).</p>261</div>262<div class="availability docutils container">263<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p>264<p>This module does not work or is not available on WebAssembly. See265<a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>266</div>267<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code> module defines the following functions:</p>268<dl class="py function">269<dt class="sig sig-object py" id="urllib.request.urlopen">270<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">context=None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.request.urlopen" title="Link to this definition">¶</a></dt>271<dd><p>Open <em>url</em>, which can be either a string containing a valid, properly272encoded 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>273<p><em>data</em> must be an object specifying additional data to be sent to the274server, 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>275for details.</p>276<p>urllib.request module uses HTTP/1.1 and includes <code class="docutils literal notranslate"><span class="pre">Connection:close</span></code> header277in its HTTP requests.</p>278<p>The optional <em>timeout</em> parameter specifies a timeout in seconds for279blocking operations like the connection attempt (if not specified,280the global default timeout setting will be used). This actually281only works for HTTP, HTTPS and FTP connections.</p>282<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> instance283describing 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>284for more details.</p>285<p>This function always returns an object which can work as a286<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>.287See <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>288<p>For HTTP and HTTPS URLs, this function returns a289<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 addition290to the three new methods above, the msg attribute contains the291same 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>292attribute — the reason phrase returned by server — instead of293the response headers as it is specified in the documentation for294<code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code>.</p>295<p>For FTP, file, and data URLs, this function296returns 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>297<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>298<p>Note that <code class="docutils literal notranslate"><span class="pre">None</span></code> may be returned if no handler handles the request (though299the 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> uses300<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>301<p>In addition, if proxy settings are detected (for example, when a <code class="docutils literal notranslate"><span class="pre">*_proxy</span></code>302environment variable like <code class="xref std std-envvar docutils literal notranslate"><span class="pre">http_proxy</span></code> is set),303<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 are304handled through the proxy.</p>305<p>The legacy <code class="docutils literal notranslate"><span class="pre">urllib.urlopen</span></code> function from Python 2.6 and earlier has been306discontinued; <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 old307<code class="docutils literal notranslate"><span class="pre">urllib2.urlopen</span></code>. Proxy handling, which was done by passing a dictionary308parameter to <code class="docutils literal notranslate"><span class="pre">urllib.urlopen</span></code>, can be obtained by using309<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>310<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>311<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>,312<code class="docutils literal notranslate"><span class="pre">method</span></code> taken from the request object.</p>313</p>314<div class="versionchanged">315<p><span class="versionmodified changed">Changed in version 3.2: </span><em>cafile</em> and <em>capath</em> were added.</p>316<p>HTTPS virtual hosts are now supported if possible (that is, if317<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>318<p><em>data</em> can be an iterable object.</p>319</div>320<div class="versionchanged">321<p><span class="versionmodified changed">Changed in version 3.3: </span><em>cadefault</em> was added.</p>322</div>323<div class="versionchanged">324<p><span class="versionmodified changed">Changed in version 3.4.3: </span><em>context</em> was added.</p>325</div>326<div class="versionchanged">327<p><span class="versionmodified changed">Changed in version 3.10: </span>HTTPS connection now send an ALPN extension with protocol indicator328<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 set329ALPN 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>330</div>331<div class="versionchanged">332<p><span class="versionmodified changed">Changed in version 3.13: </span>Remove <em>cafile</em>, <em>capath</em> and <em>cadefault</em> parameters: use the <em>context</em>333parameter instead.</p>334</div>335</dd></dl>336 337<dl class="py function">338<dt class="sig sig-object py" id="urllib.request.install_opener">339<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>340<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.341Installing an opener is only necessary if you want urlopen to use that342opener; 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 of343<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 real344<code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code>, and any class with the appropriate interface will345work.</p>346</dd></dl>347 348<dl class="py function">349<dt class="sig sig-object py" id="urllib.request.build_opener">350<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>351<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 the352order 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>, or353subclasses of <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHandler</span></code> (in which case it must be possible to call354the constructor without any parameters). Instances of the following classes355will be in front of the <em>handler</em>s, unless the <em>handler</em>s contain them,356instances 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 proxy357settings 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>,358<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>,359<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>360<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> module361can 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>362<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>363attribute to modify its position in the handlers list.</p>364</dd></dl>365 366<dl class="py function">367<dt class="sig sig-object py" id="urllib.request.pathname2url">368<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>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">add_scheme</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.pathname2url" title="Link to this definition">¶</a></dt>369<dd><p>Convert the given local path to a <code class="docutils literal notranslate"><span class="pre">file:</span></code> URL. This function uses370<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 to encode the path.</p>371<p>If <em>add_scheme</em> is false (the default), the return value omits the372<code class="docutils literal notranslate"><span class="pre">file:</span></code> scheme prefix. Set <em>add_scheme</em> to true to return a complete URL.</p>373<p>This example shows the function being used on Windows:</p>374<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">urllib.request</span><span class="w"> </span><span class="kn">import</span> <span class="n">pathname2url</span>375<span class="gp">>>> </span><span class="n">path</span> <span class="o">=</span> <span class="s1">'C:</span><span class="se">\\</span><span class="s1">Program Files'</span>376<span class="gp">>>> </span><span class="n">pathname2url</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">add_scheme</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>377<span class="go">'file:///C:/Program%20Files'</span>378</pre></div>379</div>380<div class="versionchanged">381<p><span class="versionmodified changed">Changed in version 3.14: </span>Windows drive letters are no longer converted to uppercase, and <code class="docutils literal notranslate"><span class="pre">:</span></code>382characters not following a drive letter no longer cause an383<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 to be raised on Windows.</p>384</div>385<div class="versionchanged">386<p><span class="versionmodified changed">Changed in version 3.14: </span>Paths beginning with a slash are converted to URLs with authority387sections. For example, the path <code class="docutils literal notranslate"><span class="pre">/etc/hosts</span></code> is converted to388the URL <code class="docutils literal notranslate"><span class="pre">///etc/hosts</span></code>.</p>389</div>390<div class="versionchanged">391<p><span class="versionmodified changed">Changed in version 3.14: </span>The <em>add_scheme</em> parameter was added.</p>392</div>393</dd></dl>394 395<dl class="py function">396<dt class="sig sig-object py" id="urllib.request.url2pathname">397<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">url</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">require_scheme</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">resolve_host</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.url2pathname" title="Link to this definition">¶</a></dt>398<dd><p>Convert the given <code class="docutils literal notranslate"><span class="pre">file:</span></code> URL to a local path. This function uses399<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 the URL.</p>400<p>If <em>require_scheme</em> is false (the default), the given value should omit a401<code class="docutils literal notranslate"><span class="pre">file:</span></code> scheme prefix. If <em>require_scheme</em> is set to true, the given402value should include the prefix; 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> is raised403if it doesn’t.</p>404<p>The URL authority is discarded if it is empty, <code class="docutils literal notranslate"><span class="pre">localhost</span></code>, or the local405hostname. Otherwise, if <em>resolve_host</em> is set to true, the authority is406resolved using <a class="reference internal" href="socket.html#socket.gethostbyname" title="socket.gethostbyname"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.gethostbyname()</span></code></a> and discarded if it matches a407local IP address (as per <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc8089.html#section-3"><strong>RFC 8089 §3</strong></a>). If the408authority is still unhandled, then on Windows a UNC path is returned, and409on other platforms 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> is raised.</p>410<p>This example shows the function being used on Windows:</p>411<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">urllib.request</span><span class="w"> </span><span class="kn">import</span> <span class="n">url2pathname</span>412<span class="gp">>>> </span><span class="n">url</span> <span class="o">=</span> <span class="s1">'file:///C:/Program</span><span class="si">%20F</span><span class="s1">iles'</span>413<span class="gp">>>> </span><span class="n">url2pathname</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">require_scheme</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>414<span class="go">'C:\\Program Files'</span>415</pre></div>416</div>417<div class="versionchanged">418<p><span class="versionmodified changed">Changed in version 3.14: </span>Windows drive letters are no longer converted to uppercase, and <code class="docutils literal notranslate"><span class="pre">:</span></code>419characters not following a drive letter no longer cause an420<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 to be raised on Windows.</p>421</div>422<div class="versionchanged">423<p><span class="versionmodified changed">Changed in version 3.14: </span>The URL authority is discarded if it matches the local hostname.424Otherwise, if the authority isn’t empty or <code class="docutils literal notranslate"><span class="pre">localhost</span></code>, then on425Windows a UNC path is returned (as before), and on other platforms a426<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>427</div>428<div class="versionchanged">429<p><span class="versionmodified changed">Changed in version 3.14: </span>The URL query and fragment components are discarded if present.</p>430</div>431<div class="versionchanged">432<p><span class="versionmodified changed">Changed in version 3.14: </span>The <em>require_scheme</em> and <em>resolve_host</em> parameters were added.</p>433</div>434</dd></dl>435 436<dl class="py function">437<dt class="sig sig-object py" id="urllib.request.getproxies">438<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>439<dd><p>This helper function returns a dictionary of scheme to proxy server URL440mappings. It scans the environment for variables named <code class="docutils literal notranslate"><span class="pre"><scheme>_proxy</span></code>,441in a case insensitive approach, for all operating systems first, and when it442cannot find it, looks for proxy information from System443Configuration for macOS and Windows Systems Registry for Windows.444If both lowercase and uppercase environment variables exist (and disagree),445lowercase is preferred.</p>446<div class="admonition note">447<p class="admonition-title">Note</p>448<p>If the environment variable <code class="docutils literal notranslate"><span class="pre">REQUEST_METHOD</span></code> is set, which usually449indicates your script is running in a CGI environment, the environment450variable <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 is451because that variable can be injected by a client using the “Proxy:” HTTP452header. If you need to use an HTTP proxy in a CGI environment, either use453<code class="docutils literal notranslate"><span class="pre">ProxyHandler</span></code> explicitly, or make sure the variable name is in454lowercase (or at least the <code class="docutils literal notranslate"><span class="pre">_proxy</span></code> suffix).</p>455</div>456</dd></dl>457 458<p>The following classes are provided:</p>459<dl class="py class">460<dt class="sig sig-object py" id="urllib.request.Request">461<em class="property"><span class="k"><span class="pre">class</span></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>462<dd><p>This class is an abstraction of a URL request.</p>463<p><em>url</em> should be a string containing a valid, properly encoded URL.</p>464<p><em>data</em> must be an object specifying additional data to send to the465server, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no such data is needed. Currently HTTP466requests are the only ones that use <em>data</em>. The supported object467types include bytes, file-like objects, and iterables of bytes-like objects.468If 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 field469has 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 according470to the type of <em>data</em>. <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> will be used to send471bytes objects, while <code class="docutils literal notranslate"><span class="pre">Transfer-Encoding:</span> <span class="pre">chunked</span></code> as specified in472<span class="target" id="index-1"></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>473<p>For an HTTP POST request method, <em>data</em> should be a buffer in the474standard <em class="mimetype">application/x-www-form-urlencoded</em> format. The475<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 sequence476of 2-tuples and returns an ASCII string in this format. It should477be encoded to bytes before being used as the <em>data</em> parameter.</p>478<p><em>headers</em> should be a dictionary, and will be treated as if479<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.480This is often used to “spoof” the <code class="docutils literal notranslate"><span class="pre">User-Agent</span></code> header value, which is481used by a browser to identify itself – some HTTP servers only482allow requests coming from common browsers as opposed to scripts.483For example, Mozilla Firefox may identify itself as <code class="docutils literal notranslate"><span class="pre">"Mozilla/5.0</span>484<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"</span></code>, while485<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 is486<code class="docutils literal notranslate"><span class="pre">"Python-urllib/2.6"</span></code> (on Python 2.6).487All header keys are sent in camel case.</p>488<p>An appropriate <code class="docutils literal notranslate"><span class="pre">Content-Type</span></code> header should be included if the <em>data</em>489argument is present. If this header has not been provided and <em>data</em>490is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">Content-Type:</span> <span class="pre">application/x-www-form-urlencoded</span></code> will491be added as a default.</p>492<p>The next two arguments are only of interest for correct handling493of third-party HTTP cookies:</p>494<p><em>origin_req_host</em> should be the request-host of the origin495transaction, 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 to496<code class="docutils literal notranslate"><span class="pre">http.cookiejar.request_host(self)</span></code>. This is the host name or IP497address of the original request that was initiated by the user.498For example, if the request is for an image in an HTML document,499this should be the request-host of the request for the page500containing the image.</p>501<p><em>unverifiable</em> should indicate whether the request is unverifiable,502as defined by <span class="target" id="index-3"></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 unverifiable503request is one whose URL the user did not have the option to504approve. For example, if the request is for an image in an HTML505document, and the user had no option to approve the automatic506fetching of the image, this should be true.</p>507<p><em>method</em> should be a string that indicates the HTTP request method that508will be used (e.g. <code class="docutils literal notranslate"><span class="pre">'HEAD'</span></code>). If provided, its value is stored in the509<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>.510The 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.511Subclasses may indicate a different default method by setting the512<code class="xref py py-attr docutils literal notranslate"><span class="pre">method</span></code> attribute in the class itself.</p>513<div class="admonition note">514<p class="admonition-title">Note</p>515<p>The request will not work as expected if the data object is unable516to deliver its content more than once (e.g. a file or an iterable517that can produce the content only once) and the request is retried518for HTTP redirects or authentication. The <em>data</em> is sent to the519HTTP server right away after the headers. There is no support for520a 100-continue expectation in the library.</p>521</div>522<div class="versionchanged">523<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>524</div>525<div class="versionchanged">526<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>527</div>528<div class="versionchanged">529<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 been530provided and <em>data</em> is neither <code class="docutils literal notranslate"><span class="pre">None</span></code> nor a bytes object.531Fall back to use chunked transfer encoding instead.</p>532</div>533</dd></dl>534 535<dl class="py class">536<dt class="sig sig-object py" id="urllib.request.OpenerDirector">537<em class="property"><span class="k"><span class="pre">class</span></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>538<dd><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code> 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 chained539together. It manages the chaining of handlers, and recovery from errors.</p>540</dd></dl>541 542<dl class="py class">543<dt class="sig sig-object py" id="urllib.request.BaseHandler">544<em class="property"><span class="k"><span class="pre">class</span></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>545<dd><p>This is the base class for all registered handlers — and handles only the546simple mechanics of registration.</p>547</dd></dl>548 549<dl class="py class">550<dt class="sig sig-object py" id="urllib.request.HTTPDefaultErrorHandler">551<em class="property"><span class="k"><span class="pre">class</span></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>552<dd><p>A class which defines a default handler for HTTP error responses; all responses553are 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>554</dd></dl>555 556<dl class="py class">557<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler">558<em class="property"><span class="k"><span class="pre">class</span></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>559<dd><p>A class to handle redirections.</p>560</dd></dl>561 562<dl class="py class">563<dt class="sig sig-object py" id="urllib.request.HTTPCookieProcessor">564<em class="property"><span class="k"><span class="pre">class</span></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>565<dd><p>A class to handle HTTP Cookies.</p>566</dd></dl>567 568<dl class="py class">569<dt class="sig sig-object py" id="urllib.request.ProxyHandler">570<em class="property"><span class="k"><span class="pre">class</span></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>571<dd><p>Cause requests to go through a proxy. If <em>proxies</em> is given, it must be a572dictionary mapping protocol names to URLs of proxies. The default is to read573the list of proxies from the environment variables574<code class="docutils literal notranslate"><span class="pre"><protocol>_proxy</span></code>. If no proxy environment variables are set, then575in a Windows environment proxy settings are obtained from the registry’s576Internet Settings section, and in a macOS environment proxy information577is retrieved from the System Configuration Framework.</p>578<p>To disable autodetected proxy pass an empty dictionary.</p>579<p>The <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">no_proxy</span></code> environment variable can be used to specify hosts580which shouldn’t be reached via proxy; if set, it should be a comma-separated581list of hostname suffixes, optionally with <code class="docutils literal notranslate"><span class="pre">:port</span></code> appended, for example582<code class="docutils literal notranslate"><span class="pre">cern.ch,ncsa.uiuc.edu,some.host:8080</span></code>.</p>583<div class="admonition note">584<p class="admonition-title">Note</p>585<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;586see 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>587</div>588</dd></dl>589 590<dl class="py class">591<dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgr">592<em class="property"><span class="k"><span class="pre">class</span></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>593<dd><p>Keep a database of <code class="docutils literal notranslate"><span class="pre">(realm,</span> <span class="pre">uri)</span> <span class="pre">-></span> <span class="pre">(user,</span> <span class="pre">password)</span></code> mappings.</p>594</dd></dl>595 596<dl class="py class">597<dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithDefaultRealm">598<em class="property"><span class="k"><span class="pre">class</span></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>599<dd><p>Keep a database of <code class="docutils literal notranslate"><span class="pre">(realm,</span> <span class="pre">uri)</span> <span class="pre">-></span> <span class="pre">(user,</span> <span class="pre">password)</span></code> mappings. A realm of600<code class="docutils literal notranslate"><span class="pre">None</span></code> is considered a catch-all realm, which is searched if no other realm601fits.</p>602</dd></dl>603 604<dl class="py class">605<dt class="sig sig-object py" id="urllib.request.HTTPPasswordMgrWithPriorAuth">606<em class="property"><span class="k"><span class="pre">class</span></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>607<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 a608database of <code class="docutils literal notranslate"><span class="pre">uri</span> <span class="pre">-></span> <span class="pre">is_authenticated</span></code> mappings. Can be used by a609BasicAuth handler to determine when to send authentication credentials610immediately instead of waiting for a <code class="docutils literal notranslate"><span class="pre">401</span></code> response first.</p>611<div class="versionadded">612<p><span class="versionmodified added">Added in version 3.5.</span></p>613</div>614</dd></dl>615 616<dl class="py class">617<dt class="sig sig-object py" id="urllib.request.AbstractBasicAuthHandler">618<em class="property"><span class="k"><span class="pre">class</span></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>619<dd><p>This is a mixin class that helps with HTTP authentication, both to the remote620host and to a proxy. <em>password_mgr</em>, if given, should be something that is621compatible 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 section622<a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be623supported. If <em>passwd_mgr</em> also provides <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> and624<code class="docutils literal notranslate"><span class="pre">update_authenticated</span></code> methods (see625<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 the626<code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> result for a given URI to determine whether or not to627send authentication credentials with the request. If <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code>628returns <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>629is <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 is630received the request is re-sent with the authentication credentials. If631authentication succeeds, <code class="docutils literal notranslate"><span class="pre">update_authenticated</span></code> is called to set632<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 to633the URI or any of its super-URIs will automatically include the634authentication credentials.</p>635<div class="versionadded">636<p><span class="versionmodified added">Added in version 3.5: </span>Added <code class="docutils literal notranslate"><span class="pre">is_authenticated</span></code> support.</p>637</div>638</dd></dl>639 640<dl class="py class">641<dt class="sig sig-object py" id="urllib.request.HTTPBasicAuthHandler">642<em class="property"><span class="k"><span class="pre">class</span></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>643<dd><p>Handle authentication with the remote host. <em>password_mgr</em>, if given, should644be 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 to645section <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must646be 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> when647presented with a wrong Authentication scheme.</p>648</dd></dl>649 650<dl class="py class">651<dt class="sig sig-object py" id="urllib.request.ProxyBasicAuthHandler">652<em class="property"><span class="k"><span class="pre">class</span></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>653<dd><p>Handle authentication with the proxy. <em>password_mgr</em>, if given, should be654something 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 section655<a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be656supported.</p>657</dd></dl>658 659<dl class="py class">660<dt class="sig sig-object py" id="urllib.request.AbstractDigestAuthHandler">661<em class="property"><span class="k"><span class="pre">class</span></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>662<dd><p>This is a mixin class that helps with HTTP authentication, both to the remote663host and to a proxy. <em>password_mgr</em>, if given, should be something that is664compatible 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 section665<a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be666supported.</p>667<div class="versionchanged">668<p><span class="versionmodified changed">Changed in version 3.14: </span>Added support for HTTP digest authentication algorithm <code class="docutils literal notranslate"><span class="pre">SHA-256</span></code>.</p>669</div>670</dd></dl>671 672<dl class="py class">673<dt class="sig sig-object py" id="urllib.request.HTTPDigestAuthHandler">674<em class="property"><span class="k"><span class="pre">class</span></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>675<dd><p>Handle authentication with the remote host. <em>password_mgr</em>, if given, should676be 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 to677section <a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must678be supported. When both Digest Authentication Handler and Basic679Authentication Handler are both added, Digest Authentication is always tried680first. If the Digest Authentication returns a 40x response again, it is sent681to Basic Authentication handler to Handle. This Handler method will raise a682<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 than683Digest or Basic.</p>684<div class="versionchanged">685<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>686</div>687</dd></dl>688 689<dl class="py class">690<dt class="sig sig-object py" id="urllib.request.ProxyDigestAuthHandler">691<em class="property"><span class="k"><span class="pre">class</span></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>692<dd><p>Handle authentication with the proxy. <em>password_mgr</em>, if given, should be693something 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 section694<a class="reference internal" href="#http-password-mgr"><span class="std std-ref">HTTPPasswordMgr Objects</span></a> for information on the interface that must be695supported.</p>696</dd></dl>697 698<dl class="py class">699<dt class="sig sig-object py" id="urllib.request.HTTPHandler">700<em class="property"><span class="k"><span class="pre">class</span></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>701<dd><p>A class to handle opening of HTTP URLs.</p>702</dd></dl>703 704<dl class="py class">705<dt class="sig sig-object py" id="urllib.request.HTTPSHandler">706<em class="property"><span class="k"><span class="pre">class</span></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>707<dd><p>A class to handle opening of HTTPS URLs. <em>context</em> and <em>check_hostname</em>708have 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>709<div class="versionchanged">710<p><span class="versionmodified changed">Changed in version 3.2: </span><em>context</em> and <em>check_hostname</em> were added.</p>711</div>712</dd></dl>713 714<dl class="py class">715<dt class="sig sig-object py" id="urllib.request.FileHandler">716<em class="property"><span class="k"><span class="pre">class</span></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>717<dd><p>Open local files.</p>718</dd></dl>719 720<dl class="py class">721<dt class="sig sig-object py" id="urllib.request.DataHandler">722<em class="property"><span class="k"><span class="pre">class</span></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>723<dd><p>Open data URLs.</p>724<div class="versionadded">725<p><span class="versionmodified added">Added in version 3.4.</span></p>726</div>727</dd></dl>728 729<dl class="py class">730<dt class="sig sig-object py" id="urllib.request.FTPHandler">731<em class="property"><span class="k"><span class="pre">class</span></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>732<dd><p>Open FTP URLs.</p>733</dd></dl>734 735<dl class="py class">736<dt class="sig sig-object py" id="urllib.request.CacheFTPHandler">737<em class="property"><span class="k"><span class="pre">class</span></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>738<dd><p>Open FTP URLs, keeping a cache of open FTP connections to minimize delays.</p>739</dd></dl>740 741<dl class="py class">742<dt class="sig sig-object py" id="urllib.request.UnknownHandler">743<em class="property"><span class="k"><span class="pre">class</span></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>744<dd><p>A catch-all class to handle unknown URLs.</p>745</dd></dl>746 747<dl class="py class">748<dt class="sig sig-object py" id="urllib.request.HTTPErrorProcessor">749<em class="property"><span class="k"><span class="pre">class</span></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>750<dd><p>Process HTTP error responses.</p>751</dd></dl>752 753<section id="request-objects">754<span id="id1"></span><h2>Request Objects<a class="headerlink" href="#request-objects" title="Link to this heading">¶</a></h2>755<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,756and so all may be overridden in subclasses. It also defines several757public attributes that can be used by clients to inspect the parsed758request.</p>759<dl class="py attribute">760<dt class="sig sig-object py" id="urllib.request.Request.full_url">761<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>762<dd><p>The original URL passed to the constructor.</p>763<div class="versionchanged">764<p><span class="versionmodified changed">Changed in version 3.4.</span></p>765</div>766<p>Request.full_url is a property with setter, getter and a deleter. Getting767<code class="xref py py-attr docutils literal notranslate"><span class="pre">full_url</span></code> returns the original request URL with the768fragment, if it was present.</p>769</dd></dl>770 771<dl class="py attribute">772<dt class="sig sig-object py" id="urllib.request.Request.type">773<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>774<dd><p>The URI scheme.</p>775</dd></dl>776 777<dl class="py attribute">778<dt class="sig sig-object py" id="urllib.request.Request.host">779<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>780<dd><p>The URI authority, typically a host, but may also contain a port781separated by a colon.</p>782</dd></dl>783 784<dl class="py attribute">785<dt class="sig sig-object py" id="urllib.request.Request.origin_req_host">786<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>787<dd><p>The original host for the request, without port.</p>788</dd></dl>789 790<dl class="py attribute">791<dt class="sig sig-object py" id="urllib.request.Request.selector">792<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>793<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 selector794will be the full URL that is passed to the proxy.</p>795</dd></dl>796 797<dl class="py attribute">798<dt class="sig sig-object py" id="urllib.request.Request.data">799<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>800<dd><p>The entity body for the request, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if not specified.</p>801<div class="versionchanged">802<p><span class="versionmodified changed">Changed in version 3.4: </span>Changing value of <code class="xref py py-attr docutils literal notranslate"><span class="pre">Request.data</span></code> now deletes “Content-Length”803header if it was previously set or calculated.</p>804</div>805</dd></dl>806 807<dl class="py attribute">808<dt class="sig sig-object py" id="urllib.request.Request.unverifiable">809<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>810<dd><p>boolean, indicates whether the request is unverifiable as defined811by <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>.</p>812</dd></dl>813 814<dl class="py attribute">815<dt class="sig sig-object py" id="urllib.request.Request.method">816<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>817<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>,818which 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 computation819of the method to be used. Its value can be set (thus overriding the default820computation in <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_method()</span></code>) either by providing a default821value 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 by822passing a value in to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code> constructor via the <em>method</em>823argument.</p>824<div class="versionadded">825<p><span class="versionmodified added">Added in version 3.3.</span></p>826</div>827<div class="versionchanged">828<p><span class="versionmodified changed">Changed in version 3.4: </span>A default value can now be set in subclasses; previously it could only829be set via the constructor argument.</p>830</div>831</dd></dl>832 833<dl class="py method">834<dt class="sig sig-object py" id="urllib.request.Request.get_method">835<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>836<dd><p>Return a string indicating the HTTP request method. If837<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 return838<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.839This is only meaningful for HTTP requests.</p>840<div class="versionchanged">841<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>842</div>843</dd></dl>844 845<dl class="py method">846<dt class="sig sig-object py" id="urllib.request.Request.add_header">847<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>848<dd><p>Add another header to the request. Headers are currently ignored by all849handlers except HTTP handlers, where they are added to the list of headers sent850to the server. Note that there cannot be more than one header with the same851name, and later calls will overwrite previous calls in case the <em>key</em> collides.852Currently, this is no loss of HTTP functionality, since all headers which have853meaning when used more than once have a (header-specific) way of gaining the854same functionality using only one header. Note that headers added using855this method are also added to redirected requests.</p>856</dd></dl>857 858<dl class="py method">859<dt class="sig sig-object py" id="urllib.request.Request.add_unredirected_header">860<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>861<dd><p>Add a header that will not be added to a redirected request.</p>862</dd></dl>863 864<dl class="py method">865<dt class="sig sig-object py" id="urllib.request.Request.has_header">866<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>867<dd><p>Return whether the instance has the named header (checks both regular and868unredirected).</p>869</dd></dl>870 871<dl class="py method">872<dt class="sig sig-object py" id="urllib.request.Request.remove_header">873<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>874<dd><p>Remove named header from the request instance (both from regular and875unredirected headers).</p>876<div class="versionadded">877<p><span class="versionmodified added">Added in version 3.4.</span></p>878</div>879</dd></dl>880 881<dl class="py method">882<dt class="sig sig-object py" id="urllib.request.Request.get_full_url">883<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>884<dd><p>Return the URL given in the constructor.</p>885<div class="versionchanged">886<p><span class="versionmodified changed">Changed in version 3.4.</span></p>887</div>888<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>889</dd></dl>890 891<dl class="py method">892<dt class="sig sig-object py" id="urllib.request.Request.set_proxy">893<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>894<dd><p>Prepare the request by connecting to a proxy server. The <em>host</em> and <em>type</em> will895replace those of the instance, and the instance’s selector will be the original896URL given in the constructor.</p>897</dd></dl>898 899<dl class="py method">900<dt class="sig sig-object py" id="urllib.request.Request.get_header">901<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>902<dd><p>Return the value of the given header. If the header is not present, return903the default value.</p>904</dd></dl>905 906<dl class="py method">907<dt class="sig sig-object py" id="urllib.request.Request.header_items">908<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>909<dd><p>Return a list of tuples (header_name, header_value) of the Request headers.</p>910</dd></dl>911 912<div class="versionchanged">913<p><span class="versionmodified changed">Changed in version 3.4: </span>The request methods add_data, has_data, get_data, get_type, get_host,914get_selector, get_origin_req_host and is_unverifiable that were deprecated915since 3.3 have been removed.</p>916</div>917</section>918<section id="openerdirector-objects">919<span id="opener-director-objects"></span><h2>OpenerDirector Objects<a class="headerlink" href="#openerdirector-objects" title="Link to this heading">¶</a></h2>920<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>921<dl class="py method">922<dt class="sig sig-object py" id="urllib.request.OpenerDirector.add_handler">923<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>924<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 methods925are searched, and added to the possible chains (note that HTTP errors are a926special case). Note that, in the following, <em>protocol</em> should be replaced927with the actual protocol to handle, for example <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_response()</span></code> would928be the HTTP protocol response handler. Also <em>type</em> should be replaced with929the actual HTTP code, for example <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_404()</span></code> would handle HTTP930404 errors.</p>931<ul>932<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_open()</span></code> — signal that the handler knows how to open <em>protocol</em>933URLs.</p>934<p>See <a class="reference internal" href="#protocol-open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.<protocol>_open()</span></code></a> for more information.</p>935</li>936<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_<type>()</span></code> — signal that the handler knows how to handle HTTP937errors with HTTP error code <em>type</em>.</p>938<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_<nnn>()</span></code></a> for more information.</p>939</li>940<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_error()</span></code> — signal that the handler knows how to handle errors941from (non-<code class="docutils literal notranslate"><span class="pre">http</span></code>) <em>protocol</em>.</p></li>942<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_request()</span></code> — signal that the handler knows how to pre-process943<em>protocol</em> requests.</p>944<p>See <a class="reference internal" href="#protocol-request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.<protocol>_request()</span></code></a> for more information.</p>945</li>946<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_response()</span></code> — signal that the handler knows how to947post-process <em>protocol</em> responses.</p>948<p>See <a class="reference internal" href="#protocol-response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseHandler.<protocol>_response()</span></code></a> for more information.</p>949</li>950</ul>951</dd></dl>952 953<dl class="py method">954<dt class="sig sig-object py" id="urllib.request.OpenerDirector.open">955<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>956<dd><p>Open the given <em>url</em> (which can be a request object or a string), optionally957passing the given <em>data</em>. Arguments, return values and exceptions raised are958the 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 <code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code>959method 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>). The960optional <em>timeout</em> parameter specifies a timeout in seconds for blocking961operations like the connection attempt (if not specified, the global default962timeout setting will be used). The timeout feature actually works only for963HTTP, HTTPS and FTP connections.</p>964</dd></dl>965 966<dl class="py method">967<dt class="sig sig-object py" id="urllib.request.OpenerDirector.error">968<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>969<dd><p>Handle an error of the given protocol. This will call the registered error970handlers for the given protocol with the given arguments (which are protocol971specific). The HTTP protocol is a special case which uses the HTTP response972code to determine the specific error handler; refer to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_<type>()</span></code>973methods of the handler classes.</p>974<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>975</dd></dl>976 977<p>OpenerDirector objects open URLs in three stages:</p>978<p>The order in which these methods are called within each stage is determined by979sorting the handler instances.</p>980<ol class="arabic">981<li><p>Every handler with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_request()</span></code> has that982method called to pre-process the request.</p></li>983<li><p>Handlers with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_open()</span></code> are called to handle984the 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>985value (ie. a response), or raises an exception (usually986<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>987<p>In fact, the above algorithm is first tried for methods named988<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 algorithm989is repeated for methods named like <code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_open()</span></code>. If all such methods990return <code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code>, the algorithm is repeated for methods named991<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>992<p>Note that the implementation of these methods may involve calls of the parent993<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> and994<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>995</li>996<li><p>Every handler with a method named like <code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_response()</span></code> has that997method called to post-process the response.</p></li>998</ol>999</section>1000<section id="basehandler-objects">1001<span id="base-handler-objects"></span><h2>BaseHandler Objects<a class="headerlink" href="#basehandler-objects" title="Link to this heading">¶</a></h2>1002<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 directly1003useful, and others that are meant to be used by derived classes. These are1004intended for direct use:</p>1005<dl class="py method">1006<dt class="sig sig-object py" id="urllib.request.BaseHandler.add_parent">1007<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>1008<dd><p>Add a director as parent.</p>1009</dd></dl>1010 1011<dl class="py method">1012<dt class="sig sig-object py" id="urllib.request.BaseHandler.close">1013<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>1014<dd><p>Remove any parents.</p>1015</dd></dl>1016 1017<p>The following attribute and methods should only be used by classes derived from1018<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>1019<div class="admonition note">1020<p class="admonition-title">Note</p>1021<p>The convention has been adopted that subclasses defining1022<code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_request()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre"><protocol>_response()</span></code> methods are named1023<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>1024</div>1025<dl class="py attribute">1026<dt class="sig sig-object py" id="urllib.request.BaseHandler.parent">1027<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>1028<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 different1029protocol, or handle errors.</p>1030</dd></dl>1031 1032<dl class="py method">1033<dt class="sig sig-object py" id="urllib.request.BaseHandler.default_open">1034<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>1035<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 should1036define it if they want to catch all URLs.</p>1037<p>This method, if implemented, will be called by the parent1038<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 in1039the 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 <code class="xref py py-class docutils literal notranslate"><span class="pre">OpenerDirector</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.1040It 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 exceptional1041thing 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 to1042<code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code>).</p>1043<p>This method will be called before any protocol-specific open method.</p>1044</dd></dl>1045 1046<dl class="py method" id="protocol-open">1047<dt class="sig sig-object py">1048<span class="sig-name descname"><span class="pre">BaseHandler.<protocol>_open(req)</span></span></dt>1049<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 should1050define it if they want to handle URLs with the given protocol.</p>1051<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>.1052Return 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>1053</dd></dl>1054 1055<dl class="py method">1056<dt class="sig sig-object py" id="urllib.request.BaseHandler.unknown_open">1057<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>1058<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 should1059define it if they want to catch all URLs with no specific registered handler to1060open it.</p>1061<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>1062<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 for1063<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>1064</dd></dl>1065 1066<dl class="py method">1067<dt class="sig sig-object py" id="urllib.request.BaseHandler.http_error_default">1068<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>1069<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 should1070override it if they intend to provide a catch-all for otherwise unhandled HTTP1071errors. 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> getting1072the error, and should not normally be called in other circumstances.</p>1073<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> will call this method with five positional arguments:</p>1074<ol class="arabic simple">1075<li><p>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></li>1076<li><p>a file-like object with the HTTP error body,</p></li>1077<li><p>the three-digit code of the error, as a string,</p></li>1078<li><p>the user-visible explanation of the code, as a string, and</p></li>1079<li><p>the headers of the error, as a mapping object.</p></li>1080</ol>1081<p>Return values and exceptions raised should be the same as those of1082<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>1083</dd></dl>1084 1085<dl class="py method" id="http-error-nnn">1086<dt class="sig sig-object py">1087<span class="sig-name descname"><span class="pre">BaseHandler.http_error_<nnn>(req,</span> <span class="pre">fp,</span> <span class="pre">code,</span> <span class="pre">msg,</span> <span class="pre">hdrs)</span></span></dt>1088<dd><p><em>nnn</em> should be a three-digit HTTP error code. This method is also not defined1089in <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 a1090subclass, when an HTTP error with code <em>nnn</em> occurs.</p>1091<p>Subclasses should override this method to handle specific HTTP errors.</p>1092<p>Arguments, return values and exceptions raised should be the same as for1093<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>1094</dd></dl>1095 1096<dl class="py method" id="protocol-request">1097<dt class="sig sig-object py">1098<span class="sig-name descname"><span class="pre">BaseHandler.<protocol>_request(req)</span></span></dt>1099<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 should1100define it if they want to pre-process requests of the given protocol.</p>1101<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>.1102<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 a1103<code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code> object.</p>1104</dd></dl>1105 1106<dl class="py method" id="protocol-response">1107<dt class="sig sig-object py">1108<span class="sig-name descname"><span class="pre">BaseHandler.<protocol>_response(req,</span> <span class="pre">response)</span></span></dt>1109<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 should1110define it if they want to post-process responses of the given protocol.</p>1111<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>.1112<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 object1113implementing 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>. The1114return value should implement the same interface as the return value of1115<code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code>.</p>1116</dd></dl>1117 1118</section>1119<section id="httpredirecthandler-objects">1120<span id="http-redirect-handler"></span><h2>HTTPRedirectHandler Objects<a class="headerlink" href="#httpredirecthandler-objects" title="Link to this heading">¶</a></h2>1121<div class="admonition note">1122<p class="admonition-title">Note</p>1123<p>Some HTTP redirections require action from this module’s client code. If this1124is 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-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a> for1125details of the precise meanings of the various redirection codes.</p>1126<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 the1127HTTPRedirectHandler is presented with a redirected URL which is not an HTTP,1128HTTPS or FTP URL.</p>1129</div>1130<dl class="py method">1131<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.redirect_request">1132<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>1133<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 called1134by the default implementations of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_30*()</span></code> methods when a1135redirection is received from the server. If a redirection should take place,1136return a new <code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code> to allow <code class="xref py py-meth docutils literal notranslate"><span class="pre">http_error_30*()</span></code> to perform the1137redirect 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> if1138no other handler should try to handle this URL, or return <code class="docutils literal notranslate"><span class="pre">None</span></code> if you1139can’t but another handler might.</p>1140<div class="admonition note">1141<p class="admonition-title">Note</p>1142<p>The default implementation of this method does not strictly follow <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a>,1143which says that 301 and 302 responses to <code class="docutils literal notranslate"><span class="pre">POST</span></code> requests must not be1144automatically redirected without confirmation by the user. In reality, browsers1145do allow automatic redirection of these responses, changing the POST to a1146<code class="docutils literal notranslate"><span class="pre">GET</span></code>, and the default implementation reproduces this behavior.</p>1147</div>1148</dd></dl>1149 1150<dl class="py method">1151<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_301">1152<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>1153<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 the1154parent <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>1155</dd></dl>1156 1157<dl class="py method">1158<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_302">1159<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>1160<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>1161</dd></dl>1162 1163<dl class="py method">1164<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_303">1165<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>1166<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>1167</dd></dl>1168 1169<dl class="py method">1170<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_307">1171<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>1172<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’1173response. It does not allow changing the request method from <code class="docutils literal notranslate"><span class="pre">POST</span></code>1174to <code class="docutils literal notranslate"><span class="pre">GET</span></code>.</p>1175</dd></dl>1176 1177<dl class="py method">1178<dt class="sig sig-object py" id="urllib.request.HTTPRedirectHandler.http_error_308">1179<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>1180<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’1181response. It does not allow changing the request method from <code class="docutils literal notranslate"><span class="pre">POST</span></code>1182to <code class="docutils literal notranslate"><span class="pre">GET</span></code>.</p>1183<div class="versionadded">1184<p><span class="versionmodified added">Added in version 3.11.</span></p>1185</div>1186</dd></dl>1187 1188</section>1189<section id="httpcookieprocessor-objects">1190<span id="http-cookie-processor"></span><h2>HTTPCookieProcessor Objects<a class="headerlink" href="#httpcookieprocessor-objects" title="Link to this heading">¶</a></h2>1191<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>1192<dl class="py attribute">1193<dt class="sig sig-object py" id="urllib.request.HTTPCookieProcessor.cookiejar">1194<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>1195<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>1196</dd></dl>1197 1198</section>1199<section id="proxyhandler-objects">1200<span id="proxy-handler"></span><h2>ProxyHandler Objects<a class="headerlink" href="#proxyhandler-objects" title="Link to this heading">¶</a></h2>