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