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