598 lines · 23.0 KB · Text
RawDownload
============================================== HOWTO Fetch Internet Resources Using urllib2==============================================----------------------------  Fetching URLs With Python----------------------------  .. note::     There is an French translation of an earlier revision of this    HOWTO, available at `urllib2 - Le Manuel manquant    <http://www.voidspace/python/articles/urllib2_francais.shtml>`_. .. contents:: urllib2 Tutorial  Introduction============ .. sidebar:: Related Articles     You may also find useful the following article on fetching web    resources with Python :        * `Basic Authentication <http://www.voidspace.org.uk/python/articles/authentication.shtml>`_            A tutorial on *Basic Authentication*, with examples in Python.        This HOWTO is written by `Michael Foord    <http://www.voidspace.org.uk/python/index.shtml>`_. **urllib2** is a `Python <http://www.python.org>`_ module for fetching URLs(Uniform Resource Locators). It offers a very simple interface, in the form ofthe *urlopen* function. This is capable of fetching URLs using a varietyof different protocols. It also offers a slightly more complexinterface for handling common situations - like basic authentication,cookies, proxies and so on. These are provided by objects calledhandlers and openers. urllib2 supports fetching URLs for many "URL schemes" (identified by the stringbefore 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 assoon as you encounter errors or non-trivial cases when opening HTTPURLs, you will need some understanding of the HyperText TransferProtocol. The most comprehensive and authoritative reference to HTTPis :RFC:`2616`. This is a technical document and not intended to beeasy to read. This HOWTO aims to illustrate using *urllib2*, withenough detail about HTTP to help you through. It is not intended toreplace the `urllib2 docs <http://docs.python.org/lib/module-urllib2.html>`_ ,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 morecomplicated cases, concentrating on HTTP. HTTP is based on requests and responses - the client makes requestsand servers send responses. urllib2 mirrors this with a ``Request``object which represents the HTTP request you are making. In itssimplest form you create a Request object that specifies the URL youwant to fetch. Calling ``urlopen`` with this Request object returns aresponse object for the URL requested. This response is a file-likeobject, 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 handleall 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 objectsallow you to do: First, you can pass data to be sent to the server.Second, you can pass extra information ("metadata") *about* the dataor the about request itself, to the server - this information is sentas 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 toa CGI (Common Gateway Interface) script [#]_ or other webapplication). With HTTP, this is often done using what's known as a**POST** request. This is often what your browser does when you submita HTML form that you filled in on the web. Not all POSTs have to comefrom forms: you can use a POST to transmit arbitrary data to your ownapplication. In the common case of HTML forms, the data needs to beencoded in a standard way, and then passed to the Request object asthe ``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 uploadfrom HTML forms - see`HTML Specification, Form Submission <http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13>`_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 POSTrequests often have "side-effects": they change the state of thesystem in some way (for example by placing an order with the websitefor 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 causeside-effects, nothing prevents a GET request from having side-effects,nor a POST requests from having no side-effects. Data can also bepassed 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 bythe encoded values. Headers------- We'll discuss here one particular HTTP header, to illustrate how toadd headers to your HTTP request. Some websites [#]_ dislike being browsed by programs, or senddifferent versions to different browsers [#]_ . By default urllib2identifies itself as ``Python-urllib/x.y`` (where ``x`` and ``y`` arethe major and minor version numbers of the Python release,e.g. ``Python-urllib/2.5``), which may confuse the site, or just plainnot work. The way a browser identifies itself is through the``User-Agent`` header [#]_. When you create a Request object you canpass a dictionary of headers in. The following example makes the samerequest as above, but identifies itself as a version of InternetExplorer [#]_. ::     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 `info andgeturl`_ which comes after we have a look at what happens when thingsgo wrong.  Handling Exceptions=================== *urlopen* raises ``URLError`` when it cannot handle a response (thoughas usual with Python APIs, builtin exceptions such as ValueError,TypeError etc. may also be raised). ``HTTPError`` is the subclass of ``URLError`` raised in the specificcase of HTTP URLs. URLError-------- Often, URLError is raised because there is no network connection (noroute 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 "statuscode". Sometimes the status code indicates that the server is unableto fulfil the request. The default handlers will handle some of theseresponses 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, urlopenwill raise an ``HTTPError``. Typical errors include '404' (page notfound), '403' (request forbidden), and '401' (authenticationrequired). See section 10 of RFC 2616 for a reference on all the HTTP errorcodes. 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 300range), and codes in the 100-299 range indicate success, you willusually only see error codes in the 400-599 range. ``BaseHTTPServer.BaseHTTPRequestHandler.responses`` is a usefuldictionary of response codes in that shows all the response codes usedby 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.'),        } When an error is raised the server responds by returning an HTTP errorcode *and* an error page. You can use the ``HTTPError`` instance as aresponse on the page returned. This means that as well as the codeattribute, 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    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">    <?xml-stylesheet href="./css/ht2html.css"         type="text/css"?>    <html><head><title>Error 404: File Not Found</title>     ...... 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         info and geturl=============== The response returned by urlopen (or the ``HTTPError`` instance) hastwo useful methods ``info`` and ``geturl``. **geturl** - this returns the real URL of the page fetched. This isuseful because ``urlopen`` (or the opener object used) may havefollowed a redirect. The URL of the page fetched may not be the sameas the URL requested. **info** - this returns a dictionary-like object that describes thepage fetched, particularly the headers sent by the server. It iscurrently an ``httplib.HTTPMessage`` instance. Typical headers include 'Content-length', 'Content-type', and soon. See the`Quick Reference to HTTP Headers <http://www.cs.tut.fi/~jkorpela/http.html>`_for a useful listing of HTTP headers with brief explanations of their meaningand use.  Openers and Handlers==================== When you fetch a URL you use an opener (an instance of the perhapsconfusingly-named ``urllib2.OpenerDirector``). Normally we have been usingthe default opener - via ``urlopen`` - but you can create customopeners. Openers use handlers. All the "heavy lifting" is done by thehandlers. Each handler knows how to open URLs for a particular URLscheme (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 withspecific handlers installed, for example to get an opener that handlescookies, 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 conveniencefunction for creating opener objects with a single function call.``build_opener`` adds several handlers by default, but provides aquick 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 specialisedsituations. ``install_opener`` can be used to make an ``opener`` object the(global) default opener. This means that calls to ``urlopen`` will usethe opener you have installed. Opener objects have an ``open`` method, which can be called directlyto fetch urls in the same way as the ``urlopen`` function: there's noneed 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 thissubject - including an explanation of how Basic Authentication works -see the `Basic Authentication Tutorial  <http://www.voidspace.org.uk/python/articles/authentication.shtml>`_. When authentication is required, the server sends a header (as well asthe 401 error code) requesting authentication.  This specifies theauthentication 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 andpassword for the realm included as a header in the request. This is'basic authentication'. In order to simplify this process we cancreate an instance of ``HTTPBasicAuthHandler`` and an opener to usethis handler. The ``HTTPBasicAuthHandler`` uses an object called a password managerto handle the mapping of URLs and realms to passwords andusernames. If you know what the realm is (from the authenticationheader sent by the server), then you can use a``HTTPPasswordMgr``. Frequently one doesn't care what the realm is. Inthat case, it is convenient to use``HTTPPasswordMgrWithDefaultRealm``. This allows you to specify adefault username and password for a URL. This will be supplied in theabsence of you providing an alternative combination for a specificrealm. We indicate this by providing ``None`` as the realm argument tothe ``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)                       

Lines 1–500 of 598.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t