Difference between revisions of "Python - urllib"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
= References =


* http://www.voidspace.org.uk/python/articles/urllib2.shtml
Article from - http://www.voidspace.org.uk/python/articles/urllib2.shtml




= URLLIB Tutorial: urllib2 - The Missing Manual =
= URLLIB Tutorial: urllib2 - The Missing Manual =


== HOWTO Fetch Internet Resources with Python ==
== HOWTO Fetch Internet Resources with Python ==
Line 42: Line 40:
= Fetching URLs =
= Fetching URLs =


The simplest way to use urllib2 is as follows :
The simplest way to use urllib2 is as follows:


<div class="pysrc"><span class="pykeyword">import</span> <span class="pytext">urllib2</span><br /><span class="pytext">response</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">urlopen</span><span class="pyoperator">(</span><span class="pystring">'http://python.org/'</span><span class="pyoperator">)</span><br /><span class="pytext">html</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">response</span><span class="pyoperator">.</span><span class="pytext">read</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div>
<pre>
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
</pre>


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.
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.
Line 50: Line 52:
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 .read() on the response :
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 .read() on the response :


<div class="pysrc"><span class="pykeyword">import</span> <span class="pytext">urllib2</span><br /><br /><span class="pytext">req</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">Request</span><span class="pyoperator">(</span><span class="pystring">'http://www.voidspace.org.uk'</span><span class="pyoperator">)</span><br /><span class="pytext">response</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">urlopen</span><span class="pyoperator">(</span><span class="pytext">req</span><span class="pyoperator">)</span><br /><span class="pytext">the_page</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">response</span><span class="pyoperator">.</span><span class="pytext">read</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div>
<pre>
import urllib2
 
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)
the_page = response.read()
 
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 :
req = urllib2.Request('ftp://example.com/')
</pre>


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 :
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 :


<div class="pysrc"><span class="pytext">req</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">Request</span><span class="pyoperator">(</span><span class="pystring">'ftp://example.com/'</span><span class="pyoperator">)</span><span class="pytext"></span></div>
<pre>
req = urllib2.Request('ftp://example.com/')
</pre>


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") <i>about</i> 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.
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") <i>about</i> 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.
Line 62: Line 75:
== Data ==
== Data ==


Sometimes you want to send data to a URL (often the URL will refer to a CGI (Common Gateway Interface) script [http://esbinfo:8090/pages/editpage.action#id8 ][1] or other web application). With HTTP, this is often done using what's known as a <b>POST</b> 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 <i>not</i> from <tt class="docutils literal"><span class="pre">urllib2</span></tt>.
Sometimes you want to send data to a URL (often the URL will refer to a CGI (Common Gateway Interface) script or other web application). With HTTP, this is often done using what's known as a <b>POST</b> 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 <i>not</i> from <tt class="docutils literal"><span class="pre">urllib2</span></tt>.
 
<pre>
import urllib
import urllib2
 
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
 
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
</pre>


<div class="pysrc"><span class="pykeyword">import</span> <span class="pytext">urllib</span><br /><span class="pykeyword">import</span> <span class="pytext">urllib2</span><br /><br /><span class="pytext">url</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pystring">'http://www.someserver.com/cgi-bin/register.cgi'</span><br /><span class="pytext">values</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pyoperator">{</span><span class="pystring">'name'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Michael Foord'</span><span class="pyoperator">,</span><br /><span class="pystring">'location'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Northampton'</span><span class="pyoperator">,</span><br /><span class="pystring">'language'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Python'</span> <span class="pyoperator">}</span><br /><br /><span class="pytext">data</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib</span><span class="pyoperator">.</span><span class="pytext">urlencode</span><span class="pyoperator">(</span><span class="pytext">values</span><span class="pyoperator">)</span><br /><span class="pytext">req</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">Request</span><span class="pyoperator">(</span><span class="pytext">url</span><span class="pyoperator">,</span> <span class="pytext">data</span><span class="pyoperator">)</span><br /><span class="pytext">response</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">urlopen</span><span class="pyoperator">(</span><span class="pytext">req</span><span class="pyoperator">)</span><br /><span class="pytext">the_page</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">response</span><span class="pyoperator">.</span><span class="pytext">read</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div>


Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see [http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13 HTML Specification, Form Submission] for more details).
Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see [http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13 HTML Specification, Form Submission] for more details).
Line 82: Line 109:
We'll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request.
We'll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request.


Some websites [http://esbinfo:8090/pages/editpage.action#id9 ][2] dislike being browsed by programs, or send different versions to different browsers [http://esbinfo:8090/pages/editpage.action#id10 ][3] . 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 [http://esbinfo:8090/pages/editpage.action#id11 ][4]. 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 [http://esbinfo:8090/pages/editpage.action#id12 ][5].
Some websites dislike being browsed by programs, or send different versions to different browsers. 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. 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.
 
<pre>
import urllib
import urllib2
 
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
 
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
</pre>


<div class="pysrc"><span class="pykeyword">import</span> <span class="pytext">urllib</span><br /><span class="pykeyword">import</span> <span class="pytext">urllib2</span><br /><br /><span class="pytext">url</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pystring">'http://www.someserver.com/cgi-bin/register.cgi'</span><br /><span class="pytext">user_agent</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pystring">'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'</span><br /><span class="pytext">values</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pyoperator">{</span><span class="pystring">'name'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Michael Foord'</span><span class="pyoperator">,</span><br /><span class="pystring">'location'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Northampton'</span><span class="pyoperator">,</span><br /><span class="pystring">'language'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pystring">'Python'</span> <span class="pyoperator">}</span><br /><span class="pytext">headers</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pyoperator">{</span> <span class="pystring">'User-Agent'</span> <span class="pyoperator"><nowiki>:</nowiki></span> <span class="pytext">user_agent</span> <span class="pyoperator">}</span><br /><br /><span class="pytext">data</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib</span><span class="pyoperator">.</span><span class="pytext">urlencode</span><span class="pyoperator">(</span><span class="pytext">values</span><span class="pyoperator">)</span><br /><span class="pytext">req</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">Request</span><span class="pyoperator">(</span><span class="pytext">url</span><span class="pyoperator">,</span> <span class="pytext">data</span><span class="pyoperator">,</span> <span class="pytext">headers</span><span class="pyoperator">)</span><br /><span class="pytext">response</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urllib2</span><span class="pyoperator">.</span><span class="pytext">urlopen</span><span class="pyoperator">(</span><span class="pytext">req</span><span class="pyoperator">)</span><br /><span class="pytext">the_page</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">response</span><span class="pyoperator">.</span><span class="pytext">read</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div>


The response also has two useful methods. See the section on [http://esbinfo:8090/pages/editpage.action#info-and-geturl info and geturl] which comes after we have a look at what happens when things go wrong.
The response also has two useful methods. See the section on which comes after we have a look at what happens when things go wrong.


</div></div><div class="section">
</div></div><div class="section">
Line 123: Line 166:


<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 :
<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 :
<pre>
# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols', 'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted', 'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices', 'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified', 'Document has not changed since given time'),
305: ('Use Proxy', 'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect', 'Object moved temporarily -- see URI list'),
400: ('Bad Request', 'Bad request syntax or unsupported method'),
401: ('Unauthorized', 'No permission -- see authorization schemes'),
402: ('Payment Required', 'No payment -- see charging schemes'),
403: ('Forbidden', 'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed', 'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone', 'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable', 'Cannot satisfy request range.'),
417: ('Expectation Failed', 'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented','Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable', 'The server cannot process the request due to a high load'),
504: ('Gateway Timeout', 'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
</pre>


<div class="pysrc"><span class="pycomment"><nowiki># Table mapping response codes to messages; entries have the</nowiki><br /></span><span class="pycomment"><nowiki># form {code: (shortmessage, longmessage)}.</nowiki><br /></span><span class="pytext">responses</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pyoperator">{</span><br /><span class="pynumber">100</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Continue'</span><span class="pyoperator">,</span> <span class="pystring">'Request received, please continue'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">101</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Switching Protocols'</span><span class="pyoperator">,</span><br /><span class="pystring">'Switching to new protocol; obey Upgrade header'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">200</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'OK'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled, document follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">201</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Created'</span><span class="pyoperator">,</span> <span class="pystring">'Document created, URL follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">202</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Accepted'</span><span class="pyoperator">,</span><br /><span class="pystring">'Request accepted, processing continues off-line'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">203</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Non-Authoritative Information'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled from cache'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">204</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'No Content'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled, nothing follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">205</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Reset Content'</span><span class="pyoperator">,</span> <span class="pystring">'Clear input form for further input.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">206</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Partial Content'</span><span class="pyoperator">,</span> <span class="pystring">'Partial content follows.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">300</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Multiple Choices'</span><span class="pyoperator">,</span><br /><span class="pystring">'Object has several resources -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">301</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Moved Permanently'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved permanently -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">302</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Found'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved temporarily -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">303</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'See Other'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved -- see Method and URL list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">304</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Modified'</span><span class="pyoperator">,</span><br /><span class="pystring">'Document has not changed since given time'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">305</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Use Proxy'</span><span class="pyoperator">,</span><br /><span class="pystring">'You must use proxy specified in Location to access this '</span><br /><span class="pystring">'resource.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">307</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Temporary Redirect'</span><span class="pyoperator">,</span><br /><span class="pystring">'Object moved temporarily -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">400</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Bad Request'</span><span class="pyoperator">,</span><br /><span class="pystring">'Bad request syntax or unsupported method'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">401</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Unauthorized'</span><span class="pyoperator">,</span><br /><span class="pystring">'No permission -- see authorization schemes'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">402</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Payment Required'</span><span class="pyoperator">,</span><br /><span class="pystring">'No payment -- see charging schemes'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">403</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Forbidden'</span><span class="pyoperator">,</span><br /><span class="pystring">'Request forbidden -- authorization will not help'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">404</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Found'</span><span class="pyoperator">,</span> <span class="pystring">'Nothing matches the given URI'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">405</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Method Not Allowed'</span><span class="pyoperator">,</span><br /><span class="pystring">'Specified method is invalid for this server.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">406</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Acceptable'</span><span class="pyoperator">,</span> <span class="pystring">'URI not available in preferred format.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">407</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Proxy Authentication Required'</span><span class="pyoperator">,</span> <span class="pystring">'You must authenticate with '</span><br /><span class="pystring">'this proxy before proceeding.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">408</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request Timeout'</span><span class="pyoperator">,</span> <span class="pystring">'Request timed out; try again later.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">409</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Conflict'</span><span class="pyoperator">,</span> <span class="pystring">'Request conflict.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">410</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Gone'</span><span class="pyoperator">,</span><br /><span class="pystring">'URI no longer exists and has been permanently removed.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">411</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Length Required'</span><span class="pyoperator">,</span> <span class="pystring">'Client must specify Content-Length.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">412</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Precondition Failed'</span><span class="pyoperator">,</span> <span class="pystring">'Precondition in headers is false.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">413</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request Entity Too Large'</span><span class="pyoperator">,</span> <span class="pystring">'Entity is too large.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">414</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request-URI Too Long'</span><span class="pyoperator">,</span> <span class="pystring">'URI is too long.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">415</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Unsupported Media Type'</span><span class="pyoperator">,</span> <span class="pystring">'Entity body in unsupported format.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">416</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Requested Range Not Satisfiable'</span><span class="pyoperator">,</span><br /><span class="pystring">'Cannot satisfy request range.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">417</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Expectation Failed'</span><span class="pyoperator">,</span><br /><span class="pystring">'Expect condition could not be satisfied.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">500</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Internal Server Error'</span><span class="pyoperator">,</span> <span class="pystring">'Server got itself in trouble'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">501</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Implemented'</span><span class="pyoperator">,</span><br /><span class="pystring">'Server does not support this operation'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">502</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Bad Gateway'</span><span class="pyoperator">,</span> <span class="pystring">'Invalid responses from another server/proxy.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">503</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Service Unavailable'</span><span class="pyoperator">,</span><br /><span class="pystring">'The server cannot process the request due to a high load'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">504</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Gateway Timeout'</span><span class="pyoperator">,</span><br /><span class="pystring">'The gateway server did not receive a timely response'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">505</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'HTTP Version Not Supported'</span><span class="pyoperator">,</span> <span class="pystring">'Cannot fulfill request.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pyoperator">}</span><span class="pytext"></span></div>
<div class="pysrc"><span class="pycomment"><nowiki># Table mapping response codes to messages; entries have the</nowiki><br /></span><span class="pycomment"><nowiki># form {code: (shortmessage, longmessage)}.</nowiki><br /></span><span class="pytext">responses</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pyoperator">{</span><br /><span class="pynumber">100</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Continue'</span><span class="pyoperator">,</span> <span class="pystring">'Request received, please continue'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">101</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Switching Protocols'</span><span class="pyoperator">,</span><br /><span class="pystring">'Switching to new protocol; obey Upgrade header'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">200</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'OK'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled, document follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">201</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Created'</span><span class="pyoperator">,</span> <span class="pystring">'Document created, URL follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">202</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Accepted'</span><span class="pyoperator">,</span><br /><span class="pystring">'Request accepted, processing continues off-line'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">203</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Non-Authoritative Information'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled from cache'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">204</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'No Content'</span><span class="pyoperator">,</span> <span class="pystring">'Request fulfilled, nothing follows'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">205</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Reset Content'</span><span class="pyoperator">,</span> <span class="pystring">'Clear input form for further input.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">206</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Partial Content'</span><span class="pyoperator">,</span> <span class="pystring">'Partial content follows.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">300</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Multiple Choices'</span><span class="pyoperator">,</span><br /><span class="pystring">'Object has several resources -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">301</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Moved Permanently'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved permanently -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">302</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Found'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved temporarily -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">303</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'See Other'</span><span class="pyoperator">,</span> <span class="pystring">'Object moved -- see Method and URL list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">304</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Modified'</span><span class="pyoperator">,</span><br /><span class="pystring">'Document has not changed since given time'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">305</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Use Proxy'</span><span class="pyoperator">,</span><br /><span class="pystring">'You must use proxy specified in Location to access this '</span><br /><span class="pystring">'resource.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">307</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Temporary Redirect'</span><span class="pyoperator">,</span><br /><span class="pystring">'Object moved temporarily -- see URI list'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">400</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Bad Request'</span><span class="pyoperator">,</span><br /><span class="pystring">'Bad request syntax or unsupported method'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">401</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Unauthorized'</span><span class="pyoperator">,</span><br /><span class="pystring">'No permission -- see authorization schemes'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">402</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Payment Required'</span><span class="pyoperator">,</span><br /><span class="pystring">'No payment -- see charging schemes'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">403</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Forbidden'</span><span class="pyoperator">,</span><br /><span class="pystring">'Request forbidden -- authorization will not help'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">404</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Found'</span><span class="pyoperator">,</span> <span class="pystring">'Nothing matches the given URI'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">405</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Method Not Allowed'</span><span class="pyoperator">,</span><br /><span class="pystring">'Specified method is invalid for this server.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">406</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Acceptable'</span><span class="pyoperator">,</span> <span class="pystring">'URI not available in preferred format.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">407</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Proxy Authentication Required'</span><span class="pyoperator">,</span> <span class="pystring">'You must authenticate with '</span><br /><span class="pystring">'this proxy before proceeding.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">408</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request Timeout'</span><span class="pyoperator">,</span> <span class="pystring">'Request timed out; try again later.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">409</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Conflict'</span><span class="pyoperator">,</span> <span class="pystring">'Request conflict.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">410</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Gone'</span><span class="pyoperator">,</span><br /><span class="pystring">'URI no longer exists and has been permanently removed.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">411</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Length Required'</span><span class="pyoperator">,</span> <span class="pystring">'Client must specify Content-Length.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">412</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Precondition Failed'</span><span class="pyoperator">,</span> <span class="pystring">'Precondition in headers is false.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">413</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request Entity Too Large'</span><span class="pyoperator">,</span> <span class="pystring">'Entity is too large.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">414</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Request-URI Too Long'</span><span class="pyoperator">,</span> <span class="pystring">'URI is too long.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">415</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Unsupported Media Type'</span><span class="pyoperator">,</span> <span class="pystring">'Entity body in unsupported format.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">416</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Requested Range Not Satisfiable'</span><span class="pyoperator">,</span><br /><span class="pystring">'Cannot satisfy request range.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">417</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Expectation Failed'</span><span class="pyoperator">,</span><br /><span class="pystring">'Expect condition could not be satisfied.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><br /><span class="pynumber">500</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Internal Server Error'</span><span class="pyoperator">,</span> <span class="pystring">'Server got itself in trouble'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">501</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Not Implemented'</span><span class="pyoperator">,</span><br /><span class="pystring">'Server does not support this operation'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">502</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Bad Gateway'</span><span class="pyoperator">,</span> <span class="pystring">'Invalid responses from another server/proxy.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">503</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Service Unavailable'</span><span class="pyoperator">,</span><br /><span class="pystring">'The server cannot process the request due to a high load'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">504</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'Gateway Timeout'</span><span class="pyoperator">,</span><br /><span class="pystring">'The gateway server did not receive a timely response'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pynumber">505</span><span class="pyoperator"><nowiki>:</nowiki></span> <span class="pyoperator">(</span><span class="pystring">'HTTP Version Not Supported'</span><span class="pyoperator">,</span> <span class="pystring">'Cannot fulfill request.'</span><span class="pyoperator">)</span><span class="pyoperator">,</span><br /><span class="pyoperator">}</span><span class="pytext"></span></div>
Line 141: Line 237:
=== Number 1 ===
=== Number 1 ===


<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">urllib2</span> <span class="pykeyword">import</span> <span class="pytext">Request</span><span class="pyoperator">,</span> <span class="pytext">urlopen</span><span class="pyoperator">,</span> <span class="pytext">URLError</span><span class="pyoperator">,</span> <span class="pytext">HTTPError</span><br /><span class="pytext">req</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">Request</span><span class="pyoperator">(</span><span class="pytext">someurl</span><span class="pyoperator">)</span><br /><span class="pykeyword">try</span><span class="pyoperator"><nowiki>:</nowiki></span><br /><span class="pytext">response</span> <span class="pyoperator"><nowiki>=</nowiki></span> <span class="pytext">urlopen</span><span class="pyoperator">(</span><span class="pytext">req</span><span class="pyoperator">)</span><br /><span class="pykeyword">except</span> <span class="pytext">HTTPError</span><span class="pyoperator">,</span> <span class="pytext">e</span><span class="pyoperator"><nowiki>:</nowiki></span><br /><span class="pykeyword">print</span> <span class="pystring">'The server couldn't fulfill the request.'</span><br /><span class="pykeyword">print</span> <span class="pystring">'Error code: '</span><span class="pyoperator">,</span> <span class="pytext">e</span><span class="pyoperator">.</span><span class="pytext">code</span><br /><span class="pykeyword">except</span> <span class="pytext">URLError</span><span class="pyoperator">,</span> <span class="pytext">e</span><span class="pyoperator"><nowiki>:</nowiki></span><br /><span class="pykeyword">print</span> <span class="pystring">'We failed to reach a server.'</span><br /><span class="pykeyword">print</span> <span class="pystring">'Reason: '</span><span class="pyoperator">,</span> <span class="pytext">e</span><span class="pyoperator">.</span><span class="pytext">reason</span><br /><span class="pykeyword">else</span><span class="pyoperator"><nowiki>:</nowiki></span><br /><span class="pycomment"><nowiki># everything is fine</nowiki></span><span class="pytext"></span></div><div class="note">
<pre>
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
response = urlopen(req)
except HTTPError, e:
print 'The server couldn't fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
else:
# everything is fine
</pre>
 


====Note====
====Note====
Line 275: Line 385:
= Proxies =
= Proxies =


<b>urllib2</b> 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. Normally that's a good thing, but there are occasions when it may not be helpful [http://esbinfo:8090/pages/editpage.action#id13 ][6]. 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 [http://www.voidspace.org.uk/python/articles/authentication.shtml Basic Authentication] handler :
<b>urllib2</b> 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. Normally that's a good thing, but there are occasions when it may not be helpful. 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 [http://www.voidspace.org.uk/python/articles/authentication.shtml Basic Authentication] handler :


  &gt;&gt;&gt; proxy_support = urllib2.ProxyHandler({})<br />&gt;&gt;&gt; opener = urllib2.build_opener(proxy_support)<br />&gt;&gt;&gt; urllib2.install_opener(opener)<br />
  &gt;&gt;&gt; proxy_support = urllib2.ProxyHandler({})<br />&gt;&gt;&gt; opener = urllib2.build_opener(proxy_support)<br />&gt;&gt;&gt; urllib2.install_opener(opener)<br />
Line 327: Line 437:


[[Category:Python]]
[[Category:Python]]
[[Category:. httplib]]
[[Category:Python httplib]]
[[Category:. urllib]]
[[Category:Python urllib]]
[[Category:Internet]]
[[Category:Internet]]

Latest revision as of 17:48, 11 March 2017

Article from - http://www.voidspace.org.uk/python/articles/urllib2.shtml


URLLIB Tutorial: urllib2 - The Missing Manual

HOWTO Fetch Internet Resources with Python

urllib2 Tutorial

Introduction

Related Articles

You may also find useful the following articles on fetching web resources with Python :

A tutorial on Basic Authentication, with examples in Python.

  • [/python/articles/cookielib.shtml cookielib and ClientCookie]

How to handle cookies when fetching web-pages with urllib2.



urllib2 is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen 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.

urllib2 supports fetching URLs for many "URL schemes" (identified by the string before the ":" in URL - for example "ftp" is the URL scheme of "ftp://python.org/") using their associated network protocols (e.g. FTP, HTTP). This tutorial focuses on the most common case, HTTP.

For straightforward situations urlopen 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 RFC 2616. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib2, with enough detail about HTTP to help you through. It is not intended to replace the urllib2 docs , but is supplementary to them.


Fetching URLs

The simplest way to use urllib2 is as follows:

import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()

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.

HTTP is based on requests and responses - the client makes requests and servers send responses. urllib2 mirrors this with a Request 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 urlopen 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 .read() on the response :

import urllib2

req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)
the_page = response.read()

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 :
req = urllib2.Request('ftp://example.com/')

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 :

req = urllib2.Request('ftp://example.com/')

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") about 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.

Data

Sometimes you want to send data to a URL (often the URL will refer to a CGI (Common Gateway Interface) script or other web application). With HTTP, this is often done using what's known as a POST 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 data argument. The encoding is done using a function from the urllib library not from urllib2.

import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()


Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see HTML Specification, Form Submission for more details).

If you do not pass the data argument, urllib2 uses a GET 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 always cause side-effects, and GET requests never 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.

This is done as follows.

>>> import urllib2
>>> import urllib

>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.urlencode(data)
>>> print url_values
name=Somebody+Here&language=Python&location=Northampton
>>> url = 'http://www.example.com/example.cgi'
>>> full_url = url + '?' + url_values

>>> data = urllib2.open(full_url)

Notice that the full URL is created by adding a ? to the URL, followed by the encoded values.

Headers

We'll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request.

Some websites dislike being browsed by programs, or send different versions to different browsers. By default urllib2 identifies itself as Python-urllib/x.y (where x and y are the major and minor version numbers of the Python release, e.g. Python-urllib/2.5), which may confuse the site, or just plain not work. The way a browser identifies itself is through the User-Agent header. 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.

import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }

data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()


The response also has two useful methods. See the section on which comes after we have a look at what happens when things go wrong.

Handling Exceptions

urlopen raises URLError when it cannot handle a response (though as usual with Python APIs, builtin exceptions such as ValueError, TypeError etc. may also be raised).

HTTPError is the subclass of URLError raised in the specific case of HTTP URLs.

URLError

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.

e.g.

>>> req = urllib2.Request('http://www.pretend_server.org')
>>> try: urllib2.urlopen(req)
>>> except URLError, e:
>>> print e.reason
>>>

(4, 'getaddrinfo failed')

HTTPError

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 HTTPError. Typical errors include '404' (page not found), '403' (request forbidden), and '401' (authentication required).

See section 10 of RFC 2616 for a reference on all the HTTP error codes.

The HTTPError instance raised will have an integer 'code' attribute, which corresponds to the error sent by the server.

Error Codes

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.

BaseHTTPServer.BaseHTTPRequestHandler.responses 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 :

# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols', 'Switching to new protocol; obey Upgrade header'),

200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted', 'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),

300: ('Multiple Choices', 'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified', 'Document has not changed since given time'),
305: ('Use Proxy', 'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect', 'Object moved temporarily -- see URI list'),

400: ('Bad Request', 'Bad request syntax or unsupported method'),
401: ('Unauthorized', 'No permission -- see authorization schemes'),
402: ('Payment Required', 'No payment -- see charging schemes'),
403: ('Forbidden', 'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed', 'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone', 'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable', 'Cannot satisfy request range.'),
417: ('Expectation Failed', 'Expect condition could not be satisfied.'),

500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented','Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable', 'The server cannot process the request due to a high load'),
504: ('Gateway Timeout', 'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),

200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),

300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),

400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),

500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}

When an error is raised the server responds by returning an HTTP error code and an error page. You can use the HTTPError 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.

>>> req = urllib2.Request('http://www.python.org/fish.html')
>>> try:
>>> urllib2.urlopen(req)
>>> except URLError, e:

>>> print e.code
>>> print e.read()
>>>
404

"http://www.w3.org/TR/html4/loose.dtd">
type="text/css"?>


...... etc...

Wrapping it Up

So if you want to be prepared for HTTPError or URLError there are two basic approaches. I prefer the second approach.

Number 1

from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
response = urlopen(req)
except HTTPError, e:
print 'The server couldn't fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
else:
# everything is fine


Note

The except HTTPError must come first, otherwise except URLError will also catch an HTTPError.

Number 2

from urllib2 import Request, urlopen, URLError
req = Request(someurl)
try:
response = urlopen(req)
except URLError, 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

Note

URLError is a subclass of the built-in exception IOError.

This means that you can avoid importing URLError and use :

from urllib2 import Request, urlopen
req = Request(someurl)
try:
response = urlopen(req)
except IOError, 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

Under rare circumstances urllib2 can raise socket.error.

info and geturl

The response returned by urlopen (or the HTTPError instance) has two useful methods info and geturl.

geturl - this returns the real URL of the page fetched. This is useful because urlopen (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.

info - this returns a dictionary-like object that describes the page fetched, particularly the headers sent by the server. It is currently an httplib.HTTPMessage instance.

Typical headers include 'Content-length', 'Content-type', and so on. See the Quick Reference to HTTP Headers for a useful listing of HTTP headers with brief explanations of their meaning and use.

Openers and Handlers

When you fetch a URL you use an opener (an instance of the perhaps confusingly-named urllib2.OpenerDirector). Normally we have been using the default opener - via urlopen - 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.

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.

To create an opener, instantiate an OpenerDirector, and then call .add_handler(some_handler_instance) repeatedly.

Alternatively, you can use build_opener, which is a convenience function for creating opener objects with a single function call. build_opener adds several handlers by default, but provides a quick way to add more and/or override the default handlers.

Other sorts of handlers you might want to can handle proxies, authentication, and other common but slightly specialised situations.

install_opener can be used to make an opener object the (global) default opener. This means that calls to urlopen will use the opener you have installed.

Opener objects have an open method, which can be called directly to fetch urls in the same way as the urlopen function: there's no need to call install_opener, except as a convenience.

Basic Authentication

To illustrate creating and installing a handler we will use the HTTPBasicAuthHandler. For a more detailed discussion of this subject - including an explanation of how Basic Authentication works - see the Basic Authentication Tutorial.

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 : Www-authenticate: SCHEME realm="REALM".

e.g.

Www-authenticate: Basic realm="cPanel Users"

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 HTTPBasicAuthHandler and an opener to use this handler.


The HTTPBasicAuthHandler 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 HTTPPasswordMgr. Frequently one doesn't care what the realm is. In that case, it is convenient to use HTTPPasswordMgrWithDefaultRealm. 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 None as the realm argument to the add_password method.

The top-level URL is the first URL that requires authentication. URLs "deeper" than the URL you pass to .add_password() will also match.

# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(a_url)

# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)


Note

In the above example we only supplied our HHTPBasicAuthHandler to build_opener. By default openers have the handlers for normal situations - ProxyHandler, UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, HTTPErrorProcessor.

top_level_url is in fact either a full URL (including the 'http:' scheme component and the hostname and optionally the port number) e.g. "http://example.com/" or 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:example.com" is not correct.

Proxies

urllib2 will auto-detect your proxy settings and use those. This is through the ProxyHandler which is part of the normal handler chain. Normally that's a good thing, but there are occasions when it may not be helpful. One way to do this is to setup our own ProxyHandler, with no proxies defined. This is done using similar steps to setting up a Basic Authentication handler :

>>> proxy_support = urllib2.ProxyHandler({})
>>> opener = urllib2.build_opener(proxy_support)
>>> urllib2.install_opener(opener)

Note

Currently urllib2 does not support fetching of https locations through a proxy. This can be a problem.

Sockets and Layers

The Python support for fetching resources from the web is layered. urllib2 uses the httplib library, which in turn uses the socket library.

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 no timeout 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 :

import socket
import urllib2

# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# this call to urllib2.urlopen now uses the default timeout
# we have set in the socket module
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)

Footnotes

This document was reviewed and revised by John Lee.

  • Like Google for example. The proper way to use google from a program is to use PyGoogle of course. See Voidspace Google for some examples of using the Google API.
  • 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.


  • The user agent for MSIE 6 is 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)'


  • In my case I have to use a proxy to access the internet at work. If you attempt to fetch localhost 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.