blob: 4003e3b40447e5265793453ce21428e404fc3245 [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
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000013authentication, redirections, cookies and more.
Moshe Zadka8a18e992001-03-01 08:40:42 +000014
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}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000019object.
Moshe Zadka8a18e992001-03-01 08:40:42 +000020
21\var{data} should be a string, which specifies additional data to
22send to the server. In HTTP requests, which are the only ones that
23support \var{data}, it should be a buffer in the format of
Fred Drake93c86712001-03-02 20:39:34 +000024\mimetype{application/x-www-form-urlencoded}, for example one returned
25from \function{urllib.urlencode()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000026
27This function returns a file-like object with two additional methods:
28
29\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +000030 \item \method{geturl()} --- return the URL of the resource retrieved
31 \item \method{info()} --- return the meta-information of the page, as
32 a dictionary-like object
Moshe Zadka8a18e992001-03-01 08:40:42 +000033\end{itemize}
34
35Raises \exception{URLError} on errors.
36\end{funcdesc}
37
38\begin{funcdesc}{install_opener}{opener}
Fred Drake399bc8c2001-11-09 03:49:29 +000039Install an \class{OpenerDirector} instance as the default opener.
Moshe Zadka8a18e992001-03-01 08:40:42 +000040The code does not check for a real \class{OpenerDirector}, and any
41class with the appropriate interface will work.
42\end{funcdesc}
43
Fred Drake93c86712001-03-02 20:39:34 +000044\begin{funcdesc}{build_opener}{\optional{handler, \moreargs}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000045Return an \class{OpenerDirector} instance, which chains the
46handlers in the order given. \var{handler}s can be either instances
47of \class{BaseHandler}, or subclasses of \class{BaseHandler} (in
48which case it must be possible to call the constructor without
Fred Drake399bc8c2001-11-09 03:49:29 +000049any parameters). Instances of the following classes will be in
50front of the \var{handler}s, unless the \var{handler}s contain
Moshe Zadka8a18e992001-03-01 08:40:42 +000051them, instances of them or subclasses of them:
Fred Draked9cf8e72003-07-14 21:07:05 +000052\class{ProxyHandler}, \class{UnknownHandler}, \class{HTTPHandler},
53\class{HTTPDefaultErrorHandler}, \class{HTTPRedirectHandler},
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +000054\class{FTPHandler}, \class{FileHandler}, \class{HTTPErrorProcessor}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000055
Fred Drake93c86712001-03-02 20:39:34 +000056If the Python installation has SSL support (\function{socket.ssl()}
57exists), \class{HTTPSHandler} will also be added.
Gustavo Niemeyer9556fba2003-06-07 17:53:08 +000058
Fred Draked9cf8e72003-07-14 21:07:05 +000059Beginning in Python 2.3, a \class{BaseHandler} subclass may also
60change its \member{handler_order} member variable to modify its
61position in the handlers list. Besides \class{ProxyHandler}, which has
62\member{handler_order} of \code{100}, all handlers currently have it
63set to \code{500}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000064\end{funcdesc}
65
Fred Drake93c86712001-03-02 20:39:34 +000066
67The following exceptions are raised as appropriate:
68
Moshe Zadka8a18e992001-03-01 08:40:42 +000069\begin{excdesc}{URLError}
Fred Drake399bc8c2001-11-09 03:49:29 +000070The handlers raise this exception (or derived exceptions) when they
71run into a problem. It is a subclass of \exception{IOError}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000072\end{excdesc}
73
74\begin{excdesc}{HTTPError}
75A subclass of \exception{URLError}, it can also function as a
Fred Drake93c86712001-03-02 20:39:34 +000076non-exceptional file-like return value (the same thing that
77\function{urlopen()} returns). This is useful when handling exotic
78HTTP errors, such as requests for authentication.
Moshe Zadka8a18e992001-03-01 08:40:42 +000079\end{excdesc}
80
81\begin{excdesc}{GopherError}
82A subclass of \exception{URLError}, this is the error raised by the
83Gopher handler.
84\end{excdesc}
85
Fred Drake93c86712001-03-02 20:39:34 +000086
87The following classes are provided:
88
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000089\begin{classdesc}{Request}{url\optional{, data}\optional{, headers}
90 \optional{, origin_req_host}\optional{, unverifiable}}
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.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000097
98The final two arguments are only of interest for correct handling of
99third-party HTTP cookies:
100
101\var{origin_req_host} should be the request-host of the origin
102transaction, as defined by \rfc{2965}. It defaults to
103\code{cookielib.request_host(self)}. This is the host name or IP
104address of the original request that was initiated by the user. For
105example, if the request is for an image in an HTML document, this
106should be the request-host of the request for the page containing the
107image.
108
109\var{unverifiable} should indicate whether the request is
110unverifiable, as defined by RFC 2965. It defaults to False. An
111unverifiable request is one whose URL the user did not have the option
112to approve. For example, if the request is for an image in an HTML
113document, and the user had no option to approve the automatic fetching
114of the image, this should be true.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000115\end{classdesc}
116
Fred Drake93c86712001-03-02 20:39:34 +0000117\begin{classdesc}{OpenerDirector}{}
118The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
119chained together. It manages the chaining of handlers, and recovery
120from errors.
121\end{classdesc}
122
123\begin{classdesc}{BaseHandler}{}
124This is the base class for all registered handlers --- and handles only
125the simple mechanics of registration.
126\end{classdesc}
127
128\begin{classdesc}{HTTPDefaultErrorHandler}{}
129A class which defines a default handler for HTTP error responses; all
130responses are turned into \exception{HTTPError} exceptions.
131\end{classdesc}
132
133\begin{classdesc}{HTTPRedirectHandler}{}
134A class to handle redirections.
135\end{classdesc}
136
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000137\begin{classdesc}{HTTPCookieProcessor}{\optional{cookiejar}}
138A class to handle HTTP Cookies.
139\end{classdesc}
140
Fred Drake93c86712001-03-02 20:39:34 +0000141\begin{classdesc}{ProxyHandler}{\optional{proxies}}
142Cause requests to go through a proxy.
143If \var{proxies} is given, it must be a dictionary mapping
144protocol names to URLs of proxies.
145The default is to read the list of proxies from the environment
Fred Drake47852462001-05-11 15:46:45 +0000146variables \var{protocol}_proxy.
Fred Drake93c86712001-03-02 20:39:34 +0000147\end{classdesc}
148
149\begin{classdesc}{HTTPPasswordMgr}{}
150Keep a database of
151\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
152mappings.
153\end{classdesc}
154
155\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
156Keep a database of
157\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
158A realm of \code{None} is considered a catch-all realm, which is searched
159if no other realm fits.
160\end{classdesc}
161
162\begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
163This is a mixin class that helps with HTTP authentication, both
164to 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}{HTTPBasicAuthHandler}{\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}{ProxyBasicAuthHandler}{\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}{AbstractDigestAuthHandler}{\optional{password_mgr}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000185This is a mixin class that helps with HTTP authentication, both
Fred Drake93c86712001-03-02 20:39:34 +0000186to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000187\var{password_mgr}, if given, should be something that is compatible
188with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
189for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000190\end{classdesc}
191
192\begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
193Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000194\var{password_mgr}, if given, should be something that is compatible
195with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
196for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000197\end{classdesc}
198
199\begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
200Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000201\var{password_mgr}, if given, should be something that is compatible
202with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
203for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000204\end{classdesc}
205
206\begin{classdesc}{HTTPHandler}{}
207A class to handle opening of HTTP URLs.
208\end{classdesc}
209
210\begin{classdesc}{HTTPSHandler}{}
211A class to handle opening of HTTPS URLs.
212\end{classdesc}
213
214\begin{classdesc}{FileHandler}{}
215Open local files.
216\end{classdesc}
217
218\begin{classdesc}{FTPHandler}{}
219Open FTP URLs.
220\end{classdesc}
221
222\begin{classdesc}{CacheFTPHandler}{}
223Open FTP URLs, keeping a cache of open FTP connections to minimize
224delays.
225\end{classdesc}
226
227\begin{classdesc}{GopherHandler}{}
228Open gopher URLs.
229\end{classdesc}
230
231\begin{classdesc}{UnknownHandler}{}
232A catch-all class to handle unknown URLs.
233\end{classdesc}
234
235
236\subsection{Request Objects \label{request-objects}}
237
Moshe Zadka8a18e992001-03-01 08:40:42 +0000238The following methods describe all of \class{Request}'s public interface,
239and so all must be overridden in subclasses.
240
241\begin{methoddesc}[Request]{add_data}{data}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000242Set the \class{Request} data to \var{data}. This is ignored by all
243handlers except HTTP handlers --- and there it should be a byte
244string, and will change the request to be \code{POST} rather than
245\code{GET}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000246\end{methoddesc}
247
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000248\begin{methoddesc}[Request]{get_method}{}
249Return a string indicating the HTTP request method. This is only
250meaningful for HTTP requests, and currently always takes one of the
251values ("GET", "POST").
252\end{methoddesc}
253
Fred Drake399bc8c2001-11-09 03:49:29 +0000254\begin{methoddesc}[Request]{has_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000255Return whether the instance has a non-\code{None} data.
256\end{methoddesc}
257
Fred Drake399bc8c2001-11-09 03:49:29 +0000258\begin{methoddesc}[Request]{get_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000259Return the instance's data.
260\end{methoddesc}
261
262\begin{methoddesc}[Request]{add_header}{key, val}
Fred Drake93c86712001-03-02 20:39:34 +0000263Add another header to the request. Headers are currently ignored by
264all handlers except HTTP handlers, where they are added to the list
Fred Drake399bc8c2001-11-09 03:49:29 +0000265of headers sent to the server. Note that there cannot be more than
Fred Drake93c86712001-03-02 20:39:34 +0000266one header with the same name, and later calls will overwrite
267previous calls in case the \var{key} collides. Currently, this is
268no loss of HTTP functionality, since all headers which have meaning
Fred Drake399bc8c2001-11-09 03:49:29 +0000269when used more than once have a (header-specific) way of gaining the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000270same functionality using only one header.
271\end{methoddesc}
272
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000273\begin{methoddesc}[Request]{add_unredirected_header}{key, header}
274Add a header that will not be added to a redirected request.
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000275\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000276\end{methoddesc}
277
278\begin{methoddesc}[Request]{has_header}{header}
279Return whether the instance has the named header (checks both regular
280and unredirected).
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000281\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000282\end{methoddesc}
283
Moshe Zadka8a18e992001-03-01 08:40:42 +0000284\begin{methoddesc}[Request]{get_full_url}{}
285Return the URL given in the constructor.
286\end{methoddesc}
287
288\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000289Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000290\end{methoddesc}
291
292\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000293Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000294\end{methoddesc}
295
296\begin{methoddesc}[Request]{get_selector}{}
297Return the selector --- the part of the URL that is sent to
298the server.
299\end{methoddesc}
300
301\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000302Prepare the request by connecting to a proxy server. The \var{host}
303and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000304selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000305\end{methoddesc}
306
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000307\begin{methoddesc}[Request]{get_origin_req_host}{}
308Return the request-host of the origin transaction, as defined by
309\rfc{2965}. See the documentation for the \class{Request}
310constructor.
311\end{methoddesc}
312
313\begin{methoddesc}[Request]{is_unverifiable}{}
314Return whether the request is unverifiable, as defined by RFC 2965.
315See the documentation for the \class{Request} constructor.
316\end{methoddesc}
317
Fred Drake93c86712001-03-02 20:39:34 +0000318
319\subsection{OpenerDirector Objects \label{opener-director-objects}}
320
321\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000322
323\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000324\var{handler} should be an instance of \class{BaseHandler}. The
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000325following methods are searched, and added to the possible chains (note
326that HTTP errors are a special case).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000327
328\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000329 \item \method{\var{protocol}_open()} ---
330 signal that the handler knows how to open \var{protocol} URLs.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000331 \item \method{http_error_\var{type}()} ---
332 signal that the handler knows how to handle HTTP errors with HTTP
333 error code \var{type}.
334 \item \method{\var{protocol}_error()} ---
335 signal that the handler knows how to handle errors from
336 (non-\code{http}) \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000337 \item \method{\var{protocol}_request()} ---
338 signal that the handler knows how to pre-process \var{protocol}
339 requests.
340 \item \method{\var{protocol}_response()} ---
341 signal that the handler knows how to post-process \var{protocol}
342 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000343\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000344\end{methoddesc}
345
Moshe Zadka8a18e992001-03-01 08:40:42 +0000346\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000347Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000348optionally passing the given \var{data}.
349Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000350of \function{urlopen()} (which simply calls the \method{open()} method
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000351on the currently installed global \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000352\end{methoddesc}
353
Fred Drake93c86712001-03-02 20:39:34 +0000354\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
355 arg\optional{, \moreargs}}}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000356Handle an error of the given protocol. This will call the registered
Fred Drake399bc8c2001-11-09 03:49:29 +0000357error handlers for the given protocol with the given arguments (which
358are protocol specific). The HTTP protocol is a special case which
359uses the HTTP response code to determine the specific error handler;
360refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000361
362Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000363of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000364\end{methoddesc}
365
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000366OpenerDirector objects open URLs in three stages:
367
Andrew M. Kuchling300ce192004-07-10 18:28:33 +0000368The order in which these methods are called within each stage is
369determined by sorting the handler instances.
370
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000371\begin{enumerate}
372 \item Every handler with a method named like
373 \method{\var{protocol}_request()} has that method called to
374 pre-process the request.
375
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000376 \item Handlers with a method named like
377 \method{\var{protocol}_open()} are called to handle the request.
378 This stage ends when a handler either returns a
379 non-\constant{None} value (ie. a response), or raises an exception
380 (usually URLError). Exceptions are allowed to propagate.
381
382 In fact, the above algorithm is first tried for methods named
383 \method{default_open}. If all such methods return
384 \constant{None}, the algorithm is repeated for methods named like
385 \method{\var{protocol}_open()}. If all such methods return
386 \constant{None}, the algorithm is repeated for methods named
387 \method{unknown_open()}.
388
389 Note that the implementation of these methods may involve calls of
390 the parent \class{OpenerDirector} instance's \method{.open()} and
391 \method{.error()} methods.
392
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000393 \item Every handler with a method named like
394 \method{\var{protocol}_response()} has that method called to
395 post-process the response.
396
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000397\end{enumerate}
Fred Drake93c86712001-03-02 20:39:34 +0000398
399\subsection{BaseHandler Objects \label{base-handler-objects}}
400
401\class{BaseHandler} objects provide a couple of methods that are
402directly useful, and others that are meant to be used by derived
403classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000404
405\begin{methoddesc}[BaseHandler]{add_parent}{director}
406Add a director as parent.
407\end{methoddesc}
408
409\begin{methoddesc}[BaseHandler]{close}{}
410Remove any parents.
411\end{methoddesc}
412
Fred Drake399bc8c2001-11-09 03:49:29 +0000413The following members and methods should only be used by classes
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000414derived from \class{BaseHandler}. \note{The convention has been
415adopted that subclasses defining \method{\var{protocol}_request()} or
416\method{\var{protocol}_response()} methods are named
417\class{*Processor}; all others are named \class{*Handler}.}
418
Moshe Zadka8a18e992001-03-01 08:40:42 +0000419
420\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000421A valid \class{OpenerDirector}, which can be used to open using a
422different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000423\end{memberdesc}
424
425\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000426This method is \emph{not} defined in \class{BaseHandler}, but
427subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000428
Fred Drake399bc8c2001-11-09 03:49:29 +0000429This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000430\class{OpenerDirector}. It should return a file-like object as
431described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000432\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000433\exception{URLError}, unless a truly exceptional thing happens (for
434example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000435\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000436
437This method will be called before any protocol-specific open method.
438\end{methoddesc}
439
Fred Drake47852462001-05-11 15:46:45 +0000440\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000441This method is \emph{not} defined in \class{BaseHandler}, but
442subclasses should define it if they want to handle URLs with the given
443protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000444
Fred Drake399bc8c2001-11-09 03:49:29 +0000445This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000446\class{OpenerDirector}. Return values should be the same as for
447\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000448\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000449
450\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000451This method is \var{not} defined in \class{BaseHandler}, but
452subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000453specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000454
Fred Drake399bc8c2001-11-09 03:49:29 +0000455This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000456\class{OpenerDirector}. Return values should be the same as for
457\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000458\end{methoddesc}
459
460\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000461This method is \emph{not} defined in \class{BaseHandler}, but
462subclasses should override it if they intend to provide a catch-all
463for otherwise unhandled HTTP errors. It will be called automatically
464by the \class{OpenerDirector} getting the error, and should not
465normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000466
Fred Drake93c86712001-03-02 20:39:34 +0000467\var{req} will be a \class{Request} object, \var{fp} will be a
468file-like object with the HTTP error body, \var{code} will be the
469three-digit code of the error, \var{msg} will be the user-visible
470explanation of the code and \var{hdrs} will be a mapping object with
471the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000472
473Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000474of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000475\end{methoddesc}
476
Fred Drake93c86712001-03-02 20:39:34 +0000477\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
478\var{nnn} should be a three-digit HTTP error code. This method is
479also not defined in \class{BaseHandler}, but will be called, if it
480exists, on an instance of a subclass, when an HTTP error with code
481\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000482
Fred Drake93c86712001-03-02 20:39:34 +0000483Subclasses should override this method to handle specific HTTP
484errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000485
Fred Drake93c86712001-03-02 20:39:34 +0000486Arguments, return values and exceptions raised should be the same as
487for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000488\end{methoddesc}
489
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000490\begin{methoddescni}[BaseHandler]{\var{protocol}_request}{req}
491This method is \emph{not} defined in \class{BaseHandler}, but
492subclasses should define it if they want to pre-process requests of
493the given protocol.
494
495This method, if defined, will be called by the parent
496\class{OpenerDirector}. \var{req} will be a \class{Request} object.
497The return value should be a \class{Request} object.
498\end{methoddescni}
499
500\begin{methoddescni}[BaseHandler]{\var{protocol}_response}{req, response}
501This method is \emph{not} defined in \class{BaseHandler}, but
502subclasses should define it if they want to post-process responses of
503the given protocol.
504
505This method, if defined, will be called by the parent
506\class{OpenerDirector}. \var{req} will be a \class{Request} object.
507\var{response} will be an object implementing the same interface as
508the return value of \function{urlopen()}. The return value should
509implement the same interface as the return value of
510\function{urlopen()}.
511\end{methoddescni}
512
Fred Drake93c86712001-03-02 20:39:34 +0000513\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000514
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000515\note{Some HTTP redirections require action from this module's client
516 code. If this is the case, \exception{HTTPError} is raised. See
517 \rfc{2616} for details of the precise meanings of the various
518 redirection codes.}
519
520\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
521 fp, code, msg, hdrs}
522Return a \class{Request} or \code{None} in response to a redirect.
523This is called by the default implementations of the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000524\method{http_error_30*()} methods when a redirection is received from
525the server. If a redirection should take place, return a new
Fred Draked9cf8e72003-07-14 21:07:05 +0000526\class{Request} to allow \method{http_error_30*()} to perform the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000527redirect. Otherwise, raise \exception{HTTPError} if no other handler
528should try to handle this URL, or return \code{None} if you can't but
529another handler might.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000530
Fred Draked9cf8e72003-07-14 21:07:05 +0000531\begin{notice}
532 The default implementation of this method does not strictly
533 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000534 requests must not be automatically redirected without confirmation by
535 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000536 these responses, changing the POST to a \code{GET}, and the default
537 implementation reproduces this behavior.
538\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000539\end{methoddesc}
540
Moshe Zadka8a18e992001-03-01 08:40:42 +0000541
Fred Drake93c86712001-03-02 20:39:34 +0000542\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
543 fp, code, msg, hdrs}
544Redirect to the \code{Location:} URL. This method is called by
545the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000546`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000547\end{methoddesc}
548
Fred Drake93c86712001-03-02 20:39:34 +0000549\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
550 fp, code, msg, hdrs}
551The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000552`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000553\end{methoddesc}
554
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000555\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
556 fp, code, msg, hdrs}
557The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000558`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000559\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000560
Martin v. Löwis162f0812003-07-12 07:33:32 +0000561\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
562 fp, code, msg, hdrs}
563The same as \method{http_error_301()}, but called for the
564`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000565\end{methoddesc}
566
Martin v. Löwis162f0812003-07-12 07:33:32 +0000567
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000568\subsection{HTTPCookieProcessor Objects \label{http-cookie-processor}}
569
570\class{HTTPCookieProcessor} instances have one attribute:
571
572\begin{memberdesc}{cookiejar}
573The \class{cookielib.CookieJar} in which cookies are stored.
574\end{memberdesc}
575
576
Fred Drake93c86712001-03-02 20:39:34 +0000577\subsection{ProxyHandler Objects \label{proxy-handler}}
578
Fred Drake47852462001-05-11 15:46:45 +0000579\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000580The \class{ProxyHandler} will have a method
581\method{\var{protocol}_open()} for every \var{protocol} which has a
582proxy in the \var{proxies} dictionary given in the constructor. The
583method will modify requests to go through the proxy, by calling
584\code{request.set_proxy()}, and call the next handler in the chain to
585actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000586\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000587
588
589\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
590
591These methods are available on \class{HTTPPasswordMgr} and
592\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000593
594\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
Fred Drakebb066cf2004-05-12 03:07:27 +0000595\var{uri} can be either a single URI, or a sequence of URIs. \var{realm},
Moshe Zadka8a18e992001-03-01 08:40:42 +0000596\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000597\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000598when authentication for \var{realm} and a super-URI of any of the
599given URIs is given.
600\end{methoddesc}
601
602\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000603Get user/password for given realm and URI, if any. This method will
604return \code{(None, None)} if there is no matching user/password.
605
606For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
607\code{None} will be searched if the given \var{realm} has no matching
608user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000609\end{methoddesc}
610
Moshe Zadka8a18e992001-03-01 08:40:42 +0000611
Fred Drake93c86712001-03-02 20:39:34 +0000612\subsection{AbstractBasicAuthHandler Objects
613 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000614
615\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
616 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000617Handle an authentication request by getting a user/password pair, and
618re-trying the request. \var{authreq} should be the name of the header
619where the information about the realm is included in the request,
620\var{host} is the host to authenticate to, \var{req} should be the
621(failed) \class{Request} object, and \var{headers} should be the error
622headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000623\end{methoddesc}
624
Fred Drake93c86712001-03-02 20:39:34 +0000625
626\subsection{HTTPBasicAuthHandler Objects
627 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000628
629\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
630 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000631Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000632\end{methoddesc}
633
Fred Drake93c86712001-03-02 20:39:34 +0000634
635\subsection{ProxyBasicAuthHandler Objects
636 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000637
638\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
639 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000640Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000641\end{methoddesc}
642
Moshe Zadka8a18e992001-03-01 08:40:42 +0000643
Fred Drake93c86712001-03-02 20:39:34 +0000644\subsection{AbstractDigestAuthHandler Objects
645 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000646
Fred Drake93c86712001-03-02 20:39:34 +0000647\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000648 {authreq, host, req, headers}
649\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000650the realm is included in the request, \var{host} should be the host to
651authenticate to, \var{req} should be the (failed) \class{Request}
652object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000653\end{methoddesc}
654
Fred Drake93c86712001-03-02 20:39:34 +0000655
656\subsection{HTTPDigestAuthHandler Objects
657 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000658
659\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
660 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000661Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000662\end{methoddesc}
663
Fred Drake93c86712001-03-02 20:39:34 +0000664
665\subsection{ProxyDigestAuthHandler Objects
666 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000667
668\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
669 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000670Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000671\end{methoddesc}
672
Fred Drake93c86712001-03-02 20:39:34 +0000673
674\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000675
676\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000677Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000678\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000679\end{methoddesc}
680
Fred Drake93c86712001-03-02 20:39:34 +0000681
682\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000683
684\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000685Send an HTTPS request, which can be either GET or POST, depending on
686\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000687\end{methoddesc}
688
Moshe Zadka8a18e992001-03-01 08:40:42 +0000689
Fred Drake93c86712001-03-02 20:39:34 +0000690\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000691
692\begin{methoddesc}[FileHandler]{file_open}{req}
693Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000694the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000695protocol to \code{ftp} otherwise, and retry opening
696it using \member{parent}.
697\end{methoddesc}
698
Fred Drake93c86712001-03-02 20:39:34 +0000699
700\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000701
702\begin{methoddesc}[FTPHandler]{ftp_open}{req}
703Open the FTP file indicated by \var{req}.
704The login is always done with empty username and password.
705\end{methoddesc}
706
Moshe Zadka8a18e992001-03-01 08:40:42 +0000707
Fred Drake93c86712001-03-02 20:39:34 +0000708\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
709
710\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
711the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000712
713\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
714Set timeout of connections to \var{t} seconds.
715\end{methoddesc}
716
717\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
718Set maximum number of cached connections to \var{m}.
719\end{methoddesc}
720
Fred Drake93c86712001-03-02 20:39:34 +0000721
722\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000723
724\begin{methoddesc}[GopherHandler]{gopher_open}{req}
725Open the gopher resource indicated by \var{req}.
726\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000727
728
729\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
730
Fred Drakea9399112001-07-05 21:14:03 +0000731\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000732Raise a \exception{URLError} exception.
733\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000734
735
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000736\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
737
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000738\versionadded{2.4}
739
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000740\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
741Process HTTP error responses.
742
743For 200 error codes, the response object is returned immediately.
744
745For non-200 error codes, this simply passes the job on to the
746\method{\var{protocol}_error_\var{code}()} handler methods, via
747\method{OpenerDirector.error()}. Eventually,
748\class{urllib2.HTTPDefaultErrorHandler} will raise an
749\exception{HTTPError} if no other handler handles the error.
750\end{methoddesc}
751
752
Fred Drake53e5b712003-04-25 15:27:33 +0000753\subsection{Examples \label{urllib2-examples}}
754
755This example gets the python.org main page and displays the first 100
756bytes of it:
757
758\begin{verbatim}
759>>> import urllib2
760>>> f = urllib2.urlopen('http://www.python.org/')
761>>> print f.read(100)
762<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
763<?xml-stylesheet href="./css/ht2html
764\end{verbatim}
765
766Here we are sending a data-stream to the stdin of a CGI and reading
767the data it returns to us:
768
769\begin{verbatim}
770>>> import urllib2
771>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
772... data='This data is passed to stdin of the CGI')
773>>> f = urllib2.urlopen(req)
774>>> print f.read()
775Got Data: "This data is passed to stdin of the CGI"
776\end{verbatim}
777
778The code for the sample CGI used in the above example is:
779
780\begin{verbatim}
781#!/usr/bin/env python
782import sys
783data = sys.stdin.read()
Raymond Hettinger5de33782004-02-08 20:25:01 +0000784print 'Content-type: text-plain\n\nGot Data: "%s"' % data
Fred Drake53e5b712003-04-25 15:27:33 +0000785\end{verbatim}