| 1 | \section{\module{urllib2} ---
|
|---|
| 2 | extensible library for opening URLs}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{urllib2}
|
|---|
| 5 | \moduleauthor{Jeremy Hylton}{[email protected]}
|
|---|
| 6 | \sectionauthor{Moshe Zadka}{[email protected]}
|
|---|
| 7 |
|
|---|
| 8 | \modulesynopsis{An extensible library for opening URLs using a variety of
|
|---|
| 9 | protocols}
|
|---|
| 10 |
|
|---|
| 11 | The \module{urllib2} module defines functions and classes which help
|
|---|
| 12 | in opening URLs (mostly HTTP) in a complex world --- basic and digest
|
|---|
| 13 | authentication, redirections, cookies and more.
|
|---|
| 14 |
|
|---|
| 15 | The \module{urllib2} module defines the following functions:
|
|---|
| 16 |
|
|---|
| 17 | \begin{funcdesc}{urlopen}{url\optional{, data}}
|
|---|
| 18 | Open the URL \var{url}, which can be either a string or a \class{Request}
|
|---|
| 19 | object.
|
|---|
| 20 |
|
|---|
| 21 | \var{data} may be a string specifying additional data to send to the
|
|---|
| 22 | server, or \code{None} if no such data is needed.
|
|---|
| 23 | Currently HTTP requests are the only ones that use \var{data};
|
|---|
| 24 | the HTTP request will be a POST instead of a GET when the \var{data}
|
|---|
| 25 | parameter is provided. \var{data} should be a buffer in the standard
|
|---|
| 26 | \mimetype{application/x-www-form-urlencoded} format. The
|
|---|
| 27 | \function{urllib.urlencode()} function takes a mapping or sequence of
|
|---|
| 28 | 2-tuples and returns a string in this format.
|
|---|
| 29 |
|
|---|
| 30 | This function returns a file-like object with two additional methods:
|
|---|
| 31 |
|
|---|
| 32 | \begin{itemize}
|
|---|
| 33 | \item \method{geturl()} --- return the URL of the resource retrieved
|
|---|
| 34 | \item \method{info()} --- return the meta-information of the page, as
|
|---|
| 35 | a dictionary-like object
|
|---|
| 36 | \end{itemize}
|
|---|
| 37 |
|
|---|
| 38 | Raises \exception{URLError} on errors.
|
|---|
| 39 |
|
|---|
| 40 | Note that \code{None} may be returned if no handler handles the
|
|---|
| 41 | request (though the default installed global \class{OpenerDirector}
|
|---|
| 42 | uses \class{UnknownHandler} to ensure this never happens).
|
|---|
| 43 | \end{funcdesc}
|
|---|
| 44 |
|
|---|
| 45 | \begin{funcdesc}{install_opener}{opener}
|
|---|
| 46 | Install an \class{OpenerDirector} instance as the default global
|
|---|
| 47 | opener. Installing an opener is only necessary if you want urlopen to
|
|---|
| 48 | use that opener; otherwise, simply call \method{OpenerDirector.open()}
|
|---|
| 49 | instead of \function{urlopen()}. The code does not check for a real
|
|---|
| 50 | \class{OpenerDirector}, and any class with the appropriate interface
|
|---|
| 51 | will work.
|
|---|
| 52 | \end{funcdesc}
|
|---|
| 53 |
|
|---|
| 54 | \begin{funcdesc}{build_opener}{\optional{handler, \moreargs}}
|
|---|
| 55 | Return an \class{OpenerDirector} instance, which chains the
|
|---|
| 56 | handlers in the order given. \var{handler}s can be either instances
|
|---|
| 57 | of \class{BaseHandler}, or subclasses of \class{BaseHandler} (in
|
|---|
| 58 | which case it must be possible to call the constructor without
|
|---|
| 59 | any parameters). Instances of the following classes will be in
|
|---|
| 60 | front of the \var{handler}s, unless the \var{handler}s contain
|
|---|
| 61 | them, instances of them or subclasses of them:
|
|---|
| 62 | \class{ProxyHandler}, \class{UnknownHandler}, \class{HTTPHandler},
|
|---|
| 63 | \class{HTTPDefaultErrorHandler}, \class{HTTPRedirectHandler},
|
|---|
| 64 | \class{FTPHandler}, \class{FileHandler}, \class{HTTPErrorProcessor}.
|
|---|
| 65 |
|
|---|
| 66 | If the Python installation has SSL support (\function{socket.ssl()}
|
|---|
| 67 | exists), \class{HTTPSHandler} will also be added.
|
|---|
| 68 |
|
|---|
| 69 | Beginning in Python 2.3, a \class{BaseHandler} subclass may also
|
|---|
| 70 | change its \member{handler_order} member variable to modify its
|
|---|
| 71 | position in the handlers list.
|
|---|
| 72 | \end{funcdesc}
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | The following exceptions are raised as appropriate:
|
|---|
| 76 |
|
|---|
| 77 | \begin{excdesc}{URLError}
|
|---|
| 78 | The handlers raise this exception (or derived exceptions) when they
|
|---|
| 79 | run into a problem. It is a subclass of \exception{IOError}.
|
|---|
| 80 | \end{excdesc}
|
|---|
| 81 |
|
|---|
| 82 | \begin{excdesc}{HTTPError}
|
|---|
| 83 | A subclass of \exception{URLError}, it can also function as a
|
|---|
| 84 | non-exceptional file-like return value (the same thing that
|
|---|
| 85 | \function{urlopen()} returns). This is useful when handling exotic
|
|---|
| 86 | HTTP errors, such as requests for authentication.
|
|---|
| 87 | \end{excdesc}
|
|---|
| 88 |
|
|---|
| 89 | \begin{excdesc}{GopherError}
|
|---|
| 90 | A subclass of \exception{URLError}, this is the error raised by the
|
|---|
| 91 | Gopher handler.
|
|---|
| 92 | \end{excdesc}
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | The following classes are provided:
|
|---|
| 96 |
|
|---|
| 97 | \begin{classdesc}{Request}{url\optional{, data}\optional{, headers}
|
|---|
| 98 | \optional{, origin_req_host}\optional{, unverifiable}}
|
|---|
| 99 | This class is an abstraction of a URL request.
|
|---|
| 100 |
|
|---|
| 101 | \var{url} should be a string containing a valid URL.
|
|---|
| 102 |
|
|---|
| 103 | \var{data} may be a string specifying additional data to send to the
|
|---|
| 104 | server, or \code{None} if no such data is needed.
|
|---|
| 105 | Currently HTTP requests are the only ones that use \var{data};
|
|---|
| 106 | the HTTP request will be a POST instead of a GET when the \var{data}
|
|---|
| 107 | parameter is provided. \var{data} should be a buffer in the standard
|
|---|
| 108 | \mimetype{application/x-www-form-urlencoded} format. The
|
|---|
| 109 | \function{urllib.urlencode()} function takes a mapping or sequence of
|
|---|
| 110 | 2-tuples and returns a string in this format.
|
|---|
| 111 |
|
|---|
| 112 | \var{headers} should be a dictionary, and will be treated as if
|
|---|
| 113 | \method{add_header()} was called with each key and value as arguments.
|
|---|
| 114 |
|
|---|
| 115 | The final two arguments are only of interest for correct handling of
|
|---|
| 116 | third-party HTTP cookies:
|
|---|
| 117 |
|
|---|
| 118 | \var{origin_req_host} should be the request-host of the origin
|
|---|
| 119 | transaction, as defined by \rfc{2965}. It defaults to
|
|---|
| 120 | \code{cookielib.request_host(self)}. This is the host name or IP
|
|---|
| 121 | address of the original request that was initiated by the user. For
|
|---|
| 122 | example, if the request is for an image in an HTML document, this
|
|---|
| 123 | should be the request-host of the request for the page containing the
|
|---|
| 124 | image.
|
|---|
| 125 |
|
|---|
| 126 | \var{unverifiable} should indicate whether the request is
|
|---|
| 127 | unverifiable, as defined by RFC 2965. It defaults to False. An
|
|---|
| 128 | unverifiable request is one whose URL the user did not have the option
|
|---|
| 129 | to approve. For example, if the request is for an image in an HTML
|
|---|
| 130 | document, and the user had no option to approve the automatic fetching
|
|---|
| 131 | of the image, this should be true.
|
|---|
| 132 | \end{classdesc}
|
|---|
| 133 |
|
|---|
| 134 | \begin{classdesc}{OpenerDirector}{}
|
|---|
| 135 | The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
|
|---|
| 136 | chained together. It manages the chaining of handlers, and recovery
|
|---|
| 137 | from errors.
|
|---|
| 138 | \end{classdesc}
|
|---|
| 139 |
|
|---|
| 140 | \begin{classdesc}{BaseHandler}{}
|
|---|
| 141 | This is the base class for all registered handlers --- and handles only
|
|---|
| 142 | the simple mechanics of registration.
|
|---|
| 143 | \end{classdesc}
|
|---|
| 144 |
|
|---|
| 145 | \begin{classdesc}{HTTPDefaultErrorHandler}{}
|
|---|
| 146 | A class which defines a default handler for HTTP error responses; all
|
|---|
| 147 | responses are turned into \exception{HTTPError} exceptions.
|
|---|
| 148 | \end{classdesc}
|
|---|
| 149 |
|
|---|
| 150 | \begin{classdesc}{HTTPRedirectHandler}{}
|
|---|
| 151 | A class to handle redirections.
|
|---|
| 152 | \end{classdesc}
|
|---|
| 153 |
|
|---|
| 154 | \begin{classdesc}{HTTPCookieProcessor}{\optional{cookiejar}}
|
|---|
| 155 | A class to handle HTTP Cookies.
|
|---|
| 156 | \end{classdesc}
|
|---|
| 157 |
|
|---|
| 158 | \begin{classdesc}{ProxyHandler}{\optional{proxies}}
|
|---|
| 159 | Cause requests to go through a proxy.
|
|---|
| 160 | If \var{proxies} is given, it must be a dictionary mapping
|
|---|
| 161 | protocol names to URLs of proxies.
|
|---|
| 162 | The default is to read the list of proxies from the environment
|
|---|
| 163 | variables \envvar{<protocol>_proxy}.
|
|---|
| 164 | \end{classdesc}
|
|---|
| 165 |
|
|---|
| 166 | \begin{classdesc}{HTTPPasswordMgr}{}
|
|---|
| 167 | Keep a database of
|
|---|
| 168 | \code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
|
|---|
| 169 | mappings.
|
|---|
| 170 | \end{classdesc}
|
|---|
| 171 |
|
|---|
| 172 | \begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
|
|---|
| 173 | Keep a database of
|
|---|
| 174 | \code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
|
|---|
| 175 | A realm of \code{None} is considered a catch-all realm, which is searched
|
|---|
| 176 | if no other realm fits.
|
|---|
| 177 | \end{classdesc}
|
|---|
| 178 |
|
|---|
| 179 | \begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
|
|---|
| 180 | This is a mixin class that helps with HTTP authentication, both
|
|---|
| 181 | to the remote host and to a proxy.
|
|---|
| 182 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 183 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 184 | for information on the interface that must be supported.
|
|---|
| 185 | \end{classdesc}
|
|---|
| 186 |
|
|---|
| 187 | \begin{classdesc}{HTTPBasicAuthHandler}{\optional{password_mgr}}
|
|---|
| 188 | Handle authentication with the remote host.
|
|---|
| 189 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 190 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 191 | for information on the interface that must be supported.
|
|---|
| 192 | \end{classdesc}
|
|---|
| 193 |
|
|---|
| 194 | \begin{classdesc}{ProxyBasicAuthHandler}{\optional{password_mgr}}
|
|---|
| 195 | Handle authentication with the proxy.
|
|---|
| 196 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 197 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 198 | for information on the interface that must be supported.
|
|---|
| 199 | \end{classdesc}
|
|---|
| 200 |
|
|---|
| 201 | \begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
|
|---|
| 202 | This is a mixin class that helps with HTTP authentication, both
|
|---|
| 203 | to the remote host and to a proxy.
|
|---|
| 204 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 205 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 206 | for information on the interface that must be supported.
|
|---|
| 207 | \end{classdesc}
|
|---|
| 208 |
|
|---|
| 209 | \begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
|
|---|
| 210 | Handle authentication with the remote host.
|
|---|
| 211 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 212 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 213 | for information on the interface that must be supported.
|
|---|
| 214 | \end{classdesc}
|
|---|
| 215 |
|
|---|
| 216 | \begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
|
|---|
| 217 | Handle authentication with the proxy.
|
|---|
| 218 | \var{password_mgr}, if given, should be something that is compatible
|
|---|
| 219 | with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
|
|---|
| 220 | for information on the interface that must be supported.
|
|---|
| 221 | \end{classdesc}
|
|---|
| 222 |
|
|---|
| 223 | \begin{classdesc}{HTTPHandler}{}
|
|---|
| 224 | A class to handle opening of HTTP URLs.
|
|---|
| 225 | \end{classdesc}
|
|---|
| 226 |
|
|---|
| 227 | \begin{classdesc}{HTTPSHandler}{}
|
|---|
| 228 | A class to handle opening of HTTPS URLs.
|
|---|
| 229 | \end{classdesc}
|
|---|
| 230 |
|
|---|
| 231 | \begin{classdesc}{FileHandler}{}
|
|---|
| 232 | Open local files.
|
|---|
| 233 | \end{classdesc}
|
|---|
| 234 |
|
|---|
| 235 | \begin{classdesc}{FTPHandler}{}
|
|---|
| 236 | Open FTP URLs.
|
|---|
| 237 | \end{classdesc}
|
|---|
| 238 |
|
|---|
| 239 | \begin{classdesc}{CacheFTPHandler}{}
|
|---|
| 240 | Open FTP URLs, keeping a cache of open FTP connections to minimize
|
|---|
| 241 | delays.
|
|---|
| 242 | \end{classdesc}
|
|---|
| 243 |
|
|---|
| 244 | \begin{classdesc}{GopherHandler}{}
|
|---|
| 245 | Open gopher URLs.
|
|---|
| 246 | \end{classdesc}
|
|---|
| 247 |
|
|---|
| 248 | \begin{classdesc}{UnknownHandler}{}
|
|---|
| 249 | A catch-all class to handle unknown URLs.
|
|---|
| 250 | \end{classdesc}
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 | \subsection{Request Objects \label{request-objects}}
|
|---|
| 254 |
|
|---|
| 255 | The following methods describe all of \class{Request}'s public interface,
|
|---|
| 256 | and so all must be overridden in subclasses.
|
|---|
| 257 |
|
|---|
| 258 | \begin{methoddesc}[Request]{add_data}{data}
|
|---|
| 259 | Set the \class{Request} data to \var{data}. This is ignored by all
|
|---|
| 260 | handlers except HTTP handlers --- and there it should be a byte
|
|---|
| 261 | string, and will change the request to be \code{POST} rather than
|
|---|
| 262 | \code{GET}.
|
|---|
| 263 | \end{methoddesc}
|
|---|
| 264 |
|
|---|
| 265 | \begin{methoddesc}[Request]{get_method}{}
|
|---|
| 266 | Return a string indicating the HTTP request method. This is only
|
|---|
| 267 | meaningful for HTTP requests, and currently always returns
|
|---|
| 268 | \code{'GET'} or \code{'POST'}.
|
|---|
| 269 | \end{methoddesc}
|
|---|
| 270 |
|
|---|
| 271 | \begin{methoddesc}[Request]{has_data}{}
|
|---|
| 272 | Return whether the instance has a non-\code{None} data.
|
|---|
| 273 | \end{methoddesc}
|
|---|
| 274 |
|
|---|
| 275 | \begin{methoddesc}[Request]{get_data}{}
|
|---|
| 276 | Return the instance's data.
|
|---|
| 277 | \end{methoddesc}
|
|---|
| 278 |
|
|---|
| 279 | \begin{methoddesc}[Request]{add_header}{key, val}
|
|---|
| 280 | Add another header to the request. Headers are currently ignored by
|
|---|
| 281 | all handlers except HTTP handlers, where they are added to the list
|
|---|
| 282 | of headers sent to the server. Note that there cannot be more than
|
|---|
| 283 | one header with the same name, and later calls will overwrite
|
|---|
| 284 | previous calls in case the \var{key} collides. Currently, this is
|
|---|
| 285 | no loss of HTTP functionality, since all headers which have meaning
|
|---|
| 286 | when used more than once have a (header-specific) way of gaining the
|
|---|
| 287 | same functionality using only one header.
|
|---|
| 288 | \end{methoddesc}
|
|---|
| 289 |
|
|---|
| 290 | \begin{methoddesc}[Request]{add_unredirected_header}{key, header}
|
|---|
| 291 | Add a header that will not be added to a redirected request.
|
|---|
| 292 | \versionadded{2.4}
|
|---|
| 293 | \end{methoddesc}
|
|---|
| 294 |
|
|---|
| 295 | \begin{methoddesc}[Request]{has_header}{header}
|
|---|
| 296 | Return whether the instance has the named header (checks both regular
|
|---|
| 297 | and unredirected).
|
|---|
| 298 | \versionadded{2.4}
|
|---|
| 299 | \end{methoddesc}
|
|---|
| 300 |
|
|---|
| 301 | \begin{methoddesc}[Request]{get_full_url}{}
|
|---|
| 302 | Return the URL given in the constructor.
|
|---|
| 303 | \end{methoddesc}
|
|---|
| 304 |
|
|---|
| 305 | \begin{methoddesc}[Request]{get_type}{}
|
|---|
| 306 | Return the type of the URL --- also known as the scheme.
|
|---|
| 307 | \end{methoddesc}
|
|---|
| 308 |
|
|---|
| 309 | \begin{methoddesc}[Request]{get_host}{}
|
|---|
| 310 | Return the host to which a connection will be made.
|
|---|
| 311 | \end{methoddesc}
|
|---|
| 312 |
|
|---|
| 313 | \begin{methoddesc}[Request]{get_selector}{}
|
|---|
| 314 | Return the selector --- the part of the URL that is sent to
|
|---|
| 315 | the server.
|
|---|
| 316 | \end{methoddesc}
|
|---|
| 317 |
|
|---|
| 318 | \begin{methoddesc}[Request]{set_proxy}{host, type}
|
|---|
| 319 | Prepare the request by connecting to a proxy server. The \var{host}
|
|---|
| 320 | and \var{type} will replace those of the instance, and the instance's
|
|---|
| 321 | selector will be the original URL given in the constructor.
|
|---|
| 322 | \end{methoddesc}
|
|---|
| 323 |
|
|---|
| 324 | \begin{methoddesc}[Request]{get_origin_req_host}{}
|
|---|
| 325 | Return the request-host of the origin transaction, as defined by
|
|---|
| 326 | \rfc{2965}. See the documentation for the \class{Request}
|
|---|
| 327 | constructor.
|
|---|
| 328 | \end{methoddesc}
|
|---|
| 329 |
|
|---|
| 330 | \begin{methoddesc}[Request]{is_unverifiable}{}
|
|---|
| 331 | Return whether the request is unverifiable, as defined by RFC 2965.
|
|---|
| 332 | See the documentation for the \class{Request} constructor.
|
|---|
| 333 | \end{methoddesc}
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | \subsection{OpenerDirector Objects \label{opener-director-objects}}
|
|---|
| 337 |
|
|---|
| 338 | \class{OpenerDirector} instances have the following methods:
|
|---|
| 339 |
|
|---|
| 340 | \begin{methoddesc}[OpenerDirector]{add_handler}{handler}
|
|---|
| 341 | \var{handler} should be an instance of \class{BaseHandler}. The
|
|---|
| 342 | following methods are searched, and added to the possible chains (note
|
|---|
| 343 | that HTTP errors are a special case).
|
|---|
| 344 |
|
|---|
| 345 | \begin{itemize}
|
|---|
| 346 | \item \method{\var{protocol}_open()} ---
|
|---|
| 347 | signal that the handler knows how to open \var{protocol} URLs.
|
|---|
| 348 | \item \method{http_error_\var{type}()} ---
|
|---|
| 349 | signal that the handler knows how to handle HTTP errors with HTTP
|
|---|
| 350 | error code \var{type}.
|
|---|
| 351 | \item \method{\var{protocol}_error()} ---
|
|---|
| 352 | signal that the handler knows how to handle errors from
|
|---|
| 353 | (non-\code{http}) \var{protocol}.
|
|---|
| 354 | \item \method{\var{protocol}_request()} ---
|
|---|
| 355 | signal that the handler knows how to pre-process \var{protocol}
|
|---|
| 356 | requests.
|
|---|
| 357 | \item \method{\var{protocol}_response()} ---
|
|---|
| 358 | signal that the handler knows how to post-process \var{protocol}
|
|---|
| 359 | responses.
|
|---|
| 360 | \end{itemize}
|
|---|
| 361 | \end{methoddesc}
|
|---|
| 362 |
|
|---|
| 363 | \begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
|
|---|
| 364 | Open the given \var{url} (which can be a request object or a string),
|
|---|
| 365 | optionally passing the given \var{data}.
|
|---|
| 366 | Arguments, return values and exceptions raised are the same as those
|
|---|
| 367 | of \function{urlopen()} (which simply calls the \method{open()} method
|
|---|
| 368 | on the currently installed global \class{OpenerDirector}).
|
|---|
| 369 | \end{methoddesc}
|
|---|
| 370 |
|
|---|
| 371 | \begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
|
|---|
| 372 | arg\optional{, \moreargs}}}
|
|---|
| 373 | Handle an error of the given protocol. This will call the registered
|
|---|
| 374 | error handlers for the given protocol with the given arguments (which
|
|---|
| 375 | are protocol specific). The HTTP protocol is a special case which
|
|---|
| 376 | uses the HTTP response code to determine the specific error handler;
|
|---|
| 377 | refer to the \method{http_error_*()} methods of the handler classes.
|
|---|
| 378 |
|
|---|
| 379 | Return values and exceptions raised are the same as those
|
|---|
| 380 | of \function{urlopen()}.
|
|---|
| 381 | \end{methoddesc}
|
|---|
| 382 |
|
|---|
| 383 | OpenerDirector objects open URLs in three stages:
|
|---|
| 384 |
|
|---|
| 385 | The order in which these methods are called within each stage is
|
|---|
| 386 | determined by sorting the handler instances.
|
|---|
| 387 |
|
|---|
| 388 | \begin{enumerate}
|
|---|
| 389 | \item Every handler with a method named like
|
|---|
| 390 | \method{\var{protocol}_request()} has that method called to
|
|---|
| 391 | pre-process the request.
|
|---|
| 392 |
|
|---|
| 393 | \item Handlers with a method named like
|
|---|
| 394 | \method{\var{protocol}_open()} are called to handle the request.
|
|---|
| 395 | This stage ends when a handler either returns a
|
|---|
| 396 | non-\constant{None} value (ie. a response), or raises an exception
|
|---|
| 397 | (usually \exception{URLError}). Exceptions are allowed to propagate.
|
|---|
| 398 |
|
|---|
| 399 | In fact, the above algorithm is first tried for methods named
|
|---|
| 400 | \method{default_open}. If all such methods return
|
|---|
| 401 | \constant{None}, the algorithm is repeated for methods named like
|
|---|
| 402 | \method{\var{protocol}_open()}. If all such methods return
|
|---|
| 403 | \constant{None}, the algorithm is repeated for methods named
|
|---|
| 404 | \method{unknown_open()}.
|
|---|
| 405 |
|
|---|
| 406 | Note that the implementation of these methods may involve calls of
|
|---|
| 407 | the parent \class{OpenerDirector} instance's \method{.open()} and
|
|---|
| 408 | \method{.error()} methods.
|
|---|
| 409 |
|
|---|
| 410 | \item Every handler with a method named like
|
|---|
| 411 | \method{\var{protocol}_response()} has that method called to
|
|---|
| 412 | post-process the response.
|
|---|
| 413 |
|
|---|
| 414 | \end{enumerate}
|
|---|
| 415 |
|
|---|
| 416 | \subsection{BaseHandler Objects \label{base-handler-objects}}
|
|---|
| 417 |
|
|---|
| 418 | \class{BaseHandler} objects provide a couple of methods that are
|
|---|
| 419 | directly useful, and others that are meant to be used by derived
|
|---|
| 420 | classes. These are intended for direct use:
|
|---|
| 421 |
|
|---|
| 422 | \begin{methoddesc}[BaseHandler]{add_parent}{director}
|
|---|
| 423 | Add a director as parent.
|
|---|
| 424 | \end{methoddesc}
|
|---|
| 425 |
|
|---|
| 426 | \begin{methoddesc}[BaseHandler]{close}{}
|
|---|
| 427 | Remove any parents.
|
|---|
| 428 | \end{methoddesc}
|
|---|
| 429 |
|
|---|
| 430 | The following members and methods should only be used by classes
|
|---|
| 431 | derived from \class{BaseHandler}. \note{The convention has been
|
|---|
| 432 | adopted that subclasses defining \method{\var{protocol}_request()} or
|
|---|
| 433 | \method{\var{protocol}_response()} methods are named
|
|---|
| 434 | \class{*Processor}; all others are named \class{*Handler}.}
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 | \begin{memberdesc}[BaseHandler]{parent}
|
|---|
| 438 | A valid \class{OpenerDirector}, which can be used to open using a
|
|---|
| 439 | different protocol, or handle errors.
|
|---|
| 440 | \end{memberdesc}
|
|---|
| 441 |
|
|---|
| 442 | \begin{methoddesc}[BaseHandler]{default_open}{req}
|
|---|
| 443 | This method is \emph{not} defined in \class{BaseHandler}, but
|
|---|
| 444 | subclasses should define it if they want to catch all URLs.
|
|---|
| 445 |
|
|---|
| 446 | This method, if implemented, will be called by the parent
|
|---|
| 447 | \class{OpenerDirector}. It should return a file-like object as
|
|---|
| 448 | described in the return value of the \method{open()} of
|
|---|
| 449 | \class{OpenerDirector}, or \code{None}. It should raise
|
|---|
| 450 | \exception{URLError}, unless a truly exceptional thing happens (for
|
|---|
| 451 | example, \exception{MemoryError} should not be mapped to
|
|---|
| 452 | \exception{URLError}).
|
|---|
| 453 |
|
|---|
| 454 | This method will be called before any protocol-specific open method.
|
|---|
| 455 | \end{methoddesc}
|
|---|
| 456 |
|
|---|
| 457 | \begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
|
|---|
| 458 | This method is \emph{not} defined in \class{BaseHandler}, but
|
|---|
| 459 | subclasses should define it if they want to handle URLs with the given
|
|---|
| 460 | protocol.
|
|---|
| 461 |
|
|---|
| 462 | This method, if defined, will be called by the parent
|
|---|
| 463 | \class{OpenerDirector}. Return values should be the same as for
|
|---|
| 464 | \method{default_open()}.
|
|---|
| 465 | \end{methoddescni}
|
|---|
| 466 |
|
|---|
| 467 | \begin{methoddesc}[BaseHandler]{unknown_open}{req}
|
|---|
| 468 | This method is \var{not} defined in \class{BaseHandler}, but
|
|---|
| 469 | subclasses should define it if they want to catch all URLs with no
|
|---|
| 470 | specific registered handler to open it.
|
|---|
| 471 |
|
|---|
| 472 | This method, if implemented, will be called by the \member{parent}
|
|---|
| 473 | \class{OpenerDirector}. Return values should be the same as for
|
|---|
| 474 | \method{default_open()}.
|
|---|
| 475 | \end{methoddesc}
|
|---|
| 476 |
|
|---|
| 477 | \begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
|
|---|
| 478 | This method is \emph{not} defined in \class{BaseHandler}, but
|
|---|
| 479 | subclasses should override it if they intend to provide a catch-all
|
|---|
| 480 | for otherwise unhandled HTTP errors. It will be called automatically
|
|---|
| 481 | by the \class{OpenerDirector} getting the error, and should not
|
|---|
| 482 | normally be called in other circumstances.
|
|---|
| 483 |
|
|---|
| 484 | \var{req} will be a \class{Request} object, \var{fp} will be a
|
|---|
| 485 | file-like object with the HTTP error body, \var{code} will be the
|
|---|
| 486 | three-digit code of the error, \var{msg} will be the user-visible
|
|---|
| 487 | explanation of the code and \var{hdrs} will be a mapping object with
|
|---|
| 488 | the headers of the error.
|
|---|
| 489 |
|
|---|
| 490 | Return values and exceptions raised should be the same as those
|
|---|
| 491 | of \function{urlopen()}.
|
|---|
| 492 | \end{methoddesc}
|
|---|
| 493 |
|
|---|
| 494 | \begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
|
|---|
| 495 | \var{nnn} should be a three-digit HTTP error code. This method is
|
|---|
| 496 | also not defined in \class{BaseHandler}, but will be called, if it
|
|---|
| 497 | exists, on an instance of a subclass, when an HTTP error with code
|
|---|
| 498 | \var{nnn} occurs.
|
|---|
| 499 |
|
|---|
| 500 | Subclasses should override this method to handle specific HTTP
|
|---|
| 501 | errors.
|
|---|
| 502 |
|
|---|
| 503 | Arguments, return values and exceptions raised should be the same as
|
|---|
| 504 | for \method{http_error_default()}.
|
|---|
| 505 | \end{methoddesc}
|
|---|
| 506 |
|
|---|
| 507 | \begin{methoddescni}[BaseHandler]{\var{protocol}_request}{req}
|
|---|
| 508 | This method is \emph{not} defined in \class{BaseHandler}, but
|
|---|
| 509 | subclasses should define it if they want to pre-process requests of
|
|---|
| 510 | the given protocol.
|
|---|
| 511 |
|
|---|
| 512 | This method, if defined, will be called by the parent
|
|---|
| 513 | \class{OpenerDirector}. \var{req} will be a \class{Request} object.
|
|---|
| 514 | The return value should be a \class{Request} object.
|
|---|
| 515 | \end{methoddescni}
|
|---|
| 516 |
|
|---|
| 517 | \begin{methoddescni}[BaseHandler]{\var{protocol}_response}{req, response}
|
|---|
| 518 | This method is \emph{not} defined in \class{BaseHandler}, but
|
|---|
| 519 | subclasses should define it if they want to post-process responses of
|
|---|
| 520 | the given protocol.
|
|---|
| 521 |
|
|---|
| 522 | This method, if defined, will be called by the parent
|
|---|
| 523 | \class{OpenerDirector}. \var{req} will be a \class{Request} object.
|
|---|
| 524 | \var{response} will be an object implementing the same interface as
|
|---|
| 525 | the return value of \function{urlopen()}. The return value should
|
|---|
| 526 | implement the same interface as the return value of
|
|---|
| 527 | \function{urlopen()}.
|
|---|
| 528 | \end{methoddescni}
|
|---|
| 529 |
|
|---|
| 530 | \subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
|
|---|
| 531 |
|
|---|
| 532 | \note{Some HTTP redirections require action from this module's client
|
|---|
| 533 | code. If this is the case, \exception{HTTPError} is raised. See
|
|---|
| 534 | \rfc{2616} for details of the precise meanings of the various
|
|---|
| 535 | redirection codes.}
|
|---|
| 536 |
|
|---|
| 537 | \begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
|
|---|
| 538 | fp, code, msg, hdrs}
|
|---|
| 539 | Return a \class{Request} or \code{None} in response to a redirect.
|
|---|
| 540 | This is called by the default implementations of the
|
|---|
| 541 | \method{http_error_30*()} methods when a redirection is received from
|
|---|
| 542 | the server. If a redirection should take place, return a new
|
|---|
| 543 | \class{Request} to allow \method{http_error_30*()} to perform the
|
|---|
| 544 | redirect. Otherwise, raise \exception{HTTPError} if no other handler
|
|---|
| 545 | should try to handle this URL, or return \code{None} if you can't but
|
|---|
| 546 | another handler might.
|
|---|
| 547 |
|
|---|
| 548 | \begin{notice}
|
|---|
| 549 | The default implementation of this method does not strictly
|
|---|
| 550 | follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
|
|---|
| 551 | requests must not be automatically redirected without confirmation by
|
|---|
| 552 | the user. In reality, browsers do allow automatic redirection of
|
|---|
| 553 | these responses, changing the POST to a \code{GET}, and the default
|
|---|
| 554 | implementation reproduces this behavior.
|
|---|
| 555 | \end{notice}
|
|---|
| 556 | \end{methoddesc}
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
|
|---|
| 560 | fp, code, msg, hdrs}
|
|---|
| 561 | Redirect to the \code{Location:} URL. This method is called by
|
|---|
| 562 | the parent \class{OpenerDirector} when getting an HTTP
|
|---|
| 563 | `moved permanently' response.
|
|---|
| 564 | \end{methoddesc}
|
|---|
| 565 |
|
|---|
| 566 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
|
|---|
| 567 | fp, code, msg, hdrs}
|
|---|
| 568 | The same as \method{http_error_301()}, but called for the
|
|---|
| 569 | `found' response.
|
|---|
| 570 | \end{methoddesc}
|
|---|
| 571 |
|
|---|
| 572 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
|
|---|
| 573 | fp, code, msg, hdrs}
|
|---|
| 574 | The same as \method{http_error_301()}, but called for the
|
|---|
| 575 | `see other' response.
|
|---|
| 576 | \end{methoddesc}
|
|---|
| 577 |
|
|---|
| 578 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
|
|---|
| 579 | fp, code, msg, hdrs}
|
|---|
| 580 | The same as \method{http_error_301()}, but called for the
|
|---|
| 581 | `temporary redirect' response.
|
|---|
| 582 | \end{methoddesc}
|
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 | \subsection{HTTPCookieProcessor Objects \label{http-cookie-processor}}
|
|---|
| 586 |
|
|---|
| 587 | \versionadded{2.4}
|
|---|
| 588 |
|
|---|
| 589 | \class{HTTPCookieProcessor} instances have one attribute:
|
|---|
| 590 |
|
|---|
| 591 | \begin{memberdesc}{cookiejar}
|
|---|
| 592 | The \class{cookielib.CookieJar} in which cookies are stored.
|
|---|
| 593 | \end{memberdesc}
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 | \subsection{ProxyHandler Objects \label{proxy-handler}}
|
|---|
| 597 |
|
|---|
| 598 | \begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
|
|---|
| 599 | The \class{ProxyHandler} will have a method
|
|---|
| 600 | \method{\var{protocol}_open()} for every \var{protocol} which has a
|
|---|
| 601 | proxy in the \var{proxies} dictionary given in the constructor. The
|
|---|
| 602 | method will modify requests to go through the proxy, by calling
|
|---|
| 603 | \code{request.set_proxy()}, and call the next handler in the chain to
|
|---|
| 604 | actually execute the protocol.
|
|---|
| 605 | \end{methoddescni}
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 | \subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
|
|---|
| 609 |
|
|---|
| 610 | These methods are available on \class{HTTPPasswordMgr} and
|
|---|
| 611 | \class{HTTPPasswordMgrWithDefaultRealm} objects.
|
|---|
| 612 |
|
|---|
| 613 | \begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
|
|---|
| 614 | \var{uri} can be either a single URI, or a sequence of URIs. \var{realm},
|
|---|
| 615 | \var{user} and \var{passwd} must be strings. This causes
|
|---|
| 616 | \code{(\var{user}, \var{passwd})} to be used as authentication tokens
|
|---|
| 617 | when authentication for \var{realm} and a super-URI of any of the
|
|---|
| 618 | given URIs is given.
|
|---|
| 619 | \end{methoddesc}
|
|---|
| 620 |
|
|---|
| 621 | \begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
|
|---|
| 622 | Get user/password for given realm and URI, if any. This method will
|
|---|
| 623 | return \code{(None, None)} if there is no matching user/password.
|
|---|
| 624 |
|
|---|
| 625 | For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
|
|---|
| 626 | \code{None} will be searched if the given \var{realm} has no matching
|
|---|
| 627 | user/password.
|
|---|
| 628 | \end{methoddesc}
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 | \subsection{AbstractBasicAuthHandler Objects
|
|---|
| 632 | \label{abstract-basic-auth-handler}}
|
|---|
| 633 |
|
|---|
| 634 | \begin{methoddesc}[AbstractBasicAuthHandler]{http_error_auth_reqed}
|
|---|
| 635 | {authreq, host, req, headers}
|
|---|
| 636 | Handle an authentication request by getting a user/password pair, and
|
|---|
| 637 | re-trying the request. \var{authreq} should be the name of the header
|
|---|
| 638 | where the information about the realm is included in the request,
|
|---|
| 639 | \var{host} specifies the URL and path to authenticate for, \var{req}
|
|---|
| 640 | should be the (failed) \class{Request} object, and \var{headers}
|
|---|
| 641 | should be the error headers.
|
|---|
| 642 |
|
|---|
| 643 | \var{host} is either an authority (e.g. \code{"python.org"}) or a URL
|
|---|
| 644 | containing an authority component (e.g. \code{"http://python.org/"}).
|
|---|
| 645 | In either case, the authority must not contain a userinfo component
|
|---|
| 646 | (so, \code{"python.org"} and \code{"python.org:80"} are fine,
|
|---|
| 647 | \code{"joe:[email protected]"} is not).
|
|---|
| 648 | \end{methoddesc}
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 | \subsection{HTTPBasicAuthHandler Objects
|
|---|
| 652 | \label{http-basic-auth-handler}}
|
|---|
| 653 |
|
|---|
| 654 | \begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
|
|---|
| 655 | msg, hdrs}
|
|---|
| 656 | Retry the request with authentication information, if available.
|
|---|
| 657 | \end{methoddesc}
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 | \subsection{ProxyBasicAuthHandler Objects
|
|---|
| 661 | \label{proxy-basic-auth-handler}}
|
|---|
| 662 |
|
|---|
| 663 | \begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
|
|---|
| 664 | msg, hdrs}
|
|---|
| 665 | Retry the request with authentication information, if available.
|
|---|
| 666 | \end{methoddesc}
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 | \subsection{AbstractDigestAuthHandler Objects
|
|---|
| 670 | \label{abstract-digest-auth-handler}}
|
|---|
| 671 |
|
|---|
|
|---|