blob: 3f8ff3ab169b5a038f013c1b8f887f34320b98bd [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},
55\class{FTPHandler}, \class{FileHandler}
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
251\begin{methoddesc}[Request]{get_full_url}{}
252Return the URL given in the constructor.
253\end{methoddesc}
254
255\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000256Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000257\end{methoddesc}
258
259\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000260Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000261\end{methoddesc}
262
263\begin{methoddesc}[Request]{get_selector}{}
264Return the selector --- the part of the URL that is sent to
265the server.
266\end{methoddesc}
267
268\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000269Prepare the request by connecting to a proxy server. The \var{host}
270and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000271selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000272\end{methoddesc}
273
Fred Drake93c86712001-03-02 20:39:34 +0000274
275\subsection{OpenerDirector Objects \label{opener-director-objects}}
276
277\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000278
279\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000280\var{handler} should be an instance of \class{BaseHandler}. The
281following methods are searched, and added to the possible chains.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000282
283\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000284 \item \method{\var{protocol}_open()} ---
285 signal that the handler knows how to open \var{protocol} URLs.
286 \item \method{\var{protocol}_error_\var{type}()} ---
287 signal that the handler knows how to handle \var{type} errors from
288 \var{protocol}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000289\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000290\end{methoddesc}
291
292\begin{methoddesc}[OpenerDirector]{close}{}
293Explicitly break cycles, and delete all the handlers.
294Because the \class{OpenerDirector} needs to know the registered handlers,
295and a handler needs to know who the \class{OpenerDirector} who called
Fred Drake399bc8c2001-11-09 03:49:29 +0000296it is, there is a reference cycle. Even though recent versions of Python
Moshe Zadka8a18e992001-03-01 08:40:42 +0000297have cycle-collection, it is sometimes preferable to explicitly break
298the cycles.
299\end{methoddesc}
300
301\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000302Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000303optionally passing the given \var{data}.
304Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000305of \function{urlopen()} (which simply calls the \method{open()} method
Raymond Hettinger0dfd7a92003-05-10 07:40:56 +0000306on the default installed \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000307\end{methoddesc}
308
Fred Drake93c86712001-03-02 20:39:34 +0000309\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
310 arg\optional{, \moreargs}}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000311Handle an error in a given protocol. This will call the registered
312error handlers for the given protocol with the given arguments (which
313are protocol specific). The HTTP protocol is a special case which
314uses the HTTP response code to determine the specific error handler;
315refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000316
317Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000318of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000319\end{methoddesc}
320
Fred Drake93c86712001-03-02 20:39:34 +0000321
322\subsection{BaseHandler Objects \label{base-handler-objects}}
323
324\class{BaseHandler} objects provide a couple of methods that are
325directly useful, and others that are meant to be used by derived
326classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000327
328\begin{methoddesc}[BaseHandler]{add_parent}{director}
329Add a director as parent.
330\end{methoddesc}
331
332\begin{methoddesc}[BaseHandler]{close}{}
333Remove any parents.
334\end{methoddesc}
335
Fred Drake399bc8c2001-11-09 03:49:29 +0000336The following members and methods should only be used by classes
Fred Drake93c86712001-03-02 20:39:34 +0000337derived from \class{BaseHandler}:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000338
339\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000340A valid \class{OpenerDirector}, which can be used to open using a
341different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000342\end{memberdesc}
343
344\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000345This method is \emph{not} defined in \class{BaseHandler}, but
346subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000347
Fred Drake399bc8c2001-11-09 03:49:29 +0000348This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000349\class{OpenerDirector}. It should return a file-like object as
350described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000351\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000352\exception{URLError}, unless a truly exceptional thing happens (for
353example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000354\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000355
356This method will be called before any protocol-specific open method.
357\end{methoddesc}
358
Fred Drake47852462001-05-11 15:46:45 +0000359\begin{methoddescni}[BaseHandler]{\var{protocol}_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 handle URLs with the given
362protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000363
Fred Drake399bc8c2001-11-09 03:49:29 +0000364This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000365\class{OpenerDirector}. Return values should be the same as for
366\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000367\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000368
369\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000370This method is \var{not} defined in \class{BaseHandler}, but
371subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000372specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000373
Fred Drake399bc8c2001-11-09 03:49:29 +0000374This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000375\class{OpenerDirector}. Return values should be the same as for
376\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000377\end{methoddesc}
378
379\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000380This method is \emph{not} defined in \class{BaseHandler}, but
381subclasses should override it if they intend to provide a catch-all
382for otherwise unhandled HTTP errors. It will be called automatically
383by the \class{OpenerDirector} getting the error, and should not
384normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000385
Fred Drake93c86712001-03-02 20:39:34 +0000386\var{req} will be a \class{Request} object, \var{fp} will be a
387file-like object with the HTTP error body, \var{code} will be the
388three-digit code of the error, \var{msg} will be the user-visible
389explanation of the code and \var{hdrs} will be a mapping object with
390the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000391
392Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000393of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000394\end{methoddesc}
395
Fred Drake93c86712001-03-02 20:39:34 +0000396\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
397\var{nnn} should be a three-digit HTTP error code. This method is
398also not defined in \class{BaseHandler}, but will be called, if it
399exists, on an instance of a subclass, when an HTTP error with code
400\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000401
Fred Drake93c86712001-03-02 20:39:34 +0000402Subclasses should override this method to handle specific HTTP
403errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000404
Fred Drake93c86712001-03-02 20:39:34 +0000405Arguments, return values and exceptions raised should be the same as
406for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000407\end{methoddesc}
408
Fred Drake93c86712001-03-02 20:39:34 +0000409\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000410
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000411\note{Some HTTP redirections require action from this module's client
412 code. If this is the case, \exception{HTTPError} is raised. See
413 \rfc{2616} for details of the precise meanings of the various
414 redirection codes.}
415
416\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
417 fp, code, msg, hdrs}
418Return a \class{Request} or \code{None} in response to a redirect.
419This is called by the default implementations of the
Fred Draked9cf8e72003-07-14 21:07:05 +0000420\method{http_error_30*()} methods when a redirection is received
421from the server. If a redirection should take place, return a new
422\class{Request} to allow \method{http_error_30*()} to perform the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000423redirect. Otherwise, raise \exception{HTTPError} if no other
424\class{Handler} should try to handle this URL, or return \code{None}
425if you can't but another \class{Handler} might.
426
Fred Draked9cf8e72003-07-14 21:07:05 +0000427\begin{notice}
428 The default implementation of this method does not strictly
429 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000430 requests must not be automatically redirected without confirmation by
431 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000432 these responses, changing the POST to a \code{GET}, and the default
433 implementation reproduces this behavior.
434\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000435\end{methoddesc}
436
Moshe Zadka8a18e992001-03-01 08:40:42 +0000437
Fred Drake93c86712001-03-02 20:39:34 +0000438\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
439 fp, code, msg, hdrs}
440Redirect to the \code{Location:} URL. This method is called by
441the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000442`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000443\end{methoddesc}
444
Fred Drake93c86712001-03-02 20:39:34 +0000445\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
446 fp, code, msg, hdrs}
447The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000448`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000449\end{methoddesc}
450
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000451\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
452 fp, code, msg, hdrs}
453The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000454`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000455\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000456
Martin v. Löwis162f0812003-07-12 07:33:32 +0000457\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
458 fp, code, msg, hdrs}
459The same as \method{http_error_301()}, but called for the
460`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000461\end{methoddesc}
462
Martin v. Löwis162f0812003-07-12 07:33:32 +0000463
Fred Drake93c86712001-03-02 20:39:34 +0000464\subsection{ProxyHandler Objects \label{proxy-handler}}
465
Fred Drake47852462001-05-11 15:46:45 +0000466\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000467The \class{ProxyHandler} will have a method
468\method{\var{protocol}_open()} for every \var{protocol} which has a
469proxy in the \var{proxies} dictionary given in the constructor. The
470method will modify requests to go through the proxy, by calling
471\code{request.set_proxy()}, and call the next handler in the chain to
472actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000473\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000474
475
476\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
477
478These methods are available on \class{HTTPPasswordMgr} and
479\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000480
481\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
482\var{uri} can be either a single URI, or a sequene of URIs. \var{realm},
483\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000484\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000485when authentication for \var{realm} and a super-URI of any of the
486given URIs is given.
487\end{methoddesc}
488
489\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000490Get user/password for given realm and URI, if any. This method will
491return \code{(None, None)} if there is no matching user/password.
492
493For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
494\code{None} will be searched if the given \var{realm} has no matching
495user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000496\end{methoddesc}
497
Moshe Zadka8a18e992001-03-01 08:40:42 +0000498
Fred Drake93c86712001-03-02 20:39:34 +0000499\subsection{AbstractBasicAuthHandler Objects
500 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000501
502\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
503 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000504Handle an authentication request by getting a user/password pair, and
505re-trying the request. \var{authreq} should be the name of the header
506where the information about the realm is included in the request,
507\var{host} is the host to authenticate to, \var{req} should be the
508(failed) \class{Request} object, and \var{headers} should be the error
509headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000510\end{methoddesc}
511
Fred Drake93c86712001-03-02 20:39:34 +0000512
513\subsection{HTTPBasicAuthHandler Objects
514 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000515
516\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
517 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000518Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000519\end{methoddesc}
520
Fred Drake93c86712001-03-02 20:39:34 +0000521
522\subsection{ProxyBasicAuthHandler Objects
523 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000524
525\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
526 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000527Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000528\end{methoddesc}
529
Moshe Zadka8a18e992001-03-01 08:40:42 +0000530
Fred Drake93c86712001-03-02 20:39:34 +0000531\subsection{AbstractDigestAuthHandler Objects
532 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000533
Fred Drake93c86712001-03-02 20:39:34 +0000534\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000535 {authreq, host, req, headers}
536\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000537the realm is included in the request, \var{host} should be the host to
538authenticate to, \var{req} should be the (failed) \class{Request}
539object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000540\end{methoddesc}
541
Fred Drake93c86712001-03-02 20:39:34 +0000542
543\subsection{HTTPDigestAuthHandler Objects
544 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000545
546\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
547 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000548Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000549\end{methoddesc}
550
Fred Drake93c86712001-03-02 20:39:34 +0000551
552\subsection{ProxyDigestAuthHandler Objects
553 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000554
555\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
556 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000557Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000558\end{methoddesc}
559
Fred Drake93c86712001-03-02 20:39:34 +0000560
561\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000562
563\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000564Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000565\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000566\end{methoddesc}
567
Fred Drake93c86712001-03-02 20:39:34 +0000568
569\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000570
571\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000572Send an HTTPS request, which can be either GET or POST, depending on
573\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000574\end{methoddesc}
575
Moshe Zadka8a18e992001-03-01 08:40:42 +0000576
Fred Drake93c86712001-03-02 20:39:34 +0000577\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000578
579\begin{methoddesc}[FileHandler]{file_open}{req}
580Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000581the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000582protocol to \code{ftp} otherwise, and retry opening
583it using \member{parent}.
584\end{methoddesc}
585
Fred Drake93c86712001-03-02 20:39:34 +0000586
587\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000588
589\begin{methoddesc}[FTPHandler]{ftp_open}{req}
590Open the FTP file indicated by \var{req}.
591The login is always done with empty username and password.
592\end{methoddesc}
593
Moshe Zadka8a18e992001-03-01 08:40:42 +0000594
Fred Drake93c86712001-03-02 20:39:34 +0000595\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
596
597\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
598the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000599
600\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
601Set timeout of connections to \var{t} seconds.
602\end{methoddesc}
603
604\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
605Set maximum number of cached connections to \var{m}.
606\end{methoddesc}
607
Fred Drake93c86712001-03-02 20:39:34 +0000608
609\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000610
611\begin{methoddesc}[GopherHandler]{gopher_open}{req}
612Open the gopher resource indicated by \var{req}.
613\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000614
615
616\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
617
Fred Drakea9399112001-07-05 21:14:03 +0000618\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000619Raise a \exception{URLError} exception.
620\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000621
622
623\subsection{Examples \label{urllib2-examples}}
624
625This example gets the python.org main page and displays the first 100
626bytes of it:
627
628\begin{verbatim}
629>>> import urllib2
630>>> f = urllib2.urlopen('http://www.python.org/')
631>>> print f.read(100)
632<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
633<?xml-stylesheet href="./css/ht2html
634\end{verbatim}
635
636Here we are sending a data-stream to the stdin of a CGI and reading
637the data it returns to us:
638
639\begin{verbatim}
640>>> import urllib2
641>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
642... data='This data is passed to stdin of the CGI')
643>>> f = urllib2.urlopen(req)
644>>> print f.read()
645Got Data: "This data is passed to stdin of the CGI"
646\end{verbatim}
647
648The code for the sample CGI used in the above example is:
649
650\begin{verbatim}
651#!/usr/bin/env python
652import sys
653data = sys.stdin.read()
654print 'Content-type: text-plain\n\nGot Data: "%s"' %
655data
656\end{verbatim}