blob: 0020b6c9c3ab2370056db615bc73d862d006cd6b [file] [log] [blame]
Moshe Zadka8a18e992001-03-01 08:40:42 +00001\section{\module{urllib2} ---
2 extensible library for opening URLs}
3
4\declaremodule{standard}{urllib2}
Moshe Zadka8a18e992001-03-01 08:40:42 +00005\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
11The \module{urllib2} module defines functions and classes which help
Fred Drake93c86712001-03-02 20:39:34 +000012in opening URLs (mostly HTTP) in a complex world --- basic and digest
Moshe Zadka8a18e992001-03-01 08:40:42 +000013authentication, redirections and more.
14
15The \module{urllib2} module defines the following functions:
16
17\begin{funcdesc}{urlopen}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +000018Open the URL \var{url}, which can be either a string or a \class{Request}
Moshe Zadka8a18e992001-03-01 08:40:42 +000019object (currently the code checks that it really is a \class{Request}
Fred Drake93c86712001-03-02 20:39:34 +000020instance, or an instance of a subclass of \class{Request}).
Moshe Zadka8a18e992001-03-01 08:40:42 +000021
22\var{data} should be a string, which specifies additional data to
23send to the server. In HTTP requests, which are the only ones that
24support \var{data}, it should be a buffer in the format of
Fred Drake93c86712001-03-02 20:39:34 +000025\mimetype{application/x-www-form-urlencoded}, for example one returned
26from \function{urllib.urlencode()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000027
28This function returns a file-like object with two additional methods:
29
30\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +000031 \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 Zadka8a18e992001-03-01 08:40:42 +000034\end{itemize}
35
36Raises \exception{URLError} on errors.
37\end{funcdesc}
38
39\begin{funcdesc}{install_opener}{opener}
Fred Drake399bc8c2001-11-09 03:49:29 +000040Install an \class{OpenerDirector} instance as the default opener.
Moshe Zadka8a18e992001-03-01 08:40:42 +000041The code does not check for a real \class{OpenerDirector}, and any
42class with the appropriate interface will work.
43\end{funcdesc}
44
Fred Drake93c86712001-03-02 20:39:34 +000045\begin{funcdesc}{build_opener}{\optional{handler, \moreargs}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000046Return an \class{OpenerDirector} instance, which chains the
47handlers in the order given. \var{handler}s can be either instances
48of \class{BaseHandler}, or subclasses of \class{BaseHandler} (in
49which case it must be possible to call the constructor without
Fred Drake399bc8c2001-11-09 03:49:29 +000050any parameters). Instances of the following classes will be in
51front of the \var{handler}s, unless the \var{handler}s contain
Moshe Zadka8a18e992001-03-01 08:40:42 +000052them, instances of them or subclasses of them:
Fred Draked9cf8e72003-07-14 21:07:05 +000053\class{ProxyHandler}, \class{UnknownHandler}, \class{HTTPHandler},
54\class{HTTPDefaultErrorHandler}, \class{HTTPRedirectHandler},
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +000055\class{FTPHandler}, \class{FileHandler}, \class{HTTPErrorProcessor}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000056
Fred Drake93c86712001-03-02 20:39:34 +000057If the Python installation has SSL support (\function{socket.ssl()}
58exists), \class{HTTPSHandler} will also be added.
Gustavo Niemeyer9556fba2003-06-07 17:53:08 +000059
Fred Draked9cf8e72003-07-14 21:07:05 +000060Beginning in Python 2.3, a \class{BaseHandler} subclass may also
61change its \member{handler_order} member variable to modify its
62position in the handlers list. Besides \class{ProxyHandler}, which has
63\member{handler_order} of \code{100}, all handlers currently have it
64set to \code{500}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000065\end{funcdesc}
66
Fred Drake93c86712001-03-02 20:39:34 +000067
68The following exceptions are raised as appropriate:
69
Moshe Zadka8a18e992001-03-01 08:40:42 +000070\begin{excdesc}{URLError}
Fred Drake399bc8c2001-11-09 03:49:29 +000071The handlers raise this exception (or derived exceptions) when they
72run into a problem. It is a subclass of \exception{IOError}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000073\end{excdesc}
74
75\begin{excdesc}{HTTPError}
76A subclass of \exception{URLError}, it can also function as a
Fred Drake93c86712001-03-02 20:39:34 +000077non-exceptional file-like return value (the same thing that
78\function{urlopen()} returns). This is useful when handling exotic
79HTTP errors, such as requests for authentication.
Moshe Zadka8a18e992001-03-01 08:40:42 +000080\end{excdesc}
81
82\begin{excdesc}{GopherError}
83A subclass of \exception{URLError}, this is the error raised by the
84Gopher handler.
85\end{excdesc}
86
Fred Drake93c86712001-03-02 20:39:34 +000087
88The following classes are provided:
89
90\begin{classdesc}{Request}{url\optional{, data\optional{, headers}}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000091This class is an abstraction of a URL request.
92
Fred Drake399bc8c2001-11-09 03:49:29 +000093\var{url} should be a string which is a valid URL. For a description
Fred Drake93c86712001-03-02 20:39:34 +000094of \var{data} see the \method{add_data()} description.
Moshe Zadka8a18e992001-03-01 08:40:42 +000095\var{headers} should be a dictionary, and will be treated as if
Fred Drake93c86712001-03-02 20:39:34 +000096\method{add_header()} was called with each key and value as arguments.
Moshe Zadka8a18e992001-03-01 08:40:42 +000097\end{classdesc}
98
Fred Drake93c86712001-03-02 20:39:34 +000099\begin{classdesc}{OpenerDirector}{}
100The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
101chained together. It manages the chaining of handlers, and recovery
102from errors.
103\end{classdesc}
104
105\begin{classdesc}{BaseHandler}{}
106This is the base class for all registered handlers --- and handles only
107the simple mechanics of registration.
108\end{classdesc}
109
110\begin{classdesc}{HTTPDefaultErrorHandler}{}
111A class which defines a default handler for HTTP error responses; all
112responses are turned into \exception{HTTPError} exceptions.
113\end{classdesc}
114
115\begin{classdesc}{HTTPRedirectHandler}{}
116A class to handle redirections.
117\end{classdesc}
118
119\begin{classdesc}{ProxyHandler}{\optional{proxies}}
120Cause requests to go through a proxy.
121If \var{proxies} is given, it must be a dictionary mapping
122protocol names to URLs of proxies.
123The default is to read the list of proxies from the environment
Fred Drake47852462001-05-11 15:46:45 +0000124variables \var{protocol}_proxy.
Fred Drake93c86712001-03-02 20:39:34 +0000125\end{classdesc}
126
127\begin{classdesc}{HTTPPasswordMgr}{}
128Keep a database of
129\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
130mappings.
131\end{classdesc}
132
133\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
134Keep a database of
135\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
136A realm of \code{None} is considered a catch-all realm, which is searched
137if no other realm fits.
138\end{classdesc}
139
140\begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
141This is a mixin class that helps with HTTP authentication, both
142to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000143\var{password_mgr}, if given, should be something that is compatible
144with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
145for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000146\end{classdesc}
147
148\begin{classdesc}{HTTPBasicAuthHandler}{\optional{password_mgr}}
149Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000150\var{password_mgr}, if given, should be something that is compatible
151with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
152for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000153\end{classdesc}
154
155\begin{classdesc}{ProxyBasicAuthHandler}{\optional{password_mgr}}
156Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000157\var{password_mgr}, if given, should be something that is compatible
158with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
159for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000160\end{classdesc}
161
162\begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000163This is a mixin class that helps with HTTP authentication, both
Fred Drake93c86712001-03-02 20:39:34 +0000164to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000165\var{password_mgr}, if given, should be something that is compatible
166with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
167for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000168\end{classdesc}
169
170\begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
171Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000172\var{password_mgr}, if given, should be something that is compatible
173with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
174for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000175\end{classdesc}
176
177\begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
178Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000179\var{password_mgr}, if given, should be something that is compatible
180with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
181for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000182\end{classdesc}
183
184\begin{classdesc}{HTTPHandler}{}
185A class to handle opening of HTTP URLs.
186\end{classdesc}
187
188\begin{classdesc}{HTTPSHandler}{}
189A class to handle opening of HTTPS URLs.
190\end{classdesc}
191
192\begin{classdesc}{FileHandler}{}
193Open local files.
194\end{classdesc}
195
196\begin{classdesc}{FTPHandler}{}
197Open FTP URLs.
198\end{classdesc}
199
200\begin{classdesc}{CacheFTPHandler}{}
201Open FTP URLs, keeping a cache of open FTP connections to minimize
202delays.
203\end{classdesc}
204
205\begin{classdesc}{GopherHandler}{}
206Open gopher URLs.
207\end{classdesc}
208
209\begin{classdesc}{UnknownHandler}{}
210A catch-all class to handle unknown URLs.
211\end{classdesc}
212
213
214\subsection{Request Objects \label{request-objects}}
215
Moshe Zadka8a18e992001-03-01 08:40:42 +0000216The following methods describe all of \class{Request}'s public interface,
217and so all must be overridden in subclasses.
218
219\begin{methoddesc}[Request]{add_data}{data}
Fred Drake399bc8c2001-11-09 03:49:29 +0000220Set the \class{Request} data to \var{data}. This is ignored
Moshe Zadka8a18e992001-03-01 08:40:42 +0000221by all handlers except HTTP handlers --- and there it should be an
Fred Drake93c86712001-03-02 20:39:34 +0000222\mimetype{application/x-www-form-encoded} buffer, and will change the
Fred Drake399bc8c2001-11-09 03:49:29 +0000223request to be \code{POST} rather than \code{GET}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000224\end{methoddesc}
225
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000226\begin{methoddesc}[Request]{get_method}{}
227Return a string indicating the HTTP request method. This is only
228meaningful for HTTP requests, and currently always takes one of the
229values ("GET", "POST").
230\end{methoddesc}
231
Fred Drake399bc8c2001-11-09 03:49:29 +0000232\begin{methoddesc}[Request]{has_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000233Return whether the instance has a non-\code{None} data.
234\end{methoddesc}
235
Fred Drake399bc8c2001-11-09 03:49:29 +0000236\begin{methoddesc}[Request]{get_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000237Return the instance's data.
238\end{methoddesc}
239
240\begin{methoddesc}[Request]{add_header}{key, val}
Fred Drake93c86712001-03-02 20:39:34 +0000241Add another header to the request. Headers are currently ignored by
242all handlers except HTTP handlers, where they are added to the list
Fred Drake399bc8c2001-11-09 03:49:29 +0000243of headers sent to the server. Note that there cannot be more than
Fred Drake93c86712001-03-02 20:39:34 +0000244one header with the same name, and later calls will overwrite
245previous calls in case the \var{key} collides. Currently, this is
246no loss of HTTP functionality, since all headers which have meaning
Fred Drake399bc8c2001-11-09 03:49:29 +0000247when used more than once have a (header-specific) way of gaining the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000248same functionality using only one header.
249\end{methoddesc}
250
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000251\begin{methoddesc}[Request]{add_unredirected_header}{key, header}
252Add a header that will not be added to a redirected request.
253\end{methoddesc}
254
255\begin{methoddesc}[Request]{has_header}{header}
256Return whether the instance has the named header (checks both regular
257and unredirected).
258\end{methoddesc}
259
Moshe Zadka8a18e992001-03-01 08:40:42 +0000260\begin{methoddesc}[Request]{get_full_url}{}
261Return the URL given in the constructor.
262\end{methoddesc}
263
264\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000265Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000266\end{methoddesc}
267
268\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000269Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000270\end{methoddesc}
271
272\begin{methoddesc}[Request]{get_selector}{}
273Return the selector --- the part of the URL that is sent to
274the server.
275\end{methoddesc}
276
277\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000278Prepare the request by connecting to a proxy server. The \var{host}
279and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000280selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000281\end{methoddesc}
282
Fred Drake93c86712001-03-02 20:39:34 +0000283
284\subsection{OpenerDirector Objects \label{opener-director-objects}}
285
286\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000287
288\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000289\var{handler} should be an instance of \class{BaseHandler}. The
290following methods are searched, and added to the possible chains.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000291
292\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000293 \item \method{\var{protocol}_open()} ---
294 signal that the handler knows how to open \var{protocol} URLs.
295 \item \method{\var{protocol}_error_\var{type}()} ---
296 signal that the handler knows how to handle \var{type} errors from
297 \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000298 \item \method{\var{protocol}_request()} ---
299 signal that the handler knows how to pre-process \var{protocol}
300 requests.
301 \item \method{\var{protocol}_response()} ---
302 signal that the handler knows how to post-process \var{protocol}
303 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000304\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000305\end{methoddesc}
306
307\begin{methoddesc}[OpenerDirector]{close}{}
308Explicitly break cycles, and delete all the handlers.
309Because the \class{OpenerDirector} needs to know the registered handlers,
310and a handler needs to know who the \class{OpenerDirector} who called
Fred Drake399bc8c2001-11-09 03:49:29 +0000311it is, there is a reference cycle. Even though recent versions of Python
Moshe Zadka8a18e992001-03-01 08:40:42 +0000312have cycle-collection, it is sometimes preferable to explicitly break
313the cycles.
314\end{methoddesc}
315
316\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000317Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000318optionally passing the given \var{data}.
319Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000320of \function{urlopen()} (which simply calls the \method{open()} method
Raymond Hettinger0dfd7a92003-05-10 07:40:56 +0000321on the default installed \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000322\end{methoddesc}
323
Fred Drake93c86712001-03-02 20:39:34 +0000324\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
325 arg\optional{, \moreargs}}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000326Handle an error in a given protocol. This will call the registered
327error handlers for the given protocol with the given arguments (which
328are protocol specific). The HTTP protocol is a special case which
329uses the HTTP response code to determine the specific error handler;
330refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000331
332Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000333of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000334\end{methoddesc}
335
Fred Drake93c86712001-03-02 20:39:34 +0000336
337\subsection{BaseHandler Objects \label{base-handler-objects}}
338
339\class{BaseHandler} objects provide a couple of methods that are
340directly useful, and others that are meant to be used by derived
341classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000342
343\begin{methoddesc}[BaseHandler]{add_parent}{director}
344Add a director as parent.
345\end{methoddesc}
346
347\begin{methoddesc}[BaseHandler]{close}{}
348Remove any parents.
349\end{methoddesc}
350
Fred Drake399bc8c2001-11-09 03:49:29 +0000351The following members and methods should only be used by classes
Fred Drake93c86712001-03-02 20:39:34 +0000352derived from \class{BaseHandler}:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000353
354\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000355A valid \class{OpenerDirector}, which can be used to open using a
356different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000357\end{memberdesc}
358
359\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000360This method is \emph{not} defined in \class{BaseHandler}, but
361subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000362
Fred Drake399bc8c2001-11-09 03:49:29 +0000363This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000364\class{OpenerDirector}. It should return a file-like object as
365described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000366\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000367\exception{URLError}, unless a truly exceptional thing happens (for
368example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000369\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000370
371This method will be called before any protocol-specific open method.
372\end{methoddesc}
373
Fred Drake47852462001-05-11 15:46:45 +0000374\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000375This method is \emph{not} defined in \class{BaseHandler}, but
376subclasses should define it if they want to handle URLs with the given
377protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000378
Fred Drake399bc8c2001-11-09 03:49:29 +0000379This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000380\class{OpenerDirector}. Return values should be the same as for
381\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000382\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000383
384\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000385This method is \var{not} defined in \class{BaseHandler}, but
386subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000387specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000388
Fred Drake399bc8c2001-11-09 03:49:29 +0000389This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000390\class{OpenerDirector}. Return values should be the same as for
391\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000392\end{methoddesc}
393
394\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000395This method is \emph{not} defined in \class{BaseHandler}, but
396subclasses should override it if they intend to provide a catch-all
397for otherwise unhandled HTTP errors. It will be called automatically
398by the \class{OpenerDirector} getting the error, and should not
399normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000400
Fred Drake93c86712001-03-02 20:39:34 +0000401\var{req} will be a \class{Request} object, \var{fp} will be a
402file-like object with the HTTP error body, \var{code} will be the
403three-digit code of the error, \var{msg} will be the user-visible
404explanation of the code and \var{hdrs} will be a mapping object with
405the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000406
407Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000408of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000409\end{methoddesc}
410
Fred Drake93c86712001-03-02 20:39:34 +0000411\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
412\var{nnn} should be a three-digit HTTP error code. This method is
413also not defined in \class{BaseHandler}, but will be called, if it
414exists, on an instance of a subclass, when an HTTP error with code
415\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000416
Fred Drake93c86712001-03-02 20:39:34 +0000417Subclasses should override this method to handle specific HTTP
418errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000419
Fred Drake93c86712001-03-02 20:39:34 +0000420Arguments, return values and exceptions raised should be the same as
421for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000422\end{methoddesc}
423
Fred Drake93c86712001-03-02 20:39:34 +0000424\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000425
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000426\note{Some HTTP redirections require action from this module's client
427 code. If this is the case, \exception{HTTPError} is raised. See
428 \rfc{2616} for details of the precise meanings of the various
429 redirection codes.}
430
431\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
432 fp, code, msg, hdrs}
433Return a \class{Request} or \code{None} in response to a redirect.
434This is called by the default implementations of the
Fred Draked9cf8e72003-07-14 21:07:05 +0000435\method{http_error_30*()} methods when a redirection is received
436from the server. If a redirection should take place, return a new
437\class{Request} to allow \method{http_error_30*()} to perform the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000438redirect. Otherwise, raise \exception{HTTPError} if no other
439\class{Handler} should try to handle this URL, or return \code{None}
440if you can't but another \class{Handler} might.
441
Fred Draked9cf8e72003-07-14 21:07:05 +0000442\begin{notice}
443 The default implementation of this method does not strictly
444 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000445 requests must not be automatically redirected without confirmation by
446 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000447 these responses, changing the POST to a \code{GET}, and the default
448 implementation reproduces this behavior.
449\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000450\end{methoddesc}
451
Moshe Zadka8a18e992001-03-01 08:40:42 +0000452
Fred Drake93c86712001-03-02 20:39:34 +0000453\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
454 fp, code, msg, hdrs}
455Redirect to the \code{Location:} URL. This method is called by
456the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000457`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000458\end{methoddesc}
459
Fred Drake93c86712001-03-02 20:39:34 +0000460\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
461 fp, code, msg, hdrs}
462The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000463`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000464\end{methoddesc}
465
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000466\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
467 fp, code, msg, hdrs}
468The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000469`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000470\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000471
Martin v. Löwis162f0812003-07-12 07:33:32 +0000472\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
473 fp, code, msg, hdrs}
474The same as \method{http_error_301()}, but called for the
475`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000476\end{methoddesc}
477
Martin v. Löwis162f0812003-07-12 07:33:32 +0000478
Fred Drake93c86712001-03-02 20:39:34 +0000479\subsection{ProxyHandler Objects \label{proxy-handler}}
480
Fred Drake47852462001-05-11 15:46:45 +0000481\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000482The \class{ProxyHandler} will have a method
483\method{\var{protocol}_open()} for every \var{protocol} which has a
484proxy in the \var{proxies} dictionary given in the constructor. The
485method will modify requests to go through the proxy, by calling
486\code{request.set_proxy()}, and call the next handler in the chain to
487actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000488\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000489
490
491\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
492
493These methods are available on \class{HTTPPasswordMgr} and
494\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000495
496\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
497\var{uri} can be either a single URI, or a sequene of URIs. \var{realm},
498\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000499\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000500when authentication for \var{realm} and a super-URI of any of the
501given URIs is given.
502\end{methoddesc}
503
504\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000505Get user/password for given realm and URI, if any. This method will
506return \code{(None, None)} if there is no matching user/password.
507
508For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
509\code{None} will be searched if the given \var{realm} has no matching
510user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000511\end{methoddesc}
512
Moshe Zadka8a18e992001-03-01 08:40:42 +0000513
Fred Drake93c86712001-03-02 20:39:34 +0000514\subsection{AbstractBasicAuthHandler Objects
515 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000516
517\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
518 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000519Handle an authentication request by getting a user/password pair, and
520re-trying the request. \var{authreq} should be the name of the header
521where the information about the realm is included in the request,
522\var{host} is the host to authenticate to, \var{req} should be the
523(failed) \class{Request} object, and \var{headers} should be the error
524headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000525\end{methoddesc}
526
Fred Drake93c86712001-03-02 20:39:34 +0000527
528\subsection{HTTPBasicAuthHandler Objects
529 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000530
531\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
532 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000533Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000534\end{methoddesc}
535
Fred Drake93c86712001-03-02 20:39:34 +0000536
537\subsection{ProxyBasicAuthHandler Objects
538 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000539
540\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
541 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000542Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000543\end{methoddesc}
544
Moshe Zadka8a18e992001-03-01 08:40:42 +0000545
Fred Drake93c86712001-03-02 20:39:34 +0000546\subsection{AbstractDigestAuthHandler Objects
547 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000548
Fred Drake93c86712001-03-02 20:39:34 +0000549\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000550 {authreq, host, req, headers}
551\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000552the realm is included in the request, \var{host} should be the host to
553authenticate to, \var{req} should be the (failed) \class{Request}
554object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000555\end{methoddesc}
556
Fred Drake93c86712001-03-02 20:39:34 +0000557
558\subsection{HTTPDigestAuthHandler Objects
559 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000560
561\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
562 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000563Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000564\end{methoddesc}
565
Fred Drake93c86712001-03-02 20:39:34 +0000566
567\subsection{ProxyDigestAuthHandler Objects
568 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000569
570\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
571 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000572Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000573\end{methoddesc}
574
Fred Drake93c86712001-03-02 20:39:34 +0000575
576\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000577
578\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000579Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000580\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000581\end{methoddesc}
582
Fred Drake93c86712001-03-02 20:39:34 +0000583
584\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000585
586\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000587Send an HTTPS request, which can be either GET or POST, depending on
588\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000589\end{methoddesc}
590
Moshe Zadka8a18e992001-03-01 08:40:42 +0000591
Fred Drake93c86712001-03-02 20:39:34 +0000592\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000593
594\begin{methoddesc}[FileHandler]{file_open}{req}
595Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000596the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000597protocol to \code{ftp} otherwise, and retry opening
598it using \member{parent}.
599\end{methoddesc}
600
Fred Drake93c86712001-03-02 20:39:34 +0000601
602\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000603
604\begin{methoddesc}[FTPHandler]{ftp_open}{req}
605Open the FTP file indicated by \var{req}.
606The login is always done with empty username and password.
607\end{methoddesc}
608
Moshe Zadka8a18e992001-03-01 08:40:42 +0000609
Fred Drake93c86712001-03-02 20:39:34 +0000610\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
611
612\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
613the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000614
615\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
616Set timeout of connections to \var{t} seconds.
617\end{methoddesc}
618
619\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
620Set maximum number of cached connections to \var{m}.
621\end{methoddesc}
622
Fred Drake93c86712001-03-02 20:39:34 +0000623
624\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000625
626\begin{methoddesc}[GopherHandler]{gopher_open}{req}
627Open the gopher resource indicated by \var{req}.
628\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000629
630
631\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
632
Fred Drakea9399112001-07-05 21:14:03 +0000633\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000634Raise a \exception{URLError} exception.
635\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000636
637
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000638\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
639
640\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
641Process HTTP error responses.
642
643For 200 error codes, the response object is returned immediately.
644
645For non-200 error codes, this simply passes the job on to the
646\method{\var{protocol}_error_\var{code}()} handler methods, via
647\method{OpenerDirector.error()}. Eventually,
648\class{urllib2.HTTPDefaultErrorHandler} will raise an
649\exception{HTTPError} if no other handler handles the error.
650\end{methoddesc}
651
652
Fred Drake53e5b712003-04-25 15:27:33 +0000653\subsection{Examples \label{urllib2-examples}}
654
655This example gets the python.org main page and displays the first 100
656bytes of it:
657
658\begin{verbatim}
659>>> import urllib2
660>>> f = urllib2.urlopen('http://www.python.org/')
661>>> print f.read(100)
662<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
663<?xml-stylesheet href="./css/ht2html
664\end{verbatim}
665
666Here we are sending a data-stream to the stdin of a CGI and reading
667the data it returns to us:
668
669\begin{verbatim}
670>>> import urllib2
671>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
672... data='This data is passed to stdin of the CGI')
673>>> f = urllib2.urlopen(req)
674>>> print f.read()
675Got Data: "This data is passed to stdin of the CGI"
676\end{verbatim}
677
678The code for the sample CGI used in the above example is:
679
680\begin{verbatim}
681#!/usr/bin/env python
682import sys
683data = sys.stdin.read()
684print 'Content-type: text-plain\n\nGot Data: "%s"' %
685data
686\end{verbatim}