blob: ae25e53499432931b7f2916f31d0d0a77bf17db9 [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.
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000253\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000254\end{methoddesc}
255
256\begin{methoddesc}[Request]{has_header}{header}
257Return whether the instance has the named header (checks both regular
258and unredirected).
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000259\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000260\end{methoddesc}
261
Moshe Zadka8a18e992001-03-01 08:40:42 +0000262\begin{methoddesc}[Request]{get_full_url}{}
263Return the URL given in the constructor.
264\end{methoddesc}
265
266\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000267Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000268\end{methoddesc}
269
270\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000271Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000272\end{methoddesc}
273
274\begin{methoddesc}[Request]{get_selector}{}
275Return the selector --- the part of the URL that is sent to
276the server.
277\end{methoddesc}
278
279\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000280Prepare the request by connecting to a proxy server. The \var{host}
281and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000282selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000283\end{methoddesc}
284
Fred Drake93c86712001-03-02 20:39:34 +0000285
286\subsection{OpenerDirector Objects \label{opener-director-objects}}
287
288\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000289
290\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000291\var{handler} should be an instance of \class{BaseHandler}. The
292following methods are searched, and added to the possible chains.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000293
294\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000295 \item \method{\var{protocol}_open()} ---
296 signal that the handler knows how to open \var{protocol} URLs.
297 \item \method{\var{protocol}_error_\var{type}()} ---
298 signal that the handler knows how to handle \var{type} errors from
299 \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000300 \item \method{\var{protocol}_request()} ---
301 signal that the handler knows how to pre-process \var{protocol}
302 requests.
303 \item \method{\var{protocol}_response()} ---
304 signal that the handler knows how to post-process \var{protocol}
305 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000306\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000307\end{methoddesc}
308
309\begin{methoddesc}[OpenerDirector]{close}{}
310Explicitly break cycles, and delete all the handlers.
311Because the \class{OpenerDirector} needs to know the registered handlers,
312and a handler needs to know who the \class{OpenerDirector} who called
Fred Drake399bc8c2001-11-09 03:49:29 +0000313it is, there is a reference cycle. Even though recent versions of Python
Moshe Zadka8a18e992001-03-01 08:40:42 +0000314have cycle-collection, it is sometimes preferable to explicitly break
315the cycles.
316\end{methoddesc}
317
318\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000319Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000320optionally passing the given \var{data}.
321Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000322of \function{urlopen()} (which simply calls the \method{open()} method
Raymond Hettinger0dfd7a92003-05-10 07:40:56 +0000323on the default installed \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000324\end{methoddesc}
325
Fred Drake93c86712001-03-02 20:39:34 +0000326\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
327 arg\optional{, \moreargs}}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000328Handle an error in a given protocol. This will call the registered
329error handlers for the given protocol with the given arguments (which
330are protocol specific). The HTTP protocol is a special case which
331uses the HTTP response code to determine the specific error handler;
332refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000333
334Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000335of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000336\end{methoddesc}
337
Fred Drake93c86712001-03-02 20:39:34 +0000338
339\subsection{BaseHandler Objects \label{base-handler-objects}}
340
341\class{BaseHandler} objects provide a couple of methods that are
342directly useful, and others that are meant to be used by derived
343classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000344
345\begin{methoddesc}[BaseHandler]{add_parent}{director}
346Add a director as parent.
347\end{methoddesc}
348
349\begin{methoddesc}[BaseHandler]{close}{}
350Remove any parents.
351\end{methoddesc}
352
Fred Drake399bc8c2001-11-09 03:49:29 +0000353The following members and methods should only be used by classes
Fred Drake93c86712001-03-02 20:39:34 +0000354derived from \class{BaseHandler}:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000355
356\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000357A valid \class{OpenerDirector}, which can be used to open using a
358different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000359\end{memberdesc}
360
361\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000362This method is \emph{not} defined in \class{BaseHandler}, but
363subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000364
Fred Drake399bc8c2001-11-09 03:49:29 +0000365This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000366\class{OpenerDirector}. It should return a file-like object as
367described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000368\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000369\exception{URLError}, unless a truly exceptional thing happens (for
370example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000371\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000372
373This method will be called before any protocol-specific open method.
374\end{methoddesc}
375
Fred Drake47852462001-05-11 15:46:45 +0000376\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000377This method is \emph{not} defined in \class{BaseHandler}, but
378subclasses should define it if they want to handle URLs with the given
379protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000380
Fred Drake399bc8c2001-11-09 03:49:29 +0000381This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000382\class{OpenerDirector}. Return values should be the same as for
383\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000384\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000385
386\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000387This method is \var{not} defined in \class{BaseHandler}, but
388subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000389specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000390
Fred Drake399bc8c2001-11-09 03:49:29 +0000391This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000392\class{OpenerDirector}. Return values should be the same as for
393\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000394\end{methoddesc}
395
396\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000397This method is \emph{not} defined in \class{BaseHandler}, but
398subclasses should override it if they intend to provide a catch-all
399for otherwise unhandled HTTP errors. It will be called automatically
400by the \class{OpenerDirector} getting the error, and should not
401normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000402
Fred Drake93c86712001-03-02 20:39:34 +0000403\var{req} will be a \class{Request} object, \var{fp} will be a
404file-like object with the HTTP error body, \var{code} will be the
405three-digit code of the error, \var{msg} will be the user-visible
406explanation of the code and \var{hdrs} will be a mapping object with
407the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000408
409Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000410of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000411\end{methoddesc}
412
Fred Drake93c86712001-03-02 20:39:34 +0000413\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
414\var{nnn} should be a three-digit HTTP error code. This method is
415also not defined in \class{BaseHandler}, but will be called, if it
416exists, on an instance of a subclass, when an HTTP error with code
417\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000418
Fred Drake93c86712001-03-02 20:39:34 +0000419Subclasses should override this method to handle specific HTTP
420errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000421
Fred Drake93c86712001-03-02 20:39:34 +0000422Arguments, return values and exceptions raised should be the same as
423for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000424\end{methoddesc}
425
Fred Drake93c86712001-03-02 20:39:34 +0000426\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000427
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000428\note{Some HTTP redirections require action from this module's client
429 code. If this is the case, \exception{HTTPError} is raised. See
430 \rfc{2616} for details of the precise meanings of the various
431 redirection codes.}
432
433\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
434 fp, code, msg, hdrs}
435Return a \class{Request} or \code{None} in response to a redirect.
436This is called by the default implementations of the
Fred Draked9cf8e72003-07-14 21:07:05 +0000437\method{http_error_30*()} methods when a redirection is received
438from the server. If a redirection should take place, return a new
439\class{Request} to allow \method{http_error_30*()} to perform the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000440redirect. Otherwise, raise \exception{HTTPError} if no other
441\class{Handler} should try to handle this URL, or return \code{None}
442if you can't but another \class{Handler} might.
443
Fred Draked9cf8e72003-07-14 21:07:05 +0000444\begin{notice}
445 The default implementation of this method does not strictly
446 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000447 requests must not be automatically redirected without confirmation by
448 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000449 these responses, changing the POST to a \code{GET}, and the default
450 implementation reproduces this behavior.
451\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000452\end{methoddesc}
453
Moshe Zadka8a18e992001-03-01 08:40:42 +0000454
Fred Drake93c86712001-03-02 20:39:34 +0000455\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
456 fp, code, msg, hdrs}
457Redirect to the \code{Location:} URL. This method is called by
458the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000459`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000460\end{methoddesc}
461
Fred Drake93c86712001-03-02 20:39:34 +0000462\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
463 fp, code, msg, hdrs}
464The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000465`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000466\end{methoddesc}
467
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000468\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
469 fp, code, msg, hdrs}
470The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000471`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000472\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000473
Martin v. Löwis162f0812003-07-12 07:33:32 +0000474\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
475 fp, code, msg, hdrs}
476The same as \method{http_error_301()}, but called for the
477`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000478\end{methoddesc}
479
Martin v. Löwis162f0812003-07-12 07:33:32 +0000480
Fred Drake93c86712001-03-02 20:39:34 +0000481\subsection{ProxyHandler Objects \label{proxy-handler}}
482
Fred Drake47852462001-05-11 15:46:45 +0000483\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000484The \class{ProxyHandler} will have a method
485\method{\var{protocol}_open()} for every \var{protocol} which has a
486proxy in the \var{proxies} dictionary given in the constructor. The
487method will modify requests to go through the proxy, by calling
488\code{request.set_proxy()}, and call the next handler in the chain to
489actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000490\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000491
492
493\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
494
495These methods are available on \class{HTTPPasswordMgr} and
496\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000497
498\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
499\var{uri} can be either a single URI, or a sequene of URIs. \var{realm},
500\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000501\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000502when authentication for \var{realm} and a super-URI of any of the
503given URIs is given.
504\end{methoddesc}
505
506\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000507Get user/password for given realm and URI, if any. This method will
508return \code{(None, None)} if there is no matching user/password.
509
510For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
511\code{None} will be searched if the given \var{realm} has no matching
512user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000513\end{methoddesc}
514
Moshe Zadka8a18e992001-03-01 08:40:42 +0000515
Fred Drake93c86712001-03-02 20:39:34 +0000516\subsection{AbstractBasicAuthHandler Objects
517 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000518
519\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
520 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000521Handle an authentication request by getting a user/password pair, and
522re-trying the request. \var{authreq} should be the name of the header
523where the information about the realm is included in the request,
524\var{host} is the host to authenticate to, \var{req} should be the
525(failed) \class{Request} object, and \var{headers} should be the error
526headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000527\end{methoddesc}
528
Fred Drake93c86712001-03-02 20:39:34 +0000529
530\subsection{HTTPBasicAuthHandler Objects
531 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000532
533\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
534 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000535Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000536\end{methoddesc}
537
Fred Drake93c86712001-03-02 20:39:34 +0000538
539\subsection{ProxyBasicAuthHandler Objects
540 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000541
542\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
543 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000544Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000545\end{methoddesc}
546
Moshe Zadka8a18e992001-03-01 08:40:42 +0000547
Fred Drake93c86712001-03-02 20:39:34 +0000548\subsection{AbstractDigestAuthHandler Objects
549 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000550
Fred Drake93c86712001-03-02 20:39:34 +0000551\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000552 {authreq, host, req, headers}
553\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000554the realm is included in the request, \var{host} should be the host to
555authenticate to, \var{req} should be the (failed) \class{Request}
556object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000557\end{methoddesc}
558
Fred Drake93c86712001-03-02 20:39:34 +0000559
560\subsection{HTTPDigestAuthHandler Objects
561 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000562
563\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
564 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000565Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000566\end{methoddesc}
567
Fred Drake93c86712001-03-02 20:39:34 +0000568
569\subsection{ProxyDigestAuthHandler Objects
570 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000571
572\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
573 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000574Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000575\end{methoddesc}
576
Fred Drake93c86712001-03-02 20:39:34 +0000577
578\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000579
580\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000581Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000582\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000583\end{methoddesc}
584
Fred Drake93c86712001-03-02 20:39:34 +0000585
586\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000587
588\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000589Send an HTTPS request, which can be either GET or POST, depending on
590\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000591\end{methoddesc}
592
Moshe Zadka8a18e992001-03-01 08:40:42 +0000593
Fred Drake93c86712001-03-02 20:39:34 +0000594\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000595
596\begin{methoddesc}[FileHandler]{file_open}{req}
597Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000598the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000599protocol to \code{ftp} otherwise, and retry opening
600it using \member{parent}.
601\end{methoddesc}
602
Fred Drake93c86712001-03-02 20:39:34 +0000603
604\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000605
606\begin{methoddesc}[FTPHandler]{ftp_open}{req}
607Open the FTP file indicated by \var{req}.
608The login is always done with empty username and password.
609\end{methoddesc}
610
Moshe Zadka8a18e992001-03-01 08:40:42 +0000611
Fred Drake93c86712001-03-02 20:39:34 +0000612\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
613
614\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
615the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000616
617\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
618Set timeout of connections to \var{t} seconds.
619\end{methoddesc}
620
621\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
622Set maximum number of cached connections to \var{m}.
623\end{methoddesc}
624
Fred Drake93c86712001-03-02 20:39:34 +0000625
626\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000627
628\begin{methoddesc}[GopherHandler]{gopher_open}{req}
629Open the gopher resource indicated by \var{req}.
630\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000631
632
633\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
634
Fred Drakea9399112001-07-05 21:14:03 +0000635\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000636Raise a \exception{URLError} exception.
637\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000638
639
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000640\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
641
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000642\versionadded{2.4}
643
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000644\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
645Process HTTP error responses.
646
647For 200 error codes, the response object is returned immediately.
648
649For non-200 error codes, this simply passes the job on to the
650\method{\var{protocol}_error_\var{code}()} handler methods, via
651\method{OpenerDirector.error()}. Eventually,
652\class{urllib2.HTTPDefaultErrorHandler} will raise an
653\exception{HTTPError} if no other handler handles the error.
654\end{methoddesc}
655
656
Fred Drake53e5b712003-04-25 15:27:33 +0000657\subsection{Examples \label{urllib2-examples}}
658
659This example gets the python.org main page and displays the first 100
660bytes of it:
661
662\begin{verbatim}
663>>> import urllib2
664>>> f = urllib2.urlopen('http://www.python.org/')
665>>> print f.read(100)
666<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
667<?xml-stylesheet href="./css/ht2html
668\end{verbatim}
669
670Here we are sending a data-stream to the stdin of a CGI and reading
671the data it returns to us:
672
673\begin{verbatim}
674>>> import urllib2
675>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
676... data='This data is passed to stdin of the CGI')
677>>> f = urllib2.urlopen(req)
678>>> print f.read()
679Got Data: "This data is passed to stdin of the CGI"
680\end{verbatim}
681
682The code for the sample CGI used in the above example is:
683
684\begin{verbatim}
685#!/usr/bin/env python
686import sys
687data = sys.stdin.read()
Raymond Hettinger5de33782004-02-08 20:25:01 +0000688print 'Content-type: text-plain\n\nGot Data: "%s"' % data
Fred Drake53e5b712003-04-25 15:27:33 +0000689\end{verbatim}