Open the URL url, which can be either a string or a Request object. *data* must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details. urllib.request module uses HTTP/1.1 and includes a "Connection:close"
(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
*, cafile=None, capath=None, cadefault=False, context=None)
| 137 | |
| 138 | _opener = None |
| 139 | def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
| 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 an object specifying additional data to be sent to |
| 144 | the server, or None if no such data is needed. See Request for |
| 145 | details. |
| 146 | |
| 147 | urllib.request module uses HTTP/1.1 and includes a "Connection:close" |
| 148 | header in its HTTP requests. |
| 149 | |
| 150 | The optional *timeout* parameter specifies a timeout in seconds for |
| 151 | blocking operations like the connection attempt (if not specified, the |
| 152 | global default timeout setting will be used). This only works for HTTP, |
| 153 | HTTPS and FTP connections. |
| 154 | |
| 155 | If *context* is specified, it must be a ssl.SSLContext instance describing |
| 156 | the various SSL options. See HTTPSConnection for more details. |
| 157 | |
| 158 | The optional *cafile* and *capath* parameters specify a set of trusted CA |
| 159 | certificates for HTTPS requests. cafile should point to a single file |
| 160 | containing a bundle of CA certificates, whereas capath should point to a |
| 161 | directory of hashed certificate files. More information can be found in |
| 162 | ssl.SSLContext.load_verify_locations(). |
| 163 | |
| 164 | The *cadefault* parameter is ignored. |
| 165 | |
| 166 | |
| 167 | This function always returns an object which can work as a |
| 168 | context manager and has the properties url, headers, and status. |
| 169 | See urllib.response.addinfourl for more detail on these properties. |
| 170 | |
| 171 | For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse |
| 172 | object slightly modified. In addition to the three new methods above, the |
| 173 | msg attribute contains the same information as the reason attribute --- |
| 174 | the reason phrase returned by the server --- instead of the response |
| 175 | headers as it is specified in the documentation for HTTPResponse. |
| 176 | |
| 177 | For FTP, file, and data URLs and requests explicitly handled by legacy |
| 178 | URLopener and FancyURLopener classes, this function returns a |
| 179 | urllib.response.addinfourl object. |
| 180 | |
| 181 | Note that None may be returned if no handler handles the request (though |
| 182 | the default installed global OpenerDirector uses UnknownHandler to ensure |
| 183 | this never happens). |
| 184 | |
| 185 | In addition, if proxy settings are detected (for example, when a *_proxy |
| 186 | environment variable like http_proxy is set), ProxyHandler is default |
| 187 | installed and makes sure the requests are handled through the proxy. |
| 188 | |
| 189 | ''' |
| 190 | global _opener |
| 191 | if cafile or capath or cadefault: |
| 192 | import warnings |
| 193 | warnings.warn("cafile, capath and cadefault are deprecated, use a " |
| 194 | "custom context instead.", DeprecationWarning, 2) |
| 195 | if context is not None: |
| 196 | raise ValueError( |
no test coverage detected