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