Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/howto/urllib2.rst

    r2 r391  
    135135    >>> data['language'] = 'Python'
    136136    >>> url_values = urllib.urlencode(data)
    137     >>> print url_values
     137    >>> print url_values  # The order may differ. #doctest: +SKIP
    138138    name=Somebody+Here&language=Python&location=Northampton
    139139    >>> url = 'http://www.example.com/example.cgi'
    140140    >>> full_url = url + '?' + url_values
    141     >>> data = urllib2.open(full_url)
     141    >>> data = urllib2.urlopen(full_url)
    142142
    143143Notice that the full URL is created by adding a ``?`` to the URL, followed by
     
    202202    >>> req = urllib2.Request('http://www.pretend_server.org')
    203203    >>> try: urllib2.urlopen(req)
    204     >>> except URLError, e:
    205     >>>    print e.reason
    206     >>>
     204    ... except URLError as e:
     205    ...    print e.reason   #doctest: +SKIP
     206    ...
    207207    (4, 'getaddrinfo failed')
    208208
     
    310310    >>> req = urllib2.Request('http://www.python.org/fish.html')
    311311    >>> try:
    312     >>>     urllib2.urlopen(req)
    313     >>> except HTTPError, e:
    314     >>>     print e.code
    315     >>>     print e.read()
    316     >>>
     312    ...     urllib2.urlopen(req)
     313    ... except urllib2.HTTPError as e:
     314    ...     print e.code
     315    ...     print e.read() #doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
     316    ...
    317317    404
    318     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    319         "http://www.w3.org/TR/html4/loose.dtd">
    320     <?xml-stylesheet href="./css/ht2html.css"
    321         type="text/css"?>
    322     <html><head><title>Error 404: File Not Found</title>
    323     ...... etc...
     318    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     319    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     320    ...
     321    <title>Page Not Found</title>
     322    ...
     323
    324324
    325325Wrapping it Up
     
    339339    try:
    340340        response = urlopen(req)
    341     except HTTPError, e:
     341    except HTTPError as e:
    342342        print 'The server couldn\'t fulfill the request.'
    343343        print 'Error code: ', e.code
    344     except URLError, e:
     344    except URLError as e:
    345345        print 'We failed to reach a server.'
    346346        print 'Reason: ', e.reason
     
    363363    try:
    364364        response = urlopen(req)
    365     except URLError, e:
     365    except URLError as e:
    366366        if hasattr(e, 'reason'):
    367367            print 'We failed to reach a server.'
     
    440440When authentication is required, the server sends a header (as well as the 401
    441441error code) requesting authentication.  This specifies the authentication scheme
    442 and a 'realm'. The header looks like : ``Www-authenticate: SCHEME
     442and a 'realm'. The header looks like : ``WWW-Authenticate: SCHEME
    443443realm="REALM"``.
    444444
    445445e.g. ::
    446446
    447     Www-authenticate: Basic realm="cPanel Users"
     447    WWW-Authenticate: Basic realm="cPanel Users"
    448448
    449449
     
    490490    In the above example we only supplied our ``HTTPBasicAuthHandler`` to
    491491    ``build_opener``. By default openers have the handlers for normal situations
    492     -- ``ProxyHandler``, ``UnknownHandler``, ``HTTPHandler``,
     492    -- ``ProxyHandler`` (if a proxy setting such as an :envvar:`http_proxy`
     493    environment variable is set), ``UnknownHandler``, ``HTTPHandler``,
    493494    ``HTTPDefaultErrorHandler``, ``HTTPRedirectHandler``, ``FTPHandler``,
    494495    ``FileHandler``, ``HTTPErrorProcessor``.
     
    507508
    508509**urllib2** will auto-detect your proxy settings and use those. This is through
    509 the ``ProxyHandler`` which is part of the normal handler chain. Normally that's
    510 a good thing, but there are occasions when it may not be helpful [#]_. One way
    511 to do this is to setup our own ``ProxyHandler``, with no proxies defined. This
    512 is done using similar steps to setting up a `Basic Authentication`_ handler : ::
     510the ``ProxyHandler``, which is part of the normal handler chain when a proxy
     511setting is detected.  Normally that's a good thing, but there are occasions
     512when it may not be helpful [#]_. One way to do this is to setup our own
     513``ProxyHandler``, with no proxies defined. This is done using similar steps to
     514setting up a `Basic Authentication`_ handler : ::
    513515
    514516    >>> proxy_support = urllib2.ProxyHandler({})
Note: See TracChangeset for help on using the changeset viewer.