|
138 | 138 | _opener = None |
139 | 139 | def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
140 | 140 | *, cafile=None, capath=None, cadefault=False, context=None): |
| 141 | + '''Open the URL url, which can be either a string or a Request object. |
| 142 | +
|
| 143 | + *data* must be a bytes object specifying additional data to be sent to the |
| 144 | + server, or None if no such data is needed. data may also be an iterable |
| 145 | + object and in that case Content-Length value must be specified in the |
| 146 | + headers. Currently HTTP requests are the only ones that use data; the HTTP |
| 147 | + request will be a POST instead of a GET when the data parameter is |
| 148 | + provided. |
| 149 | +
|
| 150 | + *data* should be a buffer in the standard application/x-www-form-urlencoded |
| 151 | + format. The urllib.parse.urlencode() function takes a mapping or sequence |
| 152 | + of 2-tuples and returns a string in this format. It should be encoded to |
| 153 | + bytes before being used as the data parameter. The charset parameter in |
| 154 | + Content-Type header may be used to specify the encoding. If charset |
| 155 | + parameter is not sent with the Content-Type header, the server following |
| 156 | + the HTTP 1.1 recommendation may assume that the data is encoded in |
| 157 | + ISO-8859-1 encoding. It is advisable to use charset parameter with encoding |
| 158 | + used in Content-Type header with the Request. |
| 159 | +
|
| 160 | + urllib.request module uses HTTP/1.1 and includes a "Connection:close" |
| 161 | + header in its HTTP requests. |
| 162 | +
|
| 163 | + The optional *timeout* parameter specifies a timeout in seconds for |
| 164 | + blocking operations like the connection attempt (if not specified, the |
| 165 | + global default timeout setting will be used). This only works for HTTP, |
| 166 | + HTTPS and FTP connections. |
| 167 | +
|
| 168 | + If *context* is specified, it must be a ssl.SSLContext instance describing |
| 169 | + the various SSL options. See HTTPSConnection for more details. |
| 170 | +
|
| 171 | + The optional *cafile* and *capath* parameters specify a set of trusted CA |
| 172 | + certificates for HTTPS requests. cafile should point to a single file |
| 173 | + containing a bundle of CA certificates, whereas capath should point to a |
| 174 | + directory of hashed certificate files. More information can be found in |
| 175 | + ssl.SSLContext.load_verify_locations(). |
| 176 | +
|
| 177 | + The *cadefault* parameter is ignored. |
| 178 | +
|
| 179 | + For http and https urls, this function returns a http.client.HTTPResponse |
| 180 | + object which has the following HTTPResponse Objects methods. |
| 181 | +
|
| 182 | + For ftp, file, and data urls and requests explicitly handled by legacy |
| 183 | + URLopener and FancyURLopener classes, this function returns a |
| 184 | + urllib.response.addinfourl object which can work as context manager and has |
| 185 | + methods such as: |
| 186 | +
|
| 187 | + * geturl() — return the URL of the resource retrieved, commonly used to |
| 188 | + determine if a redirect was followed |
| 189 | +
|
| 190 | + * info() — return the meta-information of the page, such as headers, in the |
| 191 | + form of an email.message_from_string() instance (see Quick Reference to |
| 192 | + HTTP Headers) |
| 193 | +
|
| 194 | + * getcode() – return the HTTP status code of the response. Raises URLError |
| 195 | + on errors. |
| 196 | +
|
| 197 | + Note that *None& may be returned if no handler handles the request (though |
| 198 | + the default installed global OpenerDirector uses UnknownHandler to ensure |
| 199 | + this never happens). |
| 200 | +
|
| 201 | + In addition, if proxy settings are detected (for example, when a *_proxy |
| 202 | + environment variable like http_proxy is set), ProxyHandler is default |
| 203 | + installed and makes sure the requests are handled through the proxy. |
| 204 | +
|
| 205 | + ''' |
141 | 206 | global _opener |
142 | 207 | if cafile or capath or cadefault: |
143 | 208 | if context is not None: |
|
0 commit comments