Changeset 391 for python/trunk/Doc/library/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/library/urllib2.rst
r2 r391 10 10 .. note:: 11 11 The :mod:`urllib2` module has been split across several modules in 12 Python 3 .0named :mod:`urllib.request` and :mod:`urllib.error`.12 Python 3 named :mod:`urllib.request` and :mod:`urllib.error`. 13 13 The :term:`2to3` tool will automatically adapt imports when converting 14 your sources to 3.0.14 your sources to Python 3. 15 15 16 16 … … 19 19 redirections, cookies and more. 20 20 21 21 22 The :mod:`urllib2` module defines the following functions: 22 23 … … 25 26 26 27 Open the URL *url*, which can be either a string or a :class:`Request` object. 28 29 .. warning:: 30 HTTPS requests do not do any verification of the server's certificate. 31 32 *data* may be a string specifying additional data to send to the server, or 33 ``None`` if no such data is needed. Currently HTTP requests are the only ones 34 that use *data*; the HTTP request will be a POST instead of a GET when the 35 *data* parameter is provided. *data* should be a buffer in the standard 36 :mimetype:`application/x-www-form-urlencoded` format. The 37 :func:`urllib.urlencode` function takes a mapping or sequence of 2-tuples and 38 returns a string in this format. urllib2 module sends HTTP/1.1 requests with 39 ``Connection:close`` header included. 40 41 The optional *timeout* parameter specifies a timeout in seconds for blocking 42 operations like the connection attempt (if not specified, the global default 43 timeout setting will be used). This actually only works for HTTP, HTTPS and 44 FTP connections. 45 46 This function returns a file-like object with two additional methods: 47 48 * :meth:`geturl` --- return the URL of the resource retrieved, commonly used to 49 determine if a redirect was followed 50 51 * :meth:`info` --- return the meta-information of the page, such as headers, 52 in the form of an :class:`mimetools.Message` instance 53 (see `Quick Reference to HTTP Headers <http://www.cs.tut.fi/~jkorpela/http.html>`_) 54 55 * :meth:`getcode` --- return the HTTP status code of the response. 56 57 Raises :exc:`URLError` on errors. 58 59 Note that ``None`` may be returned if no handler handles the request (though the 60 default installed global :class:`OpenerDirector` uses :class:`UnknownHandler` to 61 ensure this never happens). 62 63 In addition, if proxy settings are detected (for example, when a ``*_proxy`` 64 environment variable like :envvar:`http_proxy` is set), 65 :class:`ProxyHandler` is default installed and makes sure the requests are 66 handled through the proxy. 67 68 .. versionchanged:: 2.6 69 *timeout* was added. 70 71 72 .. function:: install_opener(opener) 73 74 Install an :class:`OpenerDirector` instance as the default global opener. 75 Installing an opener is only necessary if you want urlopen to use that opener; 76 otherwise, simply call :meth:`OpenerDirector.open` instead of :func:`urlopen`. 77 The code does not check for a real :class:`OpenerDirector`, and any class with 78 the appropriate interface will work. 79 80 81 .. function:: build_opener([handler, ...]) 82 83 Return an :class:`OpenerDirector` instance, which chains the handlers in the 84 order given. *handler*\s can be either instances of :class:`BaseHandler`, or 85 subclasses of :class:`BaseHandler` (in which case it must be possible to call 86 the constructor without any parameters). Instances of the following classes 87 will be in front of the *handler*\s, unless the *handler*\s contain them, 88 instances of them or subclasses of them: :class:`ProxyHandler` (if proxy 89 settings are detected), 90 :class:`UnknownHandler`, :class:`HTTPHandler`, :class:`HTTPDefaultErrorHandler`, 91 :class:`HTTPRedirectHandler`, :class:`FTPHandler`, :class:`FileHandler`, 92 :class:`HTTPErrorProcessor`. 93 94 If the Python installation has SSL support (i.e., if the :mod:`ssl` module can be imported), 95 :class:`HTTPSHandler` will also be added. 96 97 Beginning in Python 2.3, a :class:`BaseHandler` subclass may also change its 98 :attr:`handler_order` attribute to modify its position in the handlers 99 list. 100 101 The following exceptions are raised as appropriate: 102 103 104 .. exception:: URLError 105 106 The handlers raise this exception (or derived exceptions) when they run into a 107 problem. It is a subclass of :exc:`IOError`. 108 109 .. attribute:: reason 110 111 The reason for this error. It can be a message string or another exception 112 instance (:exc:`socket.error` for remote URLs, :exc:`OSError` for local 113 URLs). 114 115 116 .. exception:: HTTPError 117 118 Though being an exception (a subclass of :exc:`URLError`), an :exc:`HTTPError` 119 can also function as a non-exceptional file-like return value (the same thing 120 that :func:`urlopen` returns). This is useful when handling exotic HTTP 121 errors, such as requests for authentication. 122 123 .. attribute:: code 124 125 An HTTP status code as defined in `RFC 2616 <http://www.faqs.org/rfcs/rfc2616.html>`_. 126 This numeric value corresponds to a value found in the dictionary of 127 codes as found in :attr:`BaseHTTPServer.BaseHTTPRequestHandler.responses`. 128 129 .. attribute:: reason 130 131 The reason for this error. It can be a message string or another exception 132 instance. 133 134 The following classes are provided: 135 136 137 .. class:: Request(url[, data][, headers][, origin_req_host][, unverifiable]) 138 139 This class is an abstraction of a URL request. 140 141 *url* should be a string containing a valid URL. 27 142 28 143 *data* may be a string specifying additional data to send to the server, or … … 34 149 returns a string in this format. 35 150 36 The optional *timeout* parameter specifies a timeout in seconds for blocking37 operations like the connection attempt (if not specified, the global default38 timeout setting will be used). This actually only works for HTTP, HTTPS,39 FTP and FTPS connections.40 41 This function returns a file-like object with two additional methods:42 43 * :meth:`geturl` --- return the URL of the resource retrieved, commonly used to44 determine if a redirect was followed45 46 * :meth:`info` --- return the meta-information of the page, such as headers, in47 the form of an ``httplib.HTTPMessage`` instance48 (see `Quick Reference to HTTP Headers <http://www.cs.tut.fi/~jkorpela/http.html>`_)49 50 Raises :exc:`URLError` on errors.51 52 Note that ``None`` may be returned if no handler handles the request (though the53 default installed global :class:`OpenerDirector` uses :class:`UnknownHandler` to54 ensure this never happens).55 56 In addition, default installed :class:`ProxyHandler` makes sure the requests57 are handled through the proxy when they are set.58 59 .. versionchanged:: 2.660 *timeout* was added.61 62 63 .. function:: install_opener(opener)64 65 Install an :class:`OpenerDirector` instance as the default global opener.66 Installing an opener is only necessary if you want urlopen to use that opener;67 otherwise, simply call :meth:`OpenerDirector.open` instead of :func:`urlopen`.68 The code does not check for a real :class:`OpenerDirector`, and any class with69 the appropriate interface will work.70 71 72 .. function:: build_opener([handler, ...])73 74 Return an :class:`OpenerDirector` instance, which chains the handlers in the75 order given. *handler*\s can be either instances of :class:`BaseHandler`, or76 subclasses of :class:`BaseHandler` (in which case it must be possible to call77 the constructor without any parameters). Instances of the following classes78 will be in front of the *handler*\s, unless the *handler*\s contain them,79 instances of them or subclasses of them: :class:`ProxyHandler`,80 :class:`UnknownHandler`, :class:`HTTPHandler`, :class:`HTTPDefaultErrorHandler`,81 :class:`HTTPRedirectHandler`, :class:`FTPHandler`, :class:`FileHandler`,82 :class:`HTTPErrorProcessor`.83 84 If the Python installation has SSL support (i.e., if the :mod:`ssl` module can be imported),85 :class:`HTTPSHandler` will also be added.86 87 Beginning in Python 2.3, a :class:`BaseHandler` subclass may also change its88 :attr:`handler_order` member variable to modify its position in the handlers89 list.90 91 The following exceptions are raised as appropriate:92 93 94 .. exception:: URLError95 96 The handlers raise this exception (or derived exceptions) when they run into a97 problem. It is a subclass of :exc:`IOError`.98 99 .. attribute:: reason100 101 The reason for this error. It can be a message string or another exception102 instance (:exc:`socket.error` for remote URLs, :exc:`OSError` for local103 URLs).104 105 106 .. exception:: HTTPError107 108 Though being an exception (a subclass of :exc:`URLError`), an :exc:`HTTPError`109 can also function as a non-exceptional file-like return value (the same thing110 that :func:`urlopen` returns). This is useful when handling exotic HTTP111 errors, such as requests for authentication.112 113 .. attribute:: code114 115 An HTTP status code as defined in `RFC 2616 <http://www.faqs.org/rfcs/rfc2616.html>`_.116 This numeric value corresponds to a value found in the dictionary of117 codes as found in :attr:`BaseHTTPServer.BaseHTTPRequestHandler.responses`.118 119 120 121 The following classes are provided:122 123 124 .. class:: Request(url[, data][, headers][, origin_req_host][, unverifiable])125 126 This class is an abstraction of a URL request.127 128 *url* should be a string containing a valid URL.129 130 *data* may be a string specifying additional data to send to the server, or131 ``None`` if no such data is needed. Currently HTTP requests are the only ones132 that use *data*; the HTTP request will be a POST instead of a GET when the133 *data* parameter is provided. *data* should be a buffer in the standard134 :mimetype:`application/x-www-form-urlencoded` format. The135 :func:`urllib.urlencode` function takes a mapping or sequence of 2-tuples and136 returns a string in this format.137 138 151 *headers* should be a dictionary, and will be treated as if :meth:`add_header` 139 152 was called with each key and value as arguments. This is often used to "spoof" … … 193 206 dictionary mapping protocol names to URLs of proxies. The default is to read 194 207 the list of proxies from the environment variables 195 :envvar:`<protocol>_proxy`. If no proxy environment variables are set, in a196 Windows environment,proxy settings are obtained from the registry's197 Internet Settings section and in a Mac OS X environment,proxy information208 :envvar:`<protocol>_proxy`. If no proxy environment variables are set, then 209 in a Windows environment proxy settings are obtained from the registry's 210 Internet Settings section, and in a Mac OS X environment proxy information 198 211 is retrieved from the OS X System Configuration Framework. 199 212 … … 291 304 292 305 A catch-all class to handle unknown URLs. 306 307 308 .. class:: HTTPErrorProcessor() 309 310 Process HTTP error responses. 293 311 294 312 … … 371 389 372 390 391 .. method:: Request.get_header(header_name, default=None) 392 393 Return the value of the given header. If the header is not present, return 394 the default value. 395 396 397 .. method:: Request.header_items() 398 399 Return a list of tuples (header_name, header_value) of the Request headers. 400 401 373 402 .. method:: Request.set_proxy(host, type) 374 403 … … 428 457 optional *timeout* parameter specifies a timeout in seconds for blocking 429 458 operations like the connection attempt (if not specified, the global default 430 timeout setting will be used i). The timeout feature actually works only for431 HTTP, HTTPS , FTP and FTPSconnections).459 timeout setting will be used). The timeout feature actually works only for 460 HTTP, HTTPS and FTP connections). 432 461 433 462 .. versionchanged:: 2.6 … … 465 494 466 495 Note that the implementation of these methods may involve calls of the parent 467 :class:`OpenerDirector` instance's :meth:`.open` and :meth:`.error` methods. 496 :class:`OpenerDirector` instance's :meth:`~OpenerDirector.open` and 497 :meth:`~OpenerDirector.error` methods. 468 498 469 499 #. Every handler with a method named like :samp:`{protocol}_response` has that … … 490 520 Remove any parents. 491 521 492 The following members and methods should only be used by classes derived from522 The following attributes and methods should only be used by classes derived from 493 523 :class:`BaseHandler`. 494 524 … … 876 906 877 907 878 .. method:: HTTPErrorProcessor. unknown_open()908 .. method:: HTTPErrorProcessor.http_response() 879 909 880 910 Process HTTP error responses. … … 887 917 :class:`urllib2.HTTPDefaultErrorHandler` will raise an :exc:`HTTPError` if no 888 918 other handler handles the error. 919 920 .. method:: HTTPErrorProcessor.https_response() 921 922 Process HTTPS error responses. 923 924 The behavior is same as :meth:`http_response`. 889 925 890 926
Note:
See TracChangeset
for help on using the changeset viewer.