blob: e57f9e53e3ef2f67b28a8cb8d5baa2d8b4d03604 [file] [log] [blame]
Georg Brandl0f7ede42008-06-23 11:23:31 +00001***********************************************************
2 HOWTO Fetch Internet Resources Using The urllib Package
3***********************************************************
Georg Brandl116aa622007-08-15 14:28:22 +00004
5:Author: `Michael Foord <http://www.voidspace.org.uk/python/index.shtml>`_
6
7.. note::
8
Georg Brandl0f7ede42008-06-23 11:23:31 +00009 There is a French translation of an earlier revision of this
Georg Brandl116aa622007-08-15 14:28:22 +000010 HOWTO, available at `urllib2 - Le Manuel manquant
Christian Heimesdd15f6c2008-03-16 00:07:10 +000011 <http://www.voidspace.org.uk/python/articles/urllib2_francais.shtml>`_.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl48310cd2009-01-03 21:18:54 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014
15Introduction
16============
17
18.. sidebar:: Related Articles
19
20 You may also find useful the following article on fetching web resources
Georg Brandl0f7ede42008-06-23 11:23:31 +000021 with Python:
Georg Brandl48310cd2009-01-03 21:18:54 +000022
Georg Brandl116aa622007-08-15 14:28:22 +000023 * `Basic Authentication <http://www.voidspace.org.uk/python/articles/authentication.shtml>`_
Georg Brandl48310cd2009-01-03 21:18:54 +000024
Georg Brandl116aa622007-08-15 14:28:22 +000025 A tutorial on *Basic Authentication*, with examples in Python.
26
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000027**urllib.request** is a `Python <http://www.python.org>`_ module for fetching URLs
Georg Brandl116aa622007-08-15 14:28:22 +000028(Uniform Resource Locators). It offers a very simple interface, in the form of
29the *urlopen* function. This is capable of fetching URLs using a variety of
30different protocols. It also offers a slightly more complex interface for
31handling common situations - like basic authentication, cookies, proxies and so
32on. These are provided by objects called handlers and openers.
33
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000034urllib.request supports fetching URLs for many "URL schemes" (identified by the string
Georg Brandl116aa622007-08-15 14:28:22 +000035before the ":" in URL - for example "ftp" is the URL scheme of
36"ftp://python.org/") using their associated network protocols (e.g. FTP, HTTP).
37This tutorial focuses on the most common case, HTTP.
38
39For straightforward situations *urlopen* is very easy to use. But as soon as you
40encounter errors or non-trivial cases when opening HTTP URLs, you will need some
41understanding of the HyperText Transfer Protocol. The most comprehensive and
42authoritative reference to HTTP is :rfc:`2616`. This is a technical document and
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000043not intended to be easy to read. This HOWTO aims to illustrate using *urllib*,
Georg Brandl116aa622007-08-15 14:28:22 +000044with enough detail about HTTP to help you through. It is not intended to replace
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000045the :mod:`urllib.request` docs, but is supplementary to them.
Georg Brandl116aa622007-08-15 14:28:22 +000046
47
48Fetching URLs
49=============
50
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000051The simplest way to use urllib.request is as follows::
Georg Brandl116aa622007-08-15 14:28:22 +000052
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000053 import urllib.request
54 response = urllib.request.urlopen('http://python.org/')
Georg Brandl116aa622007-08-15 14:28:22 +000055 html = response.read()
56
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000057Many uses of urllib will be that simple (note that instead of an 'http:' URL we
Georg Brandl116aa622007-08-15 14:28:22 +000058could have used an URL starting with 'ftp:', 'file:', etc.). However, it's the
59purpose of this tutorial to explain the more complicated cases, concentrating on
60HTTP.
61
62HTTP is based on requests and responses - the client makes requests and servers
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000063send responses. urllib.request mirrors this with a ``Request`` object which represents
Georg Brandl116aa622007-08-15 14:28:22 +000064the HTTP request you are making. In its simplest form you create a Request
65object that specifies the URL you want to fetch. Calling ``urlopen`` with this
66Request object returns a response object for the URL requested. This response is
67a file-like object, which means you can for example call ``.read()`` on the
68response::
69
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000070 import urllib.request
Georg Brandl116aa622007-08-15 14:28:22 +000071
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000072 req = urllib.request.Request('http://www.voidspace.org.uk')
73 response = urllib.request.urlopen(req)
Georg Brandl116aa622007-08-15 14:28:22 +000074 the_page = response.read()
75
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000076Note that urllib.request makes use of the same Request interface to handle all URL
Georg Brandl116aa622007-08-15 14:28:22 +000077schemes. For example, you can make an FTP request like so::
78
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000079 req = urllib.request.Request('ftp://example.com/')
Georg Brandl116aa622007-08-15 14:28:22 +000080
81In the case of HTTP, there are two extra things that Request objects allow you
82to do: First, you can pass data to be sent to the server. Second, you can pass
83extra information ("metadata") *about* the data or the about request itself, to
84the server - this information is sent as HTTP "headers". Let's look at each of
85these in turn.
86
87Data
88----
89
90Sometimes you want to send data to a URL (often the URL will refer to a CGI
91(Common Gateway Interface) script [#]_ or other web application). With HTTP,
92this is often done using what's known as a **POST** request. This is often what
93your browser does when you submit a HTML form that you filled in on the web. Not
94all POSTs have to come from forms: you can use a POST to transmit arbitrary data
95to your own application. In the common case of HTML forms, the data needs to be
96encoded in a standard way, and then passed to the Request object as the ``data``
Georg Brandl0f7ede42008-06-23 11:23:31 +000097argument. The encoding is done using a function from the :mod:`urllib.parse`
98library. ::
Georg Brandl116aa622007-08-15 14:28:22 +000099
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000100 import urllib.parse
Georg Brandl48310cd2009-01-03 21:18:54 +0000101 import urllib.request
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103 url = 'http://www.someserver.com/cgi-bin/register.cgi'
104 values = {'name' : 'Michael Foord',
105 'location' : 'Northampton',
106 'language' : 'Python' }
107
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000108 data = urllib.parse.urlencode(values)
109 req = urllib.request.Request(url, data)
110 response = urllib.request.urlopen(req)
Georg Brandl116aa622007-08-15 14:28:22 +0000111 the_page = response.read()
112
113Note that other encodings are sometimes required (e.g. for file upload from HTML
114forms - see `HTML Specification, Form Submission
115<http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13>`_ for more
116details).
117
Georg Brandl0f7ede42008-06-23 11:23:31 +0000118If you do not pass the ``data`` argument, urllib uses a **GET** request. One
Georg Brandl116aa622007-08-15 14:28:22 +0000119way in which GET and POST requests differ is that POST requests often have
120"side-effects": they change the state of the system in some way (for example by
121placing an order with the website for a hundredweight of tinned spam to be
122delivered to your door). Though the HTTP standard makes it clear that POSTs are
123intended to *always* cause side-effects, and GET requests *never* to cause
124side-effects, nothing prevents a GET request from having side-effects, nor a
125POST requests from having no side-effects. Data can also be passed in an HTTP
126GET request by encoding it in the URL itself.
127
128This is done as follows::
129
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000130 >>> import urllib.request
131 >>> import urllib.parse
Georg Brandl116aa622007-08-15 14:28:22 +0000132 >>> data = {}
133 >>> data['name'] = 'Somebody Here'
134 >>> data['location'] = 'Northampton'
135 >>> data['language'] = 'Python'
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000136 >>> url_values = urllib.parse.urlencode(data)
Georg Brandl6911e3c2007-09-04 07:15:32 +0000137 >>> print(url_values)
Georg Brandl116aa622007-08-15 14:28:22 +0000138 name=Somebody+Here&language=Python&location=Northampton
139 >>> url = 'http://www.example.com/example.cgi'
140 >>> full_url = url + '?' + url_values
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000141 >>> data = urllib.request.open(full_url)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143Notice that the full URL is created by adding a ``?`` to the URL, followed by
144the encoded values.
145
146Headers
147-------
148
149We'll discuss here one particular HTTP header, to illustrate how to add headers
150to your HTTP request.
151
152Some websites [#]_ dislike being browsed by programs, or send different versions
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000153to different browsers [#]_ . By default urllib identifies itself as
Georg Brandl116aa622007-08-15 14:28:22 +0000154``Python-urllib/x.y`` (where ``x`` and ``y`` are the major and minor version
155numbers of the Python release,
156e.g. ``Python-urllib/2.5``), which may confuse the site, or just plain
157not work. The way a browser identifies itself is through the
158``User-Agent`` header [#]_. When you create a Request object you can
159pass a dictionary of headers in. The following example makes the same
160request as above, but identifies itself as a version of Internet
161Explorer [#]_. ::
162
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000163 import urllib.parse
Georg Brandl48310cd2009-01-03 21:18:54 +0000164 import urllib.request
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166 url = 'http://www.someserver.com/cgi-bin/register.cgi'
Georg Brandl48310cd2009-01-03 21:18:54 +0000167 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
Georg Brandl116aa622007-08-15 14:28:22 +0000168 values = {'name' : 'Michael Foord',
169 'location' : 'Northampton',
170 'language' : 'Python' }
171 headers = { 'User-Agent' : user_agent }
Georg Brandl48310cd2009-01-03 21:18:54 +0000172
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000173 data = urllib.parse.urlencode(values)
174 req = urllib.request.Request(url, data, headers)
175 response = urllib.request.urlopen(req)
Georg Brandl116aa622007-08-15 14:28:22 +0000176 the_page = response.read()
177
178The response also has two useful methods. See the section on `info and geturl`_
179which comes after we have a look at what happens when things go wrong.
180
181
182Handling Exceptions
183===================
184
Benjamin Petersone5384b02008-10-04 22:00:42 +0000185*urlopen* raises :exc:`URLError` when it cannot handle a response (though as usual
Georg Brandl48310cd2009-01-03 21:18:54 +0000186with Python APIs, builtin exceptions such as
Benjamin Petersone5384b02008-10-04 22:00:42 +0000187:exc:`ValueError`, :exc:`TypeError` etc. may also
Georg Brandl116aa622007-08-15 14:28:22 +0000188be raised).
189
Benjamin Petersone5384b02008-10-04 22:00:42 +0000190:exc:`HTTPError` is the subclass of :exc:`URLError` raised in the specific case of
Georg Brandl116aa622007-08-15 14:28:22 +0000191HTTP URLs.
192
Georg Brandl0f7ede42008-06-23 11:23:31 +0000193The exception classes are exported from the :mod:`urllib.error` module.
194
Georg Brandl116aa622007-08-15 14:28:22 +0000195URLError
196--------
197
198Often, URLError is raised because there is no network connection (no route to
199the specified server), or the specified server doesn't exist. In this case, the
200exception raised will have a 'reason' attribute, which is a tuple containing an
201error code and a text error message.
202
203e.g. ::
204
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000205 >>> req = urllib.request.Request('http://www.pretend_server.org')
206 >>> try: urllib.request.urlopen(req)
207 >>> except urllib.error.URLError, e:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000208 >>> print(e.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000209 >>>
210 (4, 'getaddrinfo failed')
211
212
213HTTPError
214---------
215
216Every HTTP response from the server contains a numeric "status code". Sometimes
217the status code indicates that the server is unable to fulfil the request. The
218default handlers will handle some of these responses for you (for example, if
219the response is a "redirection" that requests the client fetch the document from
Georg Brandl0f7ede42008-06-23 11:23:31 +0000220a different URL, urllib will handle that for you). For those it can't handle,
Benjamin Petersone5384b02008-10-04 22:00:42 +0000221urlopen will raise an :exc:`HTTPError`. Typical errors include '404' (page not
Georg Brandl116aa622007-08-15 14:28:22 +0000222found), '403' (request forbidden), and '401' (authentication required).
223
224See section 10 of RFC 2616 for a reference on all the HTTP error codes.
225
Benjamin Petersone5384b02008-10-04 22:00:42 +0000226The :exc:`HTTPError` instance raised will have an integer 'code' attribute, which
Georg Brandl116aa622007-08-15 14:28:22 +0000227corresponds to the error sent by the server.
228
229Error Codes
230~~~~~~~~~~~
231
232Because the default handlers handle redirects (codes in the 300 range), and
233codes in the 100-299 range indicate success, you will usually only see error
234codes in the 400-599 range.
235
Georg Brandl24420152008-05-26 16:32:26 +0000236:attr:`http.server.BaseHTTPRequestHandler.responses` is a useful dictionary of
Georg Brandl116aa622007-08-15 14:28:22 +0000237response codes in that shows all the response codes used by RFC 2616. The
238dictionary is reproduced here for convenience ::
239
240 # Table mapping response codes to messages; entries have the
241 # form {code: (shortmessage, longmessage)}.
242 responses = {
243 100: ('Continue', 'Request received, please continue'),
244 101: ('Switching Protocols',
245 'Switching to new protocol; obey Upgrade header'),
246
247 200: ('OK', 'Request fulfilled, document follows'),
248 201: ('Created', 'Document created, URL follows'),
249 202: ('Accepted',
250 'Request accepted, processing continues off-line'),
251 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
252 204: ('No Content', 'Request fulfilled, nothing follows'),
253 205: ('Reset Content', 'Clear input form for further input.'),
254 206: ('Partial Content', 'Partial content follows.'),
255
256 300: ('Multiple Choices',
257 'Object has several resources -- see URI list'),
258 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
259 302: ('Found', 'Object moved temporarily -- see URI list'),
260 303: ('See Other', 'Object moved -- see Method and URL list'),
261 304: ('Not Modified',
262 'Document has not changed since given time'),
263 305: ('Use Proxy',
264 'You must use proxy specified in Location to access this '
265 'resource.'),
266 307: ('Temporary Redirect',
267 'Object moved temporarily -- see URI list'),
268
269 400: ('Bad Request',
270 'Bad request syntax or unsupported method'),
271 401: ('Unauthorized',
272 'No permission -- see authorization schemes'),
273 402: ('Payment Required',
274 'No payment -- see charging schemes'),
275 403: ('Forbidden',
276 'Request forbidden -- authorization will not help'),
277 404: ('Not Found', 'Nothing matches the given URI'),
278 405: ('Method Not Allowed',
279 'Specified method is invalid for this server.'),
280 406: ('Not Acceptable', 'URI not available in preferred format.'),
281 407: ('Proxy Authentication Required', 'You must authenticate with '
282 'this proxy before proceeding.'),
283 408: ('Request Timeout', 'Request timed out; try again later.'),
284 409: ('Conflict', 'Request conflict.'),
285 410: ('Gone',
286 'URI no longer exists and has been permanently removed.'),
287 411: ('Length Required', 'Client must specify Content-Length.'),
288 412: ('Precondition Failed', 'Precondition in headers is false.'),
289 413: ('Request Entity Too Large', 'Entity is too large.'),
290 414: ('Request-URI Too Long', 'URI is too long.'),
291 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
292 416: ('Requested Range Not Satisfiable',
293 'Cannot satisfy request range.'),
294 417: ('Expectation Failed',
295 'Expect condition could not be satisfied.'),
296
297 500: ('Internal Server Error', 'Server got itself in trouble'),
298 501: ('Not Implemented',
299 'Server does not support this operation'),
300 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
301 503: ('Service Unavailable',
302 'The server cannot process the request due to a high load'),
303 504: ('Gateway Timeout',
304 'The gateway server did not receive a timely response'),
305 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
306 }
307
308When an error is raised the server responds by returning an HTTP error code
Benjamin Petersone5384b02008-10-04 22:00:42 +0000309*and* an error page. You can use the :exc:`HTTPError` instance as a response on the
Georg Brandl116aa622007-08-15 14:28:22 +0000310page returned. This means that as well as the code attribute, it also has read,
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000311geturl, and info, methods as returned by the ``urllib.response`` module::
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000313 >>> req = urllib.request.Request('http://www.python.org/fish.html')
Georg Brandl48310cd2009-01-03 21:18:54 +0000314 >>> try:
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000315 >>> urllib.request.urlopen(req)
316 >>> except urllib.error.URLError, e:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000317 >>> print(e.code)
318 >>> print(e.read())
Georg Brandl48310cd2009-01-03 21:18:54 +0000319 >>>
Georg Brandl116aa622007-08-15 14:28:22 +0000320 404
Georg Brandl48310cd2009-01-03 21:18:54 +0000321 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
Georg Brandl116aa622007-08-15 14:28:22 +0000322 "http://www.w3.org/TR/html4/loose.dtd">
Georg Brandl48310cd2009-01-03 21:18:54 +0000323 <?xml-stylesheet href="./css/ht2html.css"
Georg Brandl116aa622007-08-15 14:28:22 +0000324 type="text/css"?>
Georg Brandl48310cd2009-01-03 21:18:54 +0000325 <html><head><title>Error 404: File Not Found</title>
Georg Brandl116aa622007-08-15 14:28:22 +0000326 ...... etc...
327
328Wrapping it Up
329--------------
330
Benjamin Petersone5384b02008-10-04 22:00:42 +0000331So if you want to be prepared for :exc:`HTTPError` *or* :exc:`URLError` there are two
Georg Brandl116aa622007-08-15 14:28:22 +0000332basic approaches. I prefer the second approach.
333
334Number 1
335~~~~~~~~
336
337::
338
339
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000340 from urllib.request import Request, urlopen
341 from urllib.error import URLError, HTTPError
Georg Brandl116aa622007-08-15 14:28:22 +0000342 req = Request(someurl)
343 try:
344 response = urlopen(req)
345 except HTTPError, e:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000346 print('The server couldn\'t fulfill the request.')
347 print('Error code: ', e.code)
Georg Brandl116aa622007-08-15 14:28:22 +0000348 except URLError, e:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000349 print('We failed to reach a server.')
350 print('Reason: ', e.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000351 else:
352 # everything is fine
353
354
355.. note::
356
357 The ``except HTTPError`` *must* come first, otherwise ``except URLError``
Benjamin Petersone5384b02008-10-04 22:00:42 +0000358 will *also* catch an :exc:`HTTPError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000359
360Number 2
361~~~~~~~~
362
363::
364
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000365 from urllib.request import Request, urlopen
366 from urllib.error import URLError
Georg Brandl116aa622007-08-15 14:28:22 +0000367 req = Request(someurl)
368 try:
369 response = urlopen(req)
370 except URLError, e:
371 if hasattr(e, 'reason'):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000372 print('We failed to reach a server.')
373 print('Reason: ', e.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000374 elif hasattr(e, 'code'):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000375 print('The server couldn\'t fulfill the request.')
376 print('Error code: ', e.code)
Georg Brandl116aa622007-08-15 14:28:22 +0000377 else:
378 # everything is fine
Georg Brandl48310cd2009-01-03 21:18:54 +0000379
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381info and geturl
382===============
383
Benjamin Petersone5384b02008-10-04 22:00:42 +0000384The response returned by urlopen (or the :exc:`HTTPError` instance) has two
385useful methods :meth:`info` and :meth:`geturl` and is defined in the module
386:mod:`urllib.response`..
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388**geturl** - this returns the real URL of the page fetched. This is useful
389because ``urlopen`` (or the opener object used) may have followed a
390redirect. The URL of the page fetched may not be the same as the URL requested.
391
392**info** - this returns a dictionary-like object that describes the page
393fetched, particularly the headers sent by the server. It is currently an
Georg Brandl0f7ede42008-06-23 11:23:31 +0000394:class:`http.client.HTTPMessage` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396Typical headers include 'Content-length', 'Content-type', and so on. See the
397`Quick Reference to HTTP Headers <http://www.cs.tut.fi/~jkorpela/http.html>`_
398for a useful listing of HTTP headers with brief explanations of their meaning
399and use.
400
401
402Openers and Handlers
403====================
404
405When you fetch a URL you use an opener (an instance of the perhaps
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000406confusingly-named :class:`urllib.request.OpenerDirector`). Normally we have been using
Georg Brandl116aa622007-08-15 14:28:22 +0000407the default opener - via ``urlopen`` - but you can create custom
408openers. Openers use handlers. All the "heavy lifting" is done by the
409handlers. Each handler knows how to open URLs for a particular URL scheme (http,
410ftp, etc.), or how to handle an aspect of URL opening, for example HTTP
411redirections or HTTP cookies.
412
413You will want to create openers if you want to fetch URLs with specific handlers
414installed, for example to get an opener that handles cookies, or to get an
415opener that does not handle redirections.
416
417To create an opener, instantiate an ``OpenerDirector``, and then call
418``.add_handler(some_handler_instance)`` repeatedly.
419
420Alternatively, you can use ``build_opener``, which is a convenience function for
421creating opener objects with a single function call. ``build_opener`` adds
422several handlers by default, but provides a quick way to add more and/or
423override the default handlers.
424
425Other sorts of handlers you might want to can handle proxies, authentication,
426and other common but slightly specialised situations.
427
428``install_opener`` can be used to make an ``opener`` object the (global) default
429opener. This means that calls to ``urlopen`` will use the opener you have
430installed.
431
432Opener objects have an ``open`` method, which can be called directly to fetch
433urls in the same way as the ``urlopen`` function: there's no need to call
434``install_opener``, except as a convenience.
435
436
437Basic Authentication
438====================
439
440To illustrate creating and installing a handler we will use the
441``HTTPBasicAuthHandler``. For a more detailed discussion of this subject --
442including an explanation of how Basic Authentication works - see the `Basic
443Authentication Tutorial
444<http://www.voidspace.org.uk/python/articles/authentication.shtml>`_.
445
446When authentication is required, the server sends a header (as well as the 401
447error code) requesting authentication. This specifies the authentication scheme
448and a 'realm'. The header looks like : ``Www-authenticate: SCHEME
449realm="REALM"``.
450
Georg Brandl48310cd2009-01-03 21:18:54 +0000451e.g. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453 Www-authenticate: Basic realm="cPanel Users"
454
455
456The client should then retry the request with the appropriate name and password
457for the realm included as a header in the request. This is 'basic
458authentication'. In order to simplify this process we can create an instance of
459``HTTPBasicAuthHandler`` and an opener to use this handler.
460
461The ``HTTPBasicAuthHandler`` uses an object called a password manager to handle
462the mapping of URLs and realms to passwords and usernames. If you know what the
463realm is (from the authentication header sent by the server), then you can use a
464``HTTPPasswordMgr``. Frequently one doesn't care what the realm is. In that
465case, it is convenient to use ``HTTPPasswordMgrWithDefaultRealm``. This allows
466you to specify a default username and password for a URL. This will be supplied
467in the absence of you providing an alternative combination for a specific
468realm. We indicate this by providing ``None`` as the realm argument to the
469``add_password`` method.
470
471The top-level URL is the first URL that requires authentication. URLs "deeper"
472than the URL you pass to .add_password() will also match. ::
473
474 # create a password manager
Georg Brandl48310cd2009-01-03 21:18:54 +0000475 password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Georg Brandl116aa622007-08-15 14:28:22 +0000476
477 # Add the username and password.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000478 # If we knew the realm, we could use it instead of None.
Georg Brandl116aa622007-08-15 14:28:22 +0000479 top_level_url = "http://example.com/foo/"
480 password_mgr.add_password(None, top_level_url, username, password)
481
Georg Brandl48310cd2009-01-03 21:18:54 +0000482 handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484 # create "opener" (OpenerDirector instance)
Georg Brandl48310cd2009-01-03 21:18:54 +0000485 opener = urllib.request.build_opener(handler)
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487 # use the opener to fetch a URL
Georg Brandl48310cd2009-01-03 21:18:54 +0000488 opener.open(a_url)
Georg Brandl116aa622007-08-15 14:28:22 +0000489
490 # Install the opener.
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000491 # Now all calls to urllib.request.urlopen use our opener.
Georg Brandl48310cd2009-01-03 21:18:54 +0000492 urllib.request.install_opener(opener)
Georg Brandl116aa622007-08-15 14:28:22 +0000493
494.. note::
495
496 In the above example we only supplied our ``HHTPBasicAuthHandler`` to
497 ``build_opener``. By default openers have the handlers for normal situations
498 -- ``ProxyHandler``, ``UnknownHandler``, ``HTTPHandler``,
499 ``HTTPDefaultErrorHandler``, ``HTTPRedirectHandler``, ``FTPHandler``,
500 ``FileHandler``, ``HTTPErrorProcessor``.
501
502``top_level_url`` is in fact *either* a full URL (including the 'http:' scheme
503component and the hostname and optionally the port number)
504e.g. "http://example.com/" *or* an "authority" (i.e. the hostname,
505optionally including the port number) e.g. "example.com" or "example.com:8080"
506(the latter example includes a port number). The authority, if present, must
507NOT contain the "userinfo" component - for example "joe@password:example.com" is
508not correct.
509
510
511Proxies
512=======
513
Georg Brandl0f7ede42008-06-23 11:23:31 +0000514**urllib** will auto-detect your proxy settings and use those. This is through
Georg Brandl116aa622007-08-15 14:28:22 +0000515the ``ProxyHandler`` which is part of the normal handler chain. Normally that's
516a good thing, but there are occasions when it may not be helpful [#]_. One way
517to do this is to setup our own ``ProxyHandler``, with no proxies defined. This
518is done using similar steps to setting up a `Basic Authentication`_ handler : ::
519
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000520 >>> proxy_support = urllib.request.ProxyHandler({})
521 >>> opener = urllib.request.build_opener(proxy_support)
522 >>> urllib.request.install_opener(opener)
Georg Brandl116aa622007-08-15 14:28:22 +0000523
524.. note::
525
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000526 Currently ``urllib.request`` *does not* support fetching of ``https`` locations
527 through a proxy. However, this can be enabled by extending urllib.request as
Georg Brandl116aa622007-08-15 14:28:22 +0000528 shown in the recipe [#]_.
529
530
531Sockets and Layers
532==================
533
Georg Brandl0f7ede42008-06-23 11:23:31 +0000534The Python support for fetching resources from the web is layered. urllib uses
535the :mod:`http.client` library, which in turn uses the socket library.
Georg Brandl116aa622007-08-15 14:28:22 +0000536
537As of Python 2.3 you can specify how long a socket should wait for a response
538before timing out. This can be useful in applications which have to fetch web
539pages. By default the socket module has *no timeout* and can hang. Currently,
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000540the socket timeout is not exposed at the http.client or urllib.request levels.
Georg Brandl24420152008-05-26 16:32:26 +0000541However, you can set the default timeout globally for all sockets using ::
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543 import socket
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000544 import urllib.request
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546 # timeout in seconds
547 timeout = 10
Georg Brandl48310cd2009-01-03 21:18:54 +0000548 socket.setdefaulttimeout(timeout)
Georg Brandl116aa622007-08-15 14:28:22 +0000549
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000550 # this call to urllib.request.urlopen now uses the default timeout
Georg Brandl116aa622007-08-15 14:28:22 +0000551 # we have set in the socket module
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000552 req = urllib.request.Request('http://www.voidspace.org.uk')
553 response = urllib.request.urlopen(req)
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555
556-------
557
558
559Footnotes
560=========
561
562This document was reviewed and revised by John Lee.
563
564.. [#] For an introduction to the CGI protocol see
Georg Brandl48310cd2009-01-03 21:18:54 +0000565 `Writing Web Applications in Python <http://www.pyzine.com/Issue008/Section_Articles/article_CGIOne.html>`_.
Georg Brandl116aa622007-08-15 14:28:22 +0000566.. [#] Like Google for example. The *proper* way to use google from a program
567 is to use `PyGoogle <http://pygoogle.sourceforge.net>`_ of course. See
568 `Voidspace Google <http://www.voidspace.org.uk/python/recipebook.shtml#google>`_
569 for some examples of using the Google API.
570.. [#] Browser sniffing is a very bad practise for website design - building
571 sites using web standards is much more sensible. Unfortunately a lot of
572 sites still send different versions to different browsers.
573.. [#] The user agent for MSIE 6 is
574 *'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)'*
575.. [#] For details of more HTTP request headers, see
576 `Quick Reference to HTTP Headers`_.
577.. [#] In my case I have to use a proxy to access the internet at work. If you
578 attempt to fetch *localhost* URLs through this proxy it blocks them. IE
Georg Brandl0f7ede42008-06-23 11:23:31 +0000579 is set to use the proxy, which urllib picks up on. In order to test
580 scripts with a localhost server, I have to prevent urllib from using
Georg Brandl116aa622007-08-15 14:28:22 +0000581 the proxy.
Georg Brandl48310cd2009-01-03 21:18:54 +0000582.. [#] urllib opener for SSL proxy (CONNECT method): `ASPN Cookbook Recipe
Georg Brandl116aa622007-08-15 14:28:22 +0000583 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456195>`_.
Georg Brandl48310cd2009-01-03 21:18:54 +0000584