Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 18.118.226.167
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
howto /
[ HOME SHELL ]
Name
Size
Permission
Action
argparse.html
72.39
KB
-rw-r--r--
cporting.html
39.22
KB
-rw-r--r--
curses.html
42.8
KB
-rw-r--r--
descriptor.html
50.15
KB
-rw-r--r--
doanddont.html
38.69
KB
-rw-r--r--
functional.html
109
KB
-rw-r--r--
index.html
7.51
KB
-rw-r--r--
logging-cookbook.html
103.51
KB
-rw-r--r--
logging.html
93.47
KB
-rw-r--r--
pyporting.html
60.8
KB
-rw-r--r--
regex.html
133.84
KB
-rw-r--r--
sockets.html
39.93
KB
-rw-r--r--
sorting.html
44.25
KB
-rw-r--r--
unicode.html
64.29
KB
-rw-r--r--
urllib2.html
58.48
KB
-rw-r--r--
webservers.html
55.66
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : urllib2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>HOWTO Fetch Internet Resources Using urllib2 — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="Python HOWTOs" href="index.html" /> <link rel="next" title="HOWTO Use Python in the web" href="webservers.html" /> <link rel="prev" title="Unicode HOWTO" href="unicode.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="webservers.html" title="HOWTO Use Python in the web" accesskey="N">next</a> |</li> <li class="right" > <a href="unicode.html" title="Unicode HOWTO" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">Python HOWTOs</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="howto-fetch-internet-resources-using-urllib2"> <h1>HOWTO Fetch Internet Resources Using urllib2<a class="headerlink" href="#howto-fetch-internet-resources-using-urllib2" title="Permalink to this headline">¶</a></h1> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Author:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/index.shtml">Michael Foord</a></td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">There is an French translation of an earlier revision of this HOWTO, available at <a class="reference external" href="http://www.voidspace.org.uk/python/articles/urllib2_francais.shtml">urllib2 - Le Manuel manquant</a>.</p> </div> <div class="section" id="introduction"> <h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <div class="sidebar"> <p class="first sidebar-title">Related Articles</p> <p>You may also find useful the following article on fetching web resources with Python :</p> <ul class="last"> <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/python/articles/authentication.shtml">Basic Authentication</a></p> <blockquote> <div><p>A tutorial on <em>Basic Authentication</em>, with examples in Python.</p> </div></blockquote> </li> </ul> </div> <p><strong>urllib2</strong> is a <a class="reference external" href="http://www.python.org">Python</a> module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the <em>urlopen</em> function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. These are provided by objects called handlers and openers.</p> <p>urllib2 supports fetching URLs for many “URL schemes” (identified by the string before the ”:” in URL - for example “ftp” is the URL scheme of “<a class="reference external" href="ftp://python.org/">ftp://python.org/</a>”) using their associated network protocols (e.g. FTP, HTTP). This tutorial focuses on the most common case, HTTP.</p> <p>For straightforward situations <em>urlopen</em> is very easy to use. But as soon as you encounter errors or non-trivial cases when opening HTTP URLs, you will need some understanding of the HyperText Transfer Protocol. The most comprehensive and authoritative reference to HTTP is <span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a>. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using <em>urllib2</em>, with enough detail about HTTP to help you through. It is not intended to replace the <a class="reference internal" href="../library/urllib2.html#module-urllib2" title="urllib2: Next generation URL opening library."><tt class="xref py py-mod docutils literal"><span class="pre">urllib2</span></tt></a> docs, but is supplementary to them.</p> </div> <div class="section" id="fetching-urls"> <h2>Fetching URLs<a class="headerlink" href="#fetching-urls" title="Permalink to this headline">¶</a></h2> <p>The simplest way to use urllib2 is as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib2</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">'http://python.org/'</span><span class="p">)</span> <span class="n">html</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </pre></div> </div> <p>Many uses of urllib2 will be that simple (note that instead of an ‘http:’ URL we could have used an URL starting with ‘ftp:’, ‘file:’, etc.). However, it’s the purpose of this tutorial to explain the more complicated cases, concentrating on HTTP.</p> <p>HTTP is based on requests and responses - the client makes requests and servers send responses. urllib2 mirrors this with a <tt class="docutils literal"><span class="pre">Request</span></tt> object which represents the HTTP request you are making. In its simplest form you create a Request object that specifies the URL you want to fetch. Calling <tt class="docutils literal"><span class="pre">urlopen</span></tt> with this Request object returns a response object for the URL requested. This response is a file-like object, which means you can for example call <tt class="docutils literal"><span class="pre">.read()</span></tt> on the response:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib2</span> <span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">'http://www.voidspace.org.uk'</span><span class="p">)</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="n">the_page</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </pre></div> </div> <p>Note that urllib2 makes use of the same Request interface to handle all URL schemes. For example, you can make an FTP request like so:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">'ftp://example.com/'</span><span class="p">)</span> </pre></div> </div> <p>In the case of HTTP, there are two extra things that Request objects allow you to do: First, you can pass data to be sent to the server. Second, you can pass extra information (“metadata”) <em>about</em> the data or the about request itself, to the server - this information is sent as HTTP “headers”. Let’s look at each of these in turn.</p> <div class="section" id="data"> <h3>Data<a class="headerlink" href="#data" title="Permalink to this headline">¶</a></h3> <p>Sometimes you want to send data to a URL (often the URL will refer to a CGI (Common Gateway Interface) script <a class="footnote-reference" href="#id9" id="id1">[1]</a> or other web application). With HTTP, this is often done using what’s known as a <strong>POST</strong> request. This is often what your browser does when you submit a HTML form that you filled in on the web. Not all POSTs have to come from forms: you can use a POST to transmit arbitrary data to your own application. In the common case of HTML forms, the data needs to be encoded in a standard way, and then passed to the Request object as the <tt class="docutils literal"><span class="pre">data</span></tt> argument. The encoding is done using a function from the <tt class="docutils literal"><span class="pre">urllib</span></tt> library <em>not</em> from <tt class="docutils literal"><span class="pre">urllib2</span></tt>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="nn">urllib2</span> <span class="n">url</span> <span class="o">=</span> <span class="s">'http://www.someserver.com/cgi-bin/register.cgi'</span> <span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s">'name'</span> <span class="p">:</span> <span class="s">'Michael Foord'</span><span class="p">,</span> <span class="s">'location'</span> <span class="p">:</span> <span class="s">'Northampton'</span><span class="p">,</span> <span class="s">'language'</span> <span class="p">:</span> <span class="s">'Python'</span> <span class="p">}</span> <span class="n">data</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="n">the_page</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </pre></div> </div> <p>Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see <a class="reference external" href="http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13">HTML Specification, Form Submission</a> for more details).</p> <p>If you do not pass the <tt class="docutils literal"><span class="pre">data</span></tt> argument, urllib2 uses a <strong>GET</strong> request. One way in which GET and POST requests differ is that POST requests often have “side-effects”: they change the state of the system in some way (for example by placing an order with the website for a hundredweight of tinned spam to be delivered to your door). Though the HTTP standard makes it clear that POSTs are intended to <em>always</em> cause side-effects, and GET requests <em>never</em> to cause side-effects, nothing prevents a GET request from having side-effects, nor a POST requests from having no side-effects. Data can also be passed in an HTTP GET request by encoding it in the URL itself.</p> <p>This is done as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib2</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib</span> <span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="p">{}</span> <span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="s">'name'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Somebody Here'</span> <span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="s">'location'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Northampton'</span> <span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="s">'language'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Python'</span> <span class="gp">>>> </span><span class="n">url_values</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">url_values</span> <span class="c"># The order may differ. </span> <span class="go">name=Somebody+Here&language=Python&location=Northampton</span> <span class="gp">>>> </span><span class="n">url</span> <span class="o">=</span> <span class="s">'http://www.example.com/example.cgi'</span> <span class="gp">>>> </span><span class="n">full_url</span> <span class="o">=</span> <span class="n">url</span> <span class="o">+</span> <span class="s">'?'</span> <span class="o">+</span> <span class="n">url_values</span> <span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">full_url</span><span class="p">)</span> </pre></div> </div> <p>Notice that the full URL is created by adding a <tt class="docutils literal"><span class="pre">?</span></tt> to the URL, followed by the encoded values.</p> </div> <div class="section" id="headers"> <h3>Headers<a class="headerlink" href="#headers" title="Permalink to this headline">¶</a></h3> <p>We’ll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request.</p> <p>Some websites <a class="footnote-reference" href="#id10" id="id2">[2]</a> dislike being browsed by programs, or send different versions to different browsers <a class="footnote-reference" href="#id11" id="id3">[3]</a> . By default urllib2 identifies itself as <tt class="docutils literal"><span class="pre">Python-urllib/x.y</span></tt> (where <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> are the major and minor version numbers of the Python release, e.g. <tt class="docutils literal"><span class="pre">Python-urllib/2.5</span></tt>), which may confuse the site, or just plain not work. The way a browser identifies itself is through the <tt class="docutils literal"><span class="pre">User-Agent</span></tt> header <a class="footnote-reference" href="#id12" id="id4">[4]</a>. When you create a Request object you can pass a dictionary of headers in. The following example makes the same request as above, but identifies itself as a version of Internet Explorer <a class="footnote-reference" href="#id13" id="id5">[5]</a>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="nn">urllib2</span> <span class="n">url</span> <span class="o">=</span> <span class="s">'http://www.someserver.com/cgi-bin/register.cgi'</span> <span class="n">user_agent</span> <span class="o">=</span> <span class="s">'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'</span> <span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s">'name'</span> <span class="p">:</span> <span class="s">'Michael Foord'</span><span class="p">,</span> <span class="s">'location'</span> <span class="p">:</span> <span class="s">'Northampton'</span><span class="p">,</span> <span class="s">'language'</span> <span class="p">:</span> <span class="s">'Python'</span> <span class="p">}</span> <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span> <span class="s">'User-Agent'</span> <span class="p">:</span> <span class="n">user_agent</span> <span class="p">}</span> <span class="n">data</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="n">the_page</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </pre></div> </div> <p>The response also has two useful methods. See the section on <a class="reference internal" href="#info-and-geturl">info and geturl</a> which comes after we have a look at what happens when things go wrong.</p> </div> </div> <div class="section" id="handling-exceptions"> <h2>Handling Exceptions<a class="headerlink" href="#handling-exceptions" title="Permalink to this headline">¶</a></h2> <p><em>urlopen</em> raises <tt class="xref py py-exc docutils literal"><span class="pre">URLError</span></tt> when it cannot handle a response (though as usual with Python APIs, built-in exceptions such as <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> etc. may also be raised).</p> <p><tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt> is the subclass of <tt class="xref py py-exc docutils literal"><span class="pre">URLError</span></tt> raised in the specific case of HTTP URLs.</p> <div class="section" id="urlerror"> <h3>URLError<a class="headerlink" href="#urlerror" title="Permalink to this headline">¶</a></h3> <p>Often, URLError is raised because there is no network connection (no route to the specified server), or the specified server doesn’t exist. In this case, the exception raised will have a ‘reason’ attribute, which is a tuple containing an error code and a text error message.</p> <p>e.g.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">'http://www.pretend_server.org'</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">try</span><span class="p">:</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="gp">... </span><span class="k">except</span> <span class="n">URLError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">e</span><span class="o">.</span><span class="n">reason</span> <span class="gp">...</span> <span class="go">(4, 'getaddrinfo failed')</span> </pre></div> </div> </div> <div class="section" id="httperror"> <h3>HTTPError<a class="headerlink" href="#httperror" title="Permalink to this headline">¶</a></h3> <p>Every HTTP response from the server contains a numeric “status code”. Sometimes the status code indicates that the server is unable to fulfil the request. The default handlers will handle some of these responses for you (for example, if the response is a “redirection” that requests the client fetch the document from a different URL, urllib2 will handle that for you). For those it can’t handle, urlopen will raise an <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt>. Typical errors include ‘404’ (page not found), ‘403’ (request forbidden), and ‘401’ (authentication required).</p> <p>See section 10 of RFC 2616 for a reference on all the HTTP error codes.</p> <p>The <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt> instance raised will have an integer ‘code’ attribute, which corresponds to the error sent by the server.</p> <div class="section" id="error-codes"> <h4>Error Codes<a class="headerlink" href="#error-codes" title="Permalink to this headline">¶</a></h4> <p>Because the default handlers handle redirects (codes in the 300 range), and codes in the 100-299 range indicate success, you will usually only see error codes in the 400-599 range.</p> <p><tt class="docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler.responses</span></tt> is a useful dictionary of response codes in that shows all the response codes used by RFC 2616. The dictionary is reproduced here for convenience</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># Table mapping response codes to messages; entries have the</span> <span class="c"># form {code: (shortmessage, longmessage)}.</span> <span class="n">responses</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">100</span><span class="p">:</span> <span class="p">(</span><span class="s">'Continue'</span><span class="p">,</span> <span class="s">'Request received, please continue'</span><span class="p">),</span> <span class="mi">101</span><span class="p">:</span> <span class="p">(</span><span class="s">'Switching Protocols'</span><span class="p">,</span> <span class="s">'Switching to new protocol; obey Upgrade header'</span><span class="p">),</span> <span class="mi">200</span><span class="p">:</span> <span class="p">(</span><span class="s">'OK'</span><span class="p">,</span> <span class="s">'Request fulfilled, document follows'</span><span class="p">),</span> <span class="mi">201</span><span class="p">:</span> <span class="p">(</span><span class="s">'Created'</span><span class="p">,</span> <span class="s">'Document created, URL follows'</span><span class="p">),</span> <span class="mi">202</span><span class="p">:</span> <span class="p">(</span><span class="s">'Accepted'</span><span class="p">,</span> <span class="s">'Request accepted, processing continues off-line'</span><span class="p">),</span> <span class="mi">203</span><span class="p">:</span> <span class="p">(</span><span class="s">'Non-Authoritative Information'</span><span class="p">,</span> <span class="s">'Request fulfilled from cache'</span><span class="p">),</span> <span class="mi">204</span><span class="p">:</span> <span class="p">(</span><span class="s">'No Content'</span><span class="p">,</span> <span class="s">'Request fulfilled, nothing follows'</span><span class="p">),</span> <span class="mi">205</span><span class="p">:</span> <span class="p">(</span><span class="s">'Reset Content'</span><span class="p">,</span> <span class="s">'Clear input form for further input.'</span><span class="p">),</span> <span class="mi">206</span><span class="p">:</span> <span class="p">(</span><span class="s">'Partial Content'</span><span class="p">,</span> <span class="s">'Partial content follows.'</span><span class="p">),</span> <span class="mi">300</span><span class="p">:</span> <span class="p">(</span><span class="s">'Multiple Choices'</span><span class="p">,</span> <span class="s">'Object has several resources -- see URI list'</span><span class="p">),</span> <span class="mi">301</span><span class="p">:</span> <span class="p">(</span><span class="s">'Moved Permanently'</span><span class="p">,</span> <span class="s">'Object moved permanently -- see URI list'</span><span class="p">),</span> <span class="mi">302</span><span class="p">:</span> <span class="p">(</span><span class="s">'Found'</span><span class="p">,</span> <span class="s">'Object moved temporarily -- see URI list'</span><span class="p">),</span> <span class="mi">303</span><span class="p">:</span> <span class="p">(</span><span class="s">'See Other'</span><span class="p">,</span> <span class="s">'Object moved -- see Method and URL list'</span><span class="p">),</span> <span class="mi">304</span><span class="p">:</span> <span class="p">(</span><span class="s">'Not Modified'</span><span class="p">,</span> <span class="s">'Document has not changed since given time'</span><span class="p">),</span> <span class="mi">305</span><span class="p">:</span> <span class="p">(</span><span class="s">'Use Proxy'</span><span class="p">,</span> <span class="s">'You must use proxy specified in Location to access this '</span> <span class="s">'resource.'</span><span class="p">),</span> <span class="mi">307</span><span class="p">:</span> <span class="p">(</span><span class="s">'Temporary Redirect'</span><span class="p">,</span> <span class="s">'Object moved temporarily -- see URI list'</span><span class="p">),</span> <span class="mi">400</span><span class="p">:</span> <span class="p">(</span><span class="s">'Bad Request'</span><span class="p">,</span> <span class="s">'Bad request syntax or unsupported method'</span><span class="p">),</span> <span class="mi">401</span><span class="p">:</span> <span class="p">(</span><span class="s">'Unauthorized'</span><span class="p">,</span> <span class="s">'No permission -- see authorization schemes'</span><span class="p">),</span> <span class="mi">402</span><span class="p">:</span> <span class="p">(</span><span class="s">'Payment Required'</span><span class="p">,</span> <span class="s">'No payment -- see charging schemes'</span><span class="p">),</span> <span class="mi">403</span><span class="p">:</span> <span class="p">(</span><span class="s">'Forbidden'</span><span class="p">,</span> <span class="s">'Request forbidden -- authorization will not help'</span><span class="p">),</span> <span class="mi">404</span><span class="p">:</span> <span class="p">(</span><span class="s">'Not Found'</span><span class="p">,</span> <span class="s">'Nothing matches the given URI'</span><span class="p">),</span> <span class="mi">405</span><span class="p">:</span> <span class="p">(</span><span class="s">'Method Not Allowed'</span><span class="p">,</span> <span class="s">'Specified method is invalid for this server.'</span><span class="p">),</span> <span class="mi">406</span><span class="p">:</span> <span class="p">(</span><span class="s">'Not Acceptable'</span><span class="p">,</span> <span class="s">'URI not available in preferred format.'</span><span class="p">),</span> <span class="mi">407</span><span class="p">:</span> <span class="p">(</span><span class="s">'Proxy Authentication Required'</span><span class="p">,</span> <span class="s">'You must authenticate with '</span> <span class="s">'this proxy before proceeding.'</span><span class="p">),</span> <span class="mi">408</span><span class="p">:</span> <span class="p">(</span><span class="s">'Request Timeout'</span><span class="p">,</span> <span class="s">'Request timed out; try again later.'</span><span class="p">),</span> <span class="mi">409</span><span class="p">:</span> <span class="p">(</span><span class="s">'Conflict'</span><span class="p">,</span> <span class="s">'Request conflict.'</span><span class="p">),</span> <span class="mi">410</span><span class="p">:</span> <span class="p">(</span><span class="s">'Gone'</span><span class="p">,</span> <span class="s">'URI no longer exists and has been permanently removed.'</span><span class="p">),</span> <span class="mi">411</span><span class="p">:</span> <span class="p">(</span><span class="s">'Length Required'</span><span class="p">,</span> <span class="s">'Client must specify Content-Length.'</span><span class="p">),</span> <span class="mi">412</span><span class="p">:</span> <span class="p">(</span><span class="s">'Precondition Failed'</span><span class="p">,</span> <span class="s">'Precondition in headers is false.'</span><span class="p">),</span> <span class="mi">413</span><span class="p">:</span> <span class="p">(</span><span class="s">'Request Entity Too Large'</span><span class="p">,</span> <span class="s">'Entity is too large.'</span><span class="p">),</span> <span class="mi">414</span><span class="p">:</span> <span class="p">(</span><span class="s">'Request-URI Too Long'</span><span class="p">,</span> <span class="s">'URI is too long.'</span><span class="p">),</span> <span class="mi">415</span><span class="p">:</span> <span class="p">(</span><span class="s">'Unsupported Media Type'</span><span class="p">,</span> <span class="s">'Entity body in unsupported format.'</span><span class="p">),</span> <span class="mi">416</span><span class="p">:</span> <span class="p">(</span><span class="s">'Requested Range Not Satisfiable'</span><span class="p">,</span> <span class="s">'Cannot satisfy request range.'</span><span class="p">),</span> <span class="mi">417</span><span class="p">:</span> <span class="p">(</span><span class="s">'Expectation Failed'</span><span class="p">,</span> <span class="s">'Expect condition could not be satisfied.'</span><span class="p">),</span> <span class="mi">500</span><span class="p">:</span> <span class="p">(</span><span class="s">'Internal Server Error'</span><span class="p">,</span> <span class="s">'Server got itself in trouble'</span><span class="p">),</span> <span class="mi">501</span><span class="p">:</span> <span class="p">(</span><span class="s">'Not Implemented'</span><span class="p">,</span> <span class="s">'Server does not support this operation'</span><span class="p">),</span> <span class="mi">502</span><span class="p">:</span> <span class="p">(</span><span class="s">'Bad Gateway'</span><span class="p">,</span> <span class="s">'Invalid responses from another server/proxy.'</span><span class="p">),</span> <span class="mi">503</span><span class="p">:</span> <span class="p">(</span><span class="s">'Service Unavailable'</span><span class="p">,</span> <span class="s">'The server cannot process the request due to a high load'</span><span class="p">),</span> <span class="mi">504</span><span class="p">:</span> <span class="p">(</span><span class="s">'Gateway Timeout'</span><span class="p">,</span> <span class="s">'The gateway server did not receive a timely response'</span><span class="p">),</span> <span class="mi">505</span><span class="p">:</span> <span class="p">(</span><span class="s">'HTTP Version Not Supported'</span><span class="p">,</span> <span class="s">'Cannot fulfill request.'</span><span class="p">),</span> <span class="p">}</span> </pre></div> </div> <p>When an error is raised the server responds by returning an HTTP error code <em>and</em> an error page. You can use the <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt> instance as a response on the page returned. This means that as well as the code attribute, it also has read, geturl, and info, methods.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">'http://www.python.org/fish.html'</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">try</span><span class="p">:</span> <span class="gp">... </span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> <span class="gp">... </span><span class="k">except</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">HTTPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">e</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="gp">...</span> <span class="go">404</span> <span class="go"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span class="go">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span> <span class="gp">...</span> <span class="go"><title>Page Not Found</title></span> <span class="gp">...</span> </pre></div> </div> </div> </div> <div class="section" id="wrapping-it-up"> <h3>Wrapping it Up<a class="headerlink" href="#wrapping-it-up" title="Permalink to this headline">¶</a></h3> <p>So if you want to be prepared for <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt> <em>or</em> <tt class="xref py py-exc docutils literal"><span class="pre">URLError</span></tt> there are two basic approaches. I prefer the second approach.</p> <div class="section" id="number-1"> <h4>Number 1<a class="headerlink" href="#number-1" title="Permalink to this headline">¶</a></h4> <div class="highlight-python"><pre>from urllib2 import Request, urlopen, URLError, HTTPError req = Request(someurl) try: response = urlopen(req) except HTTPError as e: print 'The server couldn\'t fulfill the request.' print 'Error code: ', e.code except URLError as e: print 'We failed to reach a server.' print 'Reason: ', e.reason else: # everything is fine</pre> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <tt class="docutils literal"><span class="pre">except</span> <span class="pre">HTTPError</span></tt> <em>must</em> come first, otherwise <tt class="docutils literal"><span class="pre">except</span> <span class="pre">URLError</span></tt> will <em>also</em> catch an <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt>.</p> </div> </div> <div class="section" id="number-2"> <h4>Number 2<a class="headerlink" href="#number-2" title="Permalink to this headline">¶</a></h4> <div class="highlight-python"><pre>from urllib2 import Request, urlopen, URLError req = Request(someurl) try: response = urlopen(req) except URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server couldn\'t fulfill the request.' print 'Error code: ', e.code else: # everything is fine</pre> </div> </div> </div> </div> <div class="section" id="info-and-geturl"> <h2>info and geturl<a class="headerlink" href="#info-and-geturl" title="Permalink to this headline">¶</a></h2> <p>The response returned by urlopen (or the <tt class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></tt> instance) has two useful methods <tt class="xref py py-meth docutils literal"><span class="pre">info()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">geturl()</span></tt>.</p> <p><strong>geturl</strong> - this returns the real URL of the page fetched. This is useful because <tt class="docutils literal"><span class="pre">urlopen</span></tt> (or the opener object used) may have followed a redirect. The URL of the page fetched may not be the same as the URL requested.</p> <p><strong>info</strong> - this returns a dictionary-like object that describes the page fetched, particularly the headers sent by the server. It is currently an <tt class="docutils literal"><span class="pre">httplib.HTTPMessage</span></tt> instance.</p> <p>Typical headers include ‘Content-length’, ‘Content-type’, and so on. See the <a class="reference external" href="http://www.cs.tut.fi/~jkorpela/http.html">Quick Reference to HTTP Headers</a> for a useful listing of HTTP headers with brief explanations of their meaning and use.</p> </div> <div class="section" id="openers-and-handlers"> <h2>Openers and Handlers<a class="headerlink" href="#openers-and-handlers" title="Permalink to this headline">¶</a></h2> <p>When you fetch a URL you use an opener (an instance of the perhaps confusingly-named <a class="reference internal" href="../library/urllib2.html#urllib2.OpenerDirector" title="urllib2.OpenerDirector"><tt class="xref py py-class docutils literal"><span class="pre">urllib2.OpenerDirector</span></tt></a>). Normally we have been using the default opener - via <tt class="docutils literal"><span class="pre">urlopen</span></tt> - but you can create custom openers. Openers use handlers. All the “heavy lifting” is done by the handlers. Each handler knows how to open URLs for a particular URL scheme (http, ftp, etc.), or how to handle an aspect of URL opening, for example HTTP redirections or HTTP cookies.</p> <p>You will want to create openers if you want to fetch URLs with specific handlers installed, for example to get an opener that handles cookies, or to get an opener that does not handle redirections.</p> <p>To create an opener, instantiate an <tt class="docutils literal"><span class="pre">OpenerDirector</span></tt>, and then call <tt class="docutils literal"><span class="pre">.add_handler(some_handler_instance)</span></tt> repeatedly.</p> <p>Alternatively, you can use <tt class="docutils literal"><span class="pre">build_opener</span></tt>, which is a convenience function for creating opener objects with a single function call. <tt class="docutils literal"><span class="pre">build_opener</span></tt> adds several handlers by default, but provides a quick way to add more and/or override the default handlers.</p> <p>Other sorts of handlers you might want to can handle proxies, authentication, and other common but slightly specialised situations.</p> <p><tt class="docutils literal"><span class="pre">install_opener</span></tt> can be used to make an <tt class="docutils literal"><span class="pre">opener</span></tt> object the (global) default opener. This means that calls to <tt class="docutils literal"><span class="pre">urlopen</span></tt> will use the opener you have installed.</p> <p>Opener objects have an <tt class="docutils literal"><span class="pre">open</span></tt> method, which can be called directly to fetch urls in the same way as the <tt class="docutils literal"><span class="pre">urlopen</span></tt> function: there’s no need to call <tt class="docutils literal"><span class="pre">install_opener</span></tt>, except as a convenience.</p> </div> <div class="section" id="id6"> <h2>Basic Authentication<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h2> <p>To illustrate creating and installing a handler we will use the <tt class="docutils literal"><span class="pre">HTTPBasicAuthHandler</span></tt>. For a more detailed discussion of this subject – including an explanation of how Basic Authentication works - see the <a class="reference external" href="http://www.voidspace.org.uk/python/articles/authentication.shtml">Basic Authentication Tutorial</a>.</p> <p>When authentication is required, the server sends a header (as well as the 401 error code) requesting authentication. This specifies the authentication scheme and a ‘realm’. The header looks like : <tt class="docutils literal"><span class="pre">WWW-Authenticate:</span> <span class="pre">SCHEME</span> <span class="pre">realm="REALM"</span></tt>.</p> <p>e.g.</p> <div class="highlight-python"><pre>WWW-Authenticate: Basic realm="cPanel Users"</pre> </div> <p>The client should then retry the request with the appropriate name and password for the realm included as a header in the request. This is ‘basic authentication’. In order to simplify this process we can create an instance of <tt class="docutils literal"><span class="pre">HTTPBasicAuthHandler</span></tt> and an opener to use this handler.</p> <p>The <tt class="docutils literal"><span class="pre">HTTPBasicAuthHandler</span></tt> uses an object called a password manager to handle the mapping of URLs and realms to passwords and usernames. If you know what the realm is (from the authentication header sent by the server), then you can use a <tt class="docutils literal"><span class="pre">HTTPPasswordMgr</span></tt>. Frequently one doesn’t care what the realm is. In that case, it is convenient to use <tt class="docutils literal"><span class="pre">HTTPPasswordMgrWithDefaultRealm</span></tt>. This allows you to specify a default username and password for a URL. This will be supplied in the absence of you providing an alternative combination for a specific realm. We indicate this by providing <tt class="docutils literal"><span class="pre">None</span></tt> as the realm argument to the <tt class="docutils literal"><span class="pre">add_password</span></tt> method.</p> <p>The top-level URL is the first URL that requires authentication. URLs “deeper” than the URL you pass to .add_password() will also match.</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># create a password manager</span> <span class="n">password_mgr</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">HTTPPasswordMgrWithDefaultRealm</span><span class="p">()</span> <span class="c"># Add the username and password.</span> <span class="c"># If we knew the realm, we could use it instead of None.</span> <span class="n">top_level_url</span> <span class="o">=</span> <span class="s">"http://example.com/foo/"</span> <span class="n">password_mgr</span><span class="o">.</span><span class="n">add_password</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">top_level_url</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="n">handler</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">HTTPBasicAuthHandler</span><span class="p">(</span><span class="n">password_mgr</span><span class="p">)</span> <span class="c"># create "opener" (OpenerDirector instance)</span> <span class="n">opener</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span> <span class="c"># use the opener to fetch a URL</span> <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">a_url</span><span class="p">)</span> <span class="c"># Install the opener.</span> <span class="c"># Now all calls to urllib2.urlopen use our opener.</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">install_opener</span><span class="p">(</span><span class="n">opener</span><span class="p">)</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">In the above example we only supplied our <tt class="docutils literal"><span class="pre">HTTPBasicAuthHandler</span></tt> to <tt class="docutils literal"><span class="pre">build_opener</span></tt>. By default openers have the handlers for normal situations – <tt class="docutils literal"><span class="pre">ProxyHandler</span></tt> (if a proxy setting such as an <span class="target" id="index-1"></span><tt class="xref std std-envvar docutils literal"><span class="pre">http_proxy</span></tt> environment variable is set), <tt class="docutils literal"><span class="pre">UnknownHandler</span></tt>, <tt class="docutils literal"><span class="pre">HTTPHandler</span></tt>, <tt class="docutils literal"><span class="pre">HTTPDefaultErrorHandler</span></tt>, <tt class="docutils literal"><span class="pre">HTTPRedirectHandler</span></tt>, <tt class="docutils literal"><span class="pre">FTPHandler</span></tt>, <tt class="docutils literal"><span class="pre">FileHandler</span></tt>, <tt class="docutils literal"><span class="pre">HTTPErrorProcessor</span></tt>.</p> </div> <p><tt class="docutils literal"><span class="pre">top_level_url</span></tt> is in fact <em>either</em> a full URL (including the ‘http:’ scheme component and the hostname and optionally the port number) e.g. “<a class="reference external" href="http://example.com/">http://example.com/</a>” <em>or</em> an “authority” (i.e. the hostname, optionally including the port number) e.g. “example.com” or “example.com:8080” (the latter example includes a port number). The authority, if present, must NOT contain the “userinfo” component - for example “joe@password:example.com” is not correct.</p> </div> <div class="section" id="proxies"> <h2>Proxies<a class="headerlink" href="#proxies" title="Permalink to this headline">¶</a></h2> <p><strong>urllib2</strong> will auto-detect your proxy settings and use those. This is through the <tt class="docutils literal"><span class="pre">ProxyHandler</span></tt>, which is part of the normal handler chain when a proxy setting is detected. Normally that’s a good thing, but there are occasions when it may not be helpful <a class="footnote-reference" href="#id14" id="id7">[6]</a>. One way to do this is to setup our own <tt class="docutils literal"><span class="pre">ProxyHandler</span></tt>, with no proxies defined. This is done using similar steps to setting up a <a class="reference external" href="http://www.voidspace.org.uk/python/articles/authentication.shtml">Basic Authentication</a> handler :</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">proxy_support</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">ProxyHandler</span><span class="p">({})</span> <span class="gp">>>> </span><span class="n">opener</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">proxy_support</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">urllib2</span><span class="o">.</span><span class="n">install_opener</span><span class="p">(</span><span class="n">opener</span><span class="p">)</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Currently <tt class="docutils literal"><span class="pre">urllib2</span></tt> <em>does not</em> support fetching of <tt class="docutils literal"><span class="pre">https</span></tt> locations through a proxy. However, this can be enabled by extending urllib2 as shown in the recipe <a class="footnote-reference" href="#id15" id="id8">[7]</a>.</p> </div> </div> <div class="section" id="sockets-and-layers"> <h2>Sockets and Layers<a class="headerlink" href="#sockets-and-layers" title="Permalink to this headline">¶</a></h2> <p>The Python support for fetching resources from the web is layered. urllib2 uses the httplib library, which in turn uses the socket library.</p> <p>As of Python 2.3 you can specify how long a socket should wait for a response before timing out. This can be useful in applications which have to fetch web pages. By default the socket module has <em>no timeout</em> and can hang. Currently, the socket timeout is not exposed at the httplib or urllib2 levels. However, you can set the default timeout globally for all sockets using</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">socket</span> <span class="kn">import</span> <span class="nn">urllib2</span> <span class="c"># timeout in seconds</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">10</span> <span class="n">socket</span><span class="o">.</span><span class="n">setdefaulttimeout</span><span class="p">(</span><span class="n">timeout</span><span class="p">)</span> <span class="c"># this call to urllib2.urlopen now uses the default timeout</span> <span class="c"># we have set in the socket module</span> <span class="n">req</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="s">'http://www.voidspace.org.uk'</span><span class="p">)</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span> </pre></div> </div> </div> <hr class="docutils" /> <div class="section" id="footnotes"> <h2>Footnotes<a class="headerlink" href="#footnotes" title="Permalink to this headline">¶</a></h2> <p>This document was reviewed and revised by John Lee.</p> <table class="docutils footnote" frame="void" id="id9" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>For an introduction to the CGI protocol see <a class="reference external" href="http://www.pyzine.com/Issue008/Section_Articles/article_CGIOne.html">Writing Web Applications in Python</a>.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id10" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>Like Google for example. The <em>proper</em> way to use google from a program is to use <a class="reference external" href="http://pygoogle.sourceforge.net">PyGoogle</a> of course. See <a class="reference external" href="http://www.voidspace.org.uk/python/recipebook.shtml#google">Voidspace Google</a> for some examples of using the Google API.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id11" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td>Browser sniffing is a very bad practise for website design - building sites using web standards is much more sensible. Unfortunately a lot of sites still send different versions to different browsers.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id12" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>The user agent for MSIE 6 is <em>‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)’</em></td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id13" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id5">[5]</a></td><td>For details of more HTTP request headers, see <a class="reference external" href="http://www.cs.tut.fi/~jkorpela/http.html">Quick Reference to HTTP Headers</a>.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id14" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id7">[6]</a></td><td>In my case I have to use a proxy to access the internet at work. If you attempt to fetch <em>localhost</em> URLs through this proxy it blocks them. IE is set to use the proxy, which urllib2 picks up on. In order to test scripts with a localhost server, I have to prevent urllib2 from using the proxy.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id15" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id8">[7]</a></td><td>urllib2 opener for SSL proxy (CONNECT method): <a class="reference external" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456195">ASPN Cookbook Recipe</a>.</td></tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">HOWTO Fetch Internet Resources Using urllib2</a><ul> <li><a class="reference internal" href="#introduction">Introduction</a></li> <li><a class="reference internal" href="#fetching-urls">Fetching URLs</a><ul> <li><a class="reference internal" href="#data">Data</a></li> <li><a class="reference internal" href="#headers">Headers</a></li> </ul> </li> <li><a class="reference internal" href="#handling-exceptions">Handling Exceptions</a><ul> <li><a class="reference internal" href="#urlerror">URLError</a></li> <li><a class="reference internal" href="#httperror">HTTPError</a><ul> <li><a class="reference internal" href="#error-codes">Error Codes</a></li> </ul> </li> <li><a class="reference internal" href="#wrapping-it-up">Wrapping it Up</a><ul> <li><a class="reference internal" href="#number-1">Number 1</a></li> <li><a class="reference internal" href="#number-2">Number 2</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#info-and-geturl">info and geturl</a></li> <li><a class="reference internal" href="#openers-and-handlers">Openers and Handlers</a></li> <li><a class="reference internal" href="#id6">Basic Authentication</a></li> <li><a class="reference internal" href="#proxies">Proxies</a></li> <li><a class="reference internal" href="#sockets-and-layers">Sockets and Layers</a></li> <li><a class="reference internal" href="#footnotes">Footnotes</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="unicode.html" title="previous chapter">Unicode HOWTO</a></p> <h4>Next topic</h4> <p class="topless"><a href="webservers.html" title="next chapter">HOWTO Use Python in the web</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/howto/urllib2.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="webservers.html" title="HOWTO Use Python in the web" >next</a> |</li> <li class="right" > <a href="unicode.html" title="Unicode HOWTO" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >Python HOWTOs</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close