Changeset 391 for python/trunk/Doc/howto/urllib2.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/howto/urllib2.rst
r2 r391 135 135 >>> data['language'] = 'Python' 136 136 >>> url_values = urllib.urlencode(data) 137 >>> print url_values 137 >>> print url_values # The order may differ. #doctest: +SKIP 138 138 name=Somebody+Here&language=Python&location=Northampton 139 139 >>> url = 'http://www.example.com/example.cgi' 140 140 >>> full_url = url + '?' + url_values 141 >>> data = urllib2. open(full_url)141 >>> data = urllib2.urlopen(full_url) 142 142 143 143 Notice that the full URL is created by adding a ``?`` to the URL, followed by … … 202 202 >>> req = urllib2.Request('http://www.pretend_server.org') 203 203 >>> try: urllib2.urlopen(req) 204 >>> except URLError,e:205 >>> print e.reason206 >>>204 ... except URLError as e: 205 ... print e.reason #doctest: +SKIP 206 ... 207 207 (4, 'getaddrinfo failed') 208 208 … … 310 310 >>> req = urllib2.Request('http://www.python.org/fish.html') 311 311 >>> try: 312 >>>urllib2.urlopen(req)313 >>> except HTTPError,e:314 >>>print e.code315 >>> 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 ... 317 317 404 318 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//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 324 324 325 325 Wrapping it Up … … 339 339 try: 340 340 response = urlopen(req) 341 except HTTPError ,e:341 except HTTPError as e: 342 342 print 'The server couldn\'t fulfill the request.' 343 343 print 'Error code: ', e.code 344 except URLError ,e:344 except URLError as e: 345 345 print 'We failed to reach a server.' 346 346 print 'Reason: ', e.reason … … 363 363 try: 364 364 response = urlopen(req) 365 except URLError ,e:365 except URLError as e: 366 366 if hasattr(e, 'reason'): 367 367 print 'We failed to reach a server.' … … 440 440 When authentication is required, the server sends a header (as well as the 401 441 441 error code) requesting authentication. This specifies the authentication scheme 442 and a 'realm'. The header looks like : ``W ww-authenticate: SCHEME442 and a 'realm'. The header looks like : ``WWW-Authenticate: SCHEME 443 443 realm="REALM"``. 444 444 445 445 e.g. :: 446 446 447 W ww-authenticate: Basic realm="cPanel Users"447 WWW-Authenticate: Basic realm="cPanel Users" 448 448 449 449 … … 490 490 In the above example we only supplied our ``HTTPBasicAuthHandler`` to 491 491 ``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``, 493 494 ``HTTPDefaultErrorHandler``, ``HTTPRedirectHandler``, ``FTPHandler``, 494 495 ``FileHandler``, ``HTTPErrorProcessor``. … … 507 508 508 509 **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 : :: 510 the ``ProxyHandler``, which is part of the normal handler chain when a proxy 511 setting is detected. Normally that's a good thing, but there are occasions 512 when 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 514 setting up a `Basic Authentication`_ handler : :: 513 515 514 516 >>> proxy_support = urllib2.ProxyHandler({})
Note:
See TracChangeset
for help on using the changeset viewer.