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}, |
| 55 | \class{FTPHandler}, \class{FileHandler} |
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 | |
| 251 | \begin{methoddesc}[Request]{get_full_url}{} |
| 252 | Return the URL given in the constructor. |
| 253 | \end{methoddesc} |
| 254 | |
| 255 | \begin{methoddesc}[Request]{get_type}{} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 256 | Return the type of the URL --- also known as the scheme. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 257 | \end{methoddesc} |
| 258 | |
| 259 | \begin{methoddesc}[Request]{get_host}{} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 260 | Return the host to which a connection will be made. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 261 | \end{methoddesc} |
| 262 | |
| 263 | \begin{methoddesc}[Request]{get_selector}{} |
| 264 | Return the selector --- the part of the URL that is sent to |
| 265 | the server. |
| 266 | \end{methoddesc} |
| 267 | |
| 268 | \begin{methoddesc}[Request]{set_proxy}{host, type} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 269 | Prepare the request by connecting to a proxy server. The \var{host} |
| 270 | 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] | 271 | selector will be the original URL given in the constructor. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 272 | \end{methoddesc} |
| 273 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 274 | |
| 275 | \subsection{OpenerDirector Objects \label{opener-director-objects}} |
| 276 | |
| 277 | \class{OpenerDirector} instances have the following methods: |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 278 | |
| 279 | \begin{methoddesc}[OpenerDirector]{add_handler}{handler} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 280 | \var{handler} should be an instance of \class{BaseHandler}. The |
| 281 | following methods are searched, and added to the possible chains. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 282 | |
| 283 | \begin{itemize} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 284 | \item \method{\var{protocol}_open()} --- |
| 285 | signal that the handler knows how to open \var{protocol} URLs. |
| 286 | \item \method{\var{protocol}_error_\var{type}()} --- |
| 287 | signal that the handler knows how to handle \var{type} errors from |
| 288 | \var{protocol}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 289 | \end{itemize} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 290 | \end{methoddesc} |
| 291 | |
| 292 | \begin{methoddesc}[OpenerDirector]{close}{} |
| 293 | Explicitly break cycles, and delete all the handlers. |
| 294 | Because the \class{OpenerDirector} needs to know the registered handlers, |
| 295 | and a handler needs to know who the \class{OpenerDirector} who called |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 296 | 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] | 297 | have cycle-collection, it is sometimes preferable to explicitly break |
| 298 | the cycles. |
| 299 | \end{methoddesc} |
| 300 | |
| 301 | \begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 302 | 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] | 303 | optionally passing the given \var{data}. |
| 304 | Arguments, return values and exceptions raised are the same as those |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 305 | of \function{urlopen()} (which simply calls the \method{open()} method |
Raymond Hettinger | 0dfd7a9 | 2003-05-10 07:40:56 +0000 | [diff] [blame] | 306 | on the default installed \class{OpenerDirector}). |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 307 | \end{methoddesc} |
| 308 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 309 | \begin{methoddesc}[OpenerDirector]{error}{proto\optional{, |
| 310 | arg\optional{, \moreargs}}} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 311 | Handle an error in a given protocol. This will call the registered |
| 312 | error handlers for the given protocol with the given arguments (which |
| 313 | are protocol specific). The HTTP protocol is a special case which |
| 314 | uses the HTTP response code to determine the specific error handler; |
| 315 | refer to the \method{http_error_*()} methods of the handler classes. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 316 | |
| 317 | Return values and exceptions raised are the same as those |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 318 | of \function{urlopen()}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 319 | \end{methoddesc} |
| 320 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 321 | |
| 322 | \subsection{BaseHandler Objects \label{base-handler-objects}} |
| 323 | |
| 324 | \class{BaseHandler} objects provide a couple of methods that are |
| 325 | directly useful, and others that are meant to be used by derived |
| 326 | classes. These are intended for direct use: |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 327 | |
| 328 | \begin{methoddesc}[BaseHandler]{add_parent}{director} |
| 329 | Add a director as parent. |
| 330 | \end{methoddesc} |
| 331 | |
| 332 | \begin{methoddesc}[BaseHandler]{close}{} |
| 333 | Remove any parents. |
| 334 | \end{methoddesc} |
| 335 | |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 336 | The following members and methods should only be used by classes |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 337 | derived from \class{BaseHandler}: |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 338 | |
| 339 | \begin{memberdesc}[BaseHandler]{parent} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 340 | A valid \class{OpenerDirector}, which can be used to open using a |
| 341 | different protocol, or handle errors. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 342 | \end{memberdesc} |
| 343 | |
| 344 | \begin{methoddesc}[BaseHandler]{default_open}{req} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 345 | This method is \emph{not} defined in \class{BaseHandler}, but |
| 346 | subclasses should define it if they want to catch all URLs. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 347 | |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 348 | This method, if implemented, will be called by the parent |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 349 | \class{OpenerDirector}. It should return a file-like object as |
| 350 | described in the return value of the \method{open()} of |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 351 | \class{OpenerDirector}, or \code{None}. It should raise |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 352 | \exception{URLError}, unless a truly exceptional thing happens (for |
| 353 | example, \exception{MemoryError} should not be mapped to |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 354 | \exception{URLError}). |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 355 | |
| 356 | This method will be called before any protocol-specific open method. |
| 357 | \end{methoddesc} |
| 358 | |
Fred Drake | 4785246 | 2001-05-11 15:46:45 +0000 | [diff] [blame] | 359 | \begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 360 | This method is \emph{not} defined in \class{BaseHandler}, but |
| 361 | subclasses should define it if they want to handle URLs with the given |
| 362 | protocol. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 363 | |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 364 | This method, if defined, will be called by the parent |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 365 | \class{OpenerDirector}. Return values should be the same as for |
| 366 | \method{default_open()}. |
Fred Drake | 4785246 | 2001-05-11 15:46:45 +0000 | [diff] [blame] | 367 | \end{methoddescni} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 368 | |
| 369 | \begin{methoddesc}[BaseHandler]{unknown_open}{req} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 370 | This method is \var{not} defined in \class{BaseHandler}, but |
| 371 | 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] | 372 | specific registered handler to open it. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 373 | |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 374 | This method, if implemented, will be called by the \member{parent} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 375 | \class{OpenerDirector}. Return values should be the same as for |
| 376 | \method{default_open()}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 377 | \end{methoddesc} |
| 378 | |
| 379 | \begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 380 | This method is \emph{not} defined in \class{BaseHandler}, but |
| 381 | subclasses should override it if they intend to provide a catch-all |
| 382 | for otherwise unhandled HTTP errors. It will be called automatically |
| 383 | by the \class{OpenerDirector} getting the error, and should not |
| 384 | normally be called in other circumstances. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 385 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 386 | \var{req} will be a \class{Request} object, \var{fp} will be a |
| 387 | file-like object with the HTTP error body, \var{code} will be the |
| 388 | three-digit code of the error, \var{msg} will be the user-visible |
| 389 | explanation of the code and \var{hdrs} will be a mapping object with |
| 390 | the headers of the error. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 391 | |
| 392 | Return values and exceptions raised should be the same as those |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 393 | of \function{urlopen()}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 394 | \end{methoddesc} |
| 395 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 396 | \begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs} |
| 397 | \var{nnn} should be a three-digit HTTP error code. This method is |
| 398 | also not defined in \class{BaseHandler}, but will be called, if it |
| 399 | exists, on an instance of a subclass, when an HTTP error with code |
| 400 | \var{nnn} occurs. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 401 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 402 | Subclasses should override this method to handle specific HTTP |
| 403 | errors. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 404 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 405 | Arguments, return values and exceptions raised should be the same as |
| 406 | for \method{http_error_default()}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 407 | \end{methoddesc} |
| 408 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 409 | \subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 410 | |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 411 | \note{Some HTTP redirections require action from this module's client |
| 412 | code. If this is the case, \exception{HTTPError} is raised. See |
| 413 | \rfc{2616} for details of the precise meanings of the various |
| 414 | redirection codes.} |
| 415 | |
| 416 | \begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req, |
| 417 | fp, code, msg, hdrs} |
| 418 | Return a \class{Request} or \code{None} in response to a redirect. |
| 419 | This is called by the default implementations of the |
Fred Drake | d9cf8e7 | 2003-07-14 21:07:05 +0000 | [diff] [blame] | 420 | \method{http_error_30*()} methods when a redirection is received |
| 421 | from the server. If a redirection should take place, return a new |
| 422 | \class{Request} to allow \method{http_error_30*()} to perform the |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 423 | redirect. Otherwise, raise \exception{HTTPError} if no other |
| 424 | \class{Handler} should try to handle this URL, or return \code{None} |
| 425 | if you can't but another \class{Handler} might. |
| 426 | |
Fred Drake | d9cf8e7 | 2003-07-14 21:07:05 +0000 | [diff] [blame] | 427 | \begin{notice} |
| 428 | The default implementation of this method does not strictly |
| 429 | 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] | 430 | requests must not be automatically redirected without confirmation by |
| 431 | the user. In reality, browsers do allow automatic redirection of |
Fred Drake | d9cf8e7 | 2003-07-14 21:07:05 +0000 | [diff] [blame] | 432 | these responses, changing the POST to a \code{GET}, and the default |
| 433 | implementation reproduces this behavior. |
| 434 | \end{notice} |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 435 | \end{methoddesc} |
| 436 | |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 437 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 438 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req, |
| 439 | fp, code, msg, hdrs} |
| 440 | Redirect to the \code{Location:} URL. This method is called by |
| 441 | the parent \class{OpenerDirector} when getting an HTTP |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 442 | `moved permanently' response. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 443 | \end{methoddesc} |
| 444 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 445 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req, |
| 446 | fp, code, msg, hdrs} |
| 447 | The same as \method{http_error_301()}, but called for the |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 448 | `found' response. |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 449 | \end{methoddesc} |
| 450 | |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 451 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req, |
| 452 | fp, code, msg, hdrs} |
| 453 | 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] | 454 | `see other' response. |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 455 | \end{methoddesc} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 456 | |
Martin v. Löwis | 162f081 | 2003-07-12 07:33:32 +0000 | [diff] [blame] | 457 | \begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req, |
| 458 | fp, code, msg, hdrs} |
| 459 | The same as \method{http_error_301()}, but called for the |
| 460 | `temporary redirect' response. |
Fred Drake | 9753ae1 | 2003-07-14 20:53:57 +0000 | [diff] [blame] | 461 | \end{methoddesc} |
| 462 | |
Martin v. Löwis | 162f081 | 2003-07-12 07:33:32 +0000 | [diff] [blame] | 463 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 464 | \subsection{ProxyHandler Objects \label{proxy-handler}} |
| 465 | |
Fred Drake | 4785246 | 2001-05-11 15:46:45 +0000 | [diff] [blame] | 466 | \begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 467 | The \class{ProxyHandler} will have a method |
| 468 | \method{\var{protocol}_open()} for every \var{protocol} which has a |
| 469 | proxy in the \var{proxies} dictionary given in the constructor. The |
| 470 | method will modify requests to go through the proxy, by calling |
| 471 | \code{request.set_proxy()}, and call the next handler in the chain to |
| 472 | actually execute the protocol. |
Fred Drake | 4785246 | 2001-05-11 15:46:45 +0000 | [diff] [blame] | 473 | \end{methoddescni} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 474 | |
| 475 | |
| 476 | \subsection{HTTPPasswordMgr Objects \label{http-password-mgr}} |
| 477 | |
| 478 | These methods are available on \class{HTTPPasswordMgr} and |
| 479 | \class{HTTPPasswordMgrWithDefaultRealm} objects. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 480 | |
| 481 | \begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd} |
| 482 | \var{uri} can be either a single URI, or a sequene of URIs. \var{realm}, |
| 483 | \var{user} and \var{passwd} must be strings. This causes |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 484 | \code{(\var{user}, \var{passwd})} to be used as authentication tokens |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 485 | when authentication for \var{realm} and a super-URI of any of the |
| 486 | given URIs is given. |
| 487 | \end{methoddesc} |
| 488 | |
| 489 | \begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 490 | Get user/password for given realm and URI, if any. This method will |
| 491 | return \code{(None, None)} if there is no matching user/password. |
| 492 | |
| 493 | For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm |
| 494 | \code{None} will be searched if the given \var{realm} has no matching |
| 495 | user/password. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 496 | \end{methoddesc} |
| 497 | |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 498 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 499 | \subsection{AbstractBasicAuthHandler Objects |
| 500 | \label{abstract-basic-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 501 | |
| 502 | \begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request} |
| 503 | {authreq, host, req, headers} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 504 | Handle an authentication request by getting a user/password pair, and |
| 505 | re-trying the request. \var{authreq} should be the name of the header |
| 506 | where the information about the realm is included in the request, |
| 507 | \var{host} is the host to authenticate to, \var{req} should be the |
| 508 | (failed) \class{Request} object, and \var{headers} should be the error |
| 509 | headers. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 510 | \end{methoddesc} |
| 511 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 512 | |
| 513 | \subsection{HTTPBasicAuthHandler Objects |
| 514 | \label{http-basic-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 515 | |
| 516 | \begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code, |
| 517 | msg, hdrs} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 518 | Retry the request with authentication information, if available. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 519 | \end{methoddesc} |
| 520 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 521 | |
| 522 | \subsection{ProxyBasicAuthHandler Objects |
| 523 | \label{proxy-basic-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 524 | |
| 525 | \begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code, |
| 526 | msg, hdrs} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 527 | Retry the request with authentication information, if available. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 528 | \end{methoddesc} |
| 529 | |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 530 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 531 | \subsection{AbstractDigestAuthHandler Objects |
| 532 | \label{abstract-digest-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 533 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 534 | \begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 535 | {authreq, host, req, headers} |
| 536 | \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] | 537 | the realm is included in the request, \var{host} should be the host to |
| 538 | authenticate to, \var{req} should be the (failed) \class{Request} |
| 539 | object, and \var{headers} should be the error headers. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 540 | \end{methoddesc} |
| 541 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 542 | |
| 543 | \subsection{HTTPDigestAuthHandler Objects |
| 544 | \label{http-digest-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 545 | |
| 546 | \begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code, |
| 547 | msg, hdrs} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 548 | Retry the request with authentication information, if available. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 549 | \end{methoddesc} |
| 550 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 551 | |
| 552 | \subsection{ProxyDigestAuthHandler Objects |
| 553 | \label{proxy-digest-auth-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 554 | |
| 555 | \begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code, |
| 556 | msg, hdrs} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 557 | Retry the request with authentication information, if available. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 558 | \end{methoddesc} |
| 559 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 560 | |
| 561 | \subsection{HTTPHandler Objects \label{http-handler-objects}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 562 | |
| 563 | \begin{methoddesc}[HTTPHandler]{http_open}{req} |
Fred Drake | 399bc8c | 2001-11-09 03:49:29 +0000 | [diff] [blame] | 564 | 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] | 565 | \code{\var{req}.has_data()}. |
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{HTTPSHandler Objects \label{https-handler-objects}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 570 | |
| 571 | \begin{methoddesc}[HTTPSHandler]{https_open}{req} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 572 | Send an HTTPS request, which can be either GET or POST, depending on |
| 573 | \code{\var{req}.has_data()}. |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 574 | \end{methoddesc} |
| 575 | |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 576 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 577 | \subsection{FileHandler Objects \label{file-handler-objects}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 578 | |
| 579 | \begin{methoddesc}[FileHandler]{file_open}{req} |
| 580 | Open the file locally, if there is no host name, or |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 581 | the host name is \code{'localhost'}. Change the |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 582 | protocol to \code{ftp} otherwise, and retry opening |
| 583 | it using \member{parent}. |
| 584 | \end{methoddesc} |
| 585 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 586 | |
| 587 | \subsection{FTPHandler Objects \label{ftp-handler-objects}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 588 | |
| 589 | \begin{methoddesc}[FTPHandler]{ftp_open}{req} |
| 590 | Open the FTP file indicated by \var{req}. |
| 591 | The login is always done with empty username and password. |
| 592 | \end{methoddesc} |
| 593 | |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 594 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 595 | \subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}} |
| 596 | |
| 597 | \class{CacheFTPHandler} objects are \class{FTPHandler} objects with |
| 598 | the following additional methods: |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 599 | |
| 600 | \begin{methoddesc}[CacheFTPHandler]{setTimeout}{t} |
| 601 | Set timeout of connections to \var{t} seconds. |
| 602 | \end{methoddesc} |
| 603 | |
| 604 | \begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m} |
| 605 | Set maximum number of cached connections to \var{m}. |
| 606 | \end{methoddesc} |
| 607 | |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 608 | |
| 609 | \subsection{GopherHandler Objects \label{gopher-handler}} |
Moshe Zadka | 8a18e99 | 2001-03-01 08:40:42 +0000 | [diff] [blame] | 610 | |
| 611 | \begin{methoddesc}[GopherHandler]{gopher_open}{req} |
| 612 | Open the gopher resource indicated by \var{req}. |
| 613 | \end{methoddesc} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 614 | |
| 615 | |
| 616 | \subsection{UnknownHandler Objects \label{unknown-handler-objects}} |
| 617 | |
Fred Drake | a939911 | 2001-07-05 21:14:03 +0000 | [diff] [blame] | 618 | \begin{methoddesc}[UnknownHandler]{unknown_open}{} |
Fred Drake | 93c8671 | 2001-03-02 20:39:34 +0000 | [diff] [blame] | 619 | Raise a \exception{URLError} exception. |
| 620 | \end{methoddesc} |
Fred Drake | 53e5b71 | 2003-04-25 15:27:33 +0000 | [diff] [blame] | 621 | |
| 622 | |
| 623 | \subsection{Examples \label{urllib2-examples}} |
| 624 | |
| 625 | This example gets the python.org main page and displays the first 100 |
| 626 | bytes of it: |
| 627 | |
| 628 | \begin{verbatim} |
| 629 | >>> import urllib2 |
| 630 | >>> f = urllib2.urlopen('http://www.python.org/') |
| 631 | >>> print f.read(100) |
| 632 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 633 | <?xml-stylesheet href="./css/ht2html |
| 634 | \end{verbatim} |
| 635 | |
| 636 | Here we are sending a data-stream to the stdin of a CGI and reading |
| 637 | the data it returns to us: |
| 638 | |
| 639 | \begin{verbatim} |
| 640 | >>> import urllib2 |
| 641 | >>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi', |
| 642 | ... data='This data is passed to stdin of the CGI') |
| 643 | >>> f = urllib2.urlopen(req) |
| 644 | >>> print f.read() |
| 645 | Got Data: "This data is passed to stdin of the CGI" |
| 646 | \end{verbatim} |
| 647 | |
| 648 | The code for the sample CGI used in the above example is: |
| 649 | |
| 650 | \begin{verbatim} |
| 651 | #!/usr/bin/env python |
| 652 | import sys |
| 653 | data = sys.stdin.read() |
| 654 | print 'Content-type: text-plain\n\nGot Data: "%s"' % |
| 655 | data |
| 656 | \end{verbatim} |