blob: 842b2a31ee66f203099c85d86dcc3582af9eb10e [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.
Kurt B. Kaiser8932b412004-07-11 02:13:17 +000036
37Note that \code{None} may be returned if no handler handles the
38request (though the default installed global \class{OpenerDirector}
39uses \class{UnknownHandler} to ensure this never happens).
Moshe Zadka8a18e992001-03-01 08:40:42 +000040\end{funcdesc}
41
42\begin{funcdesc}{install_opener}{opener}
Kurt B. Kaiser8932b412004-07-11 02:13:17 +000043Install an \class{OpenerDirector} instance as the default global
44opener. Installing an opener is only necessary if you want urlopen to
45use that opener; otherwise, simply call \method{OpenerDirector.open()}
46instead of \function{urlopen()}. The code does not check for a real
47\class{OpenerDirector}, and any class with the appropriate interface
48will work.
Moshe Zadka8a18e992001-03-01 08:40:42 +000049\end{funcdesc}
50
Fred Drake93c86712001-03-02 20:39:34 +000051\begin{funcdesc}{build_opener}{\optional{handler, \moreargs}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000052Return an \class{OpenerDirector} instance, which chains the
53handlers in the order given. \var{handler}s can be either instances
54of \class{BaseHandler}, or subclasses of \class{BaseHandler} (in
55which case it must be possible to call the constructor without
Fred Drake399bc8c2001-11-09 03:49:29 +000056any parameters). Instances of the following classes will be in
57front of the \var{handler}s, unless the \var{handler}s contain
Moshe Zadka8a18e992001-03-01 08:40:42 +000058them, instances of them or subclasses of them:
Fred Draked9cf8e72003-07-14 21:07:05 +000059\class{ProxyHandler}, \class{UnknownHandler}, \class{HTTPHandler},
60\class{HTTPDefaultErrorHandler}, \class{HTTPRedirectHandler},
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +000061\class{FTPHandler}, \class{FileHandler}, \class{HTTPErrorProcessor}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000062
Fred Drake93c86712001-03-02 20:39:34 +000063If the Python installation has SSL support (\function{socket.ssl()}
64exists), \class{HTTPSHandler} will also be added.
Gustavo Niemeyer9556fba2003-06-07 17:53:08 +000065
Fred Draked9cf8e72003-07-14 21:07:05 +000066Beginning in Python 2.3, a \class{BaseHandler} subclass may also
67change its \member{handler_order} member variable to modify its
68position in the handlers list. Besides \class{ProxyHandler}, which has
69\member{handler_order} of \code{100}, all handlers currently have it
70set to \code{500}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000071\end{funcdesc}
72
Fred Drake93c86712001-03-02 20:39:34 +000073
Georg Brandl5c5fe2f2005-07-14 06:40:47 +000074The following attribute is defined:
75
76\begin{datadesc}{httpresponses}
77A mapping between HTTP status codes and the W3C names.
78
79Example: \code{urllib2.httpresponses[404]} is \code{'Not Found'}.
80\versionadded{2.5}
81\end{datadesc}
82
Fred Drake93c86712001-03-02 20:39:34 +000083The following exceptions are raised as appropriate:
84
Moshe Zadka8a18e992001-03-01 08:40:42 +000085\begin{excdesc}{URLError}
Fred Drake399bc8c2001-11-09 03:49:29 +000086The handlers raise this exception (or derived exceptions) when they
87run into a problem. It is a subclass of \exception{IOError}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000088\end{excdesc}
89
90\begin{excdesc}{HTTPError}
91A subclass of \exception{URLError}, it can also function as a
Fred Drake93c86712001-03-02 20:39:34 +000092non-exceptional file-like return value (the same thing that
93\function{urlopen()} returns). This is useful when handling exotic
94HTTP errors, such as requests for authentication.
Moshe Zadka8a18e992001-03-01 08:40:42 +000095\end{excdesc}
96
97\begin{excdesc}{GopherError}
98A subclass of \exception{URLError}, this is the error raised by the
99Gopher handler.
100\end{excdesc}
101
Fred Drake93c86712001-03-02 20:39:34 +0000102
103The following classes are provided:
104
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000105\begin{classdesc}{Request}{url\optional{, data}\optional{, headers}
106 \optional{, origin_req_host}\optional{, unverifiable}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000107This class is an abstraction of a URL request.
108
Fred Drake399bc8c2001-11-09 03:49:29 +0000109\var{url} should be a string which is a valid URL. For a description
Fred Drake93c86712001-03-02 20:39:34 +0000110of \var{data} see the \method{add_data()} description.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000111\var{headers} should be a dictionary, and will be treated as if
Fred Drake93c86712001-03-02 20:39:34 +0000112\method{add_header()} was called with each key and value as arguments.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000113
114The final two arguments are only of interest for correct handling of
115third-party HTTP cookies:
116
117\var{origin_req_host} should be the request-host of the origin
118transaction, as defined by \rfc{2965}. It defaults to
119\code{cookielib.request_host(self)}. This is the host name or IP
120address of the original request that was initiated by the user. For
121example, if the request is for an image in an HTML document, this
122should be the request-host of the request for the page containing the
123image.
124
125\var{unverifiable} should indicate whether the request is
126unverifiable, as defined by RFC 2965. It defaults to False. An
127unverifiable request is one whose URL the user did not have the option
128to approve. For example, if the request is for an image in an HTML
129document, and the user had no option to approve the automatic fetching
130of the image, this should be true.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000131\end{classdesc}
132
Fred Drake93c86712001-03-02 20:39:34 +0000133\begin{classdesc}{OpenerDirector}{}
134The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
135chained together. It manages the chaining of handlers, and recovery
136from errors.
137\end{classdesc}
138
139\begin{classdesc}{BaseHandler}{}
140This is the base class for all registered handlers --- and handles only
141the simple mechanics of registration.
142\end{classdesc}
143
144\begin{classdesc}{HTTPDefaultErrorHandler}{}
145A class which defines a default handler for HTTP error responses; all
146responses are turned into \exception{HTTPError} exceptions.
147\end{classdesc}
148
149\begin{classdesc}{HTTPRedirectHandler}{}
150A class to handle redirections.
151\end{classdesc}
152
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000153\begin{classdesc}{HTTPCookieProcessor}{\optional{cookiejar}}
154A class to handle HTTP Cookies.
155\end{classdesc}
156
Fred Drake93c86712001-03-02 20:39:34 +0000157\begin{classdesc}{ProxyHandler}{\optional{proxies}}
158Cause requests to go through a proxy.
159If \var{proxies} is given, it must be a dictionary mapping
160protocol names to URLs of proxies.
161The default is to read the list of proxies from the environment
Martin v. Löwisbe837372004-08-25 11:24:42 +0000162variables \envvar{<protocol>_proxy}.
Fred Drake93c86712001-03-02 20:39:34 +0000163\end{classdesc}
164
165\begin{classdesc}{HTTPPasswordMgr}{}
166Keep a database of
167\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
168mappings.
169\end{classdesc}
170
171\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
172Keep a database of
173\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
174A realm of \code{None} is considered a catch-all realm, which is searched
175if no other realm fits.
176\end{classdesc}
177
178\begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
179This is a mixin class that helps with HTTP authentication, both
180to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000181\var{password_mgr}, if given, should be something that is compatible
182with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
183for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000184\end{classdesc}
185
186\begin{classdesc}{HTTPBasicAuthHandler}{\optional{password_mgr}}
187Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000188\var{password_mgr}, if given, should be something that is compatible
189with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
190for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000191\end{classdesc}
192
193\begin{classdesc}{ProxyBasicAuthHandler}{\optional{password_mgr}}
194Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000195\var{password_mgr}, if given, should be something that is compatible
196with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
197for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000198\end{classdesc}
199
200\begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000201This is a mixin class that helps with HTTP authentication, both
Fred Drake93c86712001-03-02 20:39:34 +0000202to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000203\var{password_mgr}, if given, should be something that is compatible
204with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
205for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000206\end{classdesc}
207
208\begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
209Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000210\var{password_mgr}, if given, should be something that is compatible
211with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
212for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000213\end{classdesc}
214
215\begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
216Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000217\var{password_mgr}, if given, should be something that is compatible
218with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
219for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000220\end{classdesc}
221
222\begin{classdesc}{HTTPHandler}{}
223A class to handle opening of HTTP URLs.
224\end{classdesc}
225
226\begin{classdesc}{HTTPSHandler}{}
227A class to handle opening of HTTPS URLs.
228\end{classdesc}
229
230\begin{classdesc}{FileHandler}{}
231Open local files.
232\end{classdesc}
233
234\begin{classdesc}{FTPHandler}{}
235Open FTP URLs.
236\end{classdesc}
237
238\begin{classdesc}{CacheFTPHandler}{}
239Open FTP URLs, keeping a cache of open FTP connections to minimize
240delays.
241\end{classdesc}
242
243\begin{classdesc}{GopherHandler}{}
244Open gopher URLs.
245\end{classdesc}
246
247\begin{classdesc}{UnknownHandler}{}
248A catch-all class to handle unknown URLs.
249\end{classdesc}
250
251
252\subsection{Request Objects \label{request-objects}}
253
Moshe Zadka8a18e992001-03-01 08:40:42 +0000254The following methods describe all of \class{Request}'s public interface,
255and so all must be overridden in subclasses.
256
257\begin{methoddesc}[Request]{add_data}{data}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000258Set the \class{Request} data to \var{data}. This is ignored by all
259handlers except HTTP handlers --- and there it should be a byte
260string, and will change the request to be \code{POST} rather than
261\code{GET}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000262\end{methoddesc}
263
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000264\begin{methoddesc}[Request]{get_method}{}
265Return a string indicating the HTTP request method. This is only
Fred Drakecc97ebf2005-04-13 01:08:23 +0000266meaningful for HTTP requests, and currently always returns
267\code{'GET'} or \code{'POST'}.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000268\end{methoddesc}
269
Fred Drake399bc8c2001-11-09 03:49:29 +0000270\begin{methoddesc}[Request]{has_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000271Return whether the instance has a non-\code{None} data.
272\end{methoddesc}
273
Fred Drake399bc8c2001-11-09 03:49:29 +0000274\begin{methoddesc}[Request]{get_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000275Return the instance's data.
276\end{methoddesc}
277
278\begin{methoddesc}[Request]{add_header}{key, val}
Fred Drake93c86712001-03-02 20:39:34 +0000279Add another header to the request. Headers are currently ignored by
280all handlers except HTTP handlers, where they are added to the list
Fred Drake399bc8c2001-11-09 03:49:29 +0000281of headers sent to the server. Note that there cannot be more than
Fred Drake93c86712001-03-02 20:39:34 +0000282one header with the same name, and later calls will overwrite
283previous calls in case the \var{key} collides. Currently, this is
284no loss of HTTP functionality, since all headers which have meaning
Fred Drake399bc8c2001-11-09 03:49:29 +0000285when used more than once have a (header-specific) way of gaining the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000286same functionality using only one header.
287\end{methoddesc}
288
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000289\begin{methoddesc}[Request]{add_unredirected_header}{key, header}
290Add a header that will not be added to a redirected request.
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000291\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000292\end{methoddesc}
293
294\begin{methoddesc}[Request]{has_header}{header}
295Return whether the instance has the named header (checks both regular
296and unredirected).
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000297\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000298\end{methoddesc}
299
Moshe Zadka8a18e992001-03-01 08:40:42 +0000300\begin{methoddesc}[Request]{get_full_url}{}
301Return the URL given in the constructor.
302\end{methoddesc}
303
304\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000305Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000306\end{methoddesc}
307
308\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000309Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000310\end{methoddesc}
311
312\begin{methoddesc}[Request]{get_selector}{}
313Return the selector --- the part of the URL that is sent to
314the server.
315\end{methoddesc}
316
317\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000318Prepare the request by connecting to a proxy server. The \var{host}
319and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000320selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000321\end{methoddesc}
322
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000323\begin{methoddesc}[Request]{get_origin_req_host}{}
324Return the request-host of the origin transaction, as defined by
325\rfc{2965}. See the documentation for the \class{Request}
326constructor.
327\end{methoddesc}
328
329\begin{methoddesc}[Request]{is_unverifiable}{}
330Return whether the request is unverifiable, as defined by RFC 2965.
331See the documentation for the \class{Request} constructor.
332\end{methoddesc}
333
Fred Drake93c86712001-03-02 20:39:34 +0000334
335\subsection{OpenerDirector Objects \label{opener-director-objects}}
336
337\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000338
339\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000340\var{handler} should be an instance of \class{BaseHandler}. The
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000341following methods are searched, and added to the possible chains (note
342that HTTP errors are a special case).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000343
344\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000345 \item \method{\var{protocol}_open()} ---
346 signal that the handler knows how to open \var{protocol} URLs.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000347 \item \method{http_error_\var{type}()} ---
348 signal that the handler knows how to handle HTTP errors with HTTP
349 error code \var{type}.
350 \item \method{\var{protocol}_error()} ---
351 signal that the handler knows how to handle errors from
352 (non-\code{http}) \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000353 \item \method{\var{protocol}_request()} ---
354 signal that the handler knows how to pre-process \var{protocol}
355 requests.
356 \item \method{\var{protocol}_response()} ---
357 signal that the handler knows how to post-process \var{protocol}
358 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000359\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000360\end{methoddesc}
361
Moshe Zadka8a18e992001-03-01 08:40:42 +0000362\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000363Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000364optionally passing the given \var{data}.
365Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000366of \function{urlopen()} (which simply calls the \method{open()} method
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000367on the currently installed global \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000368\end{methoddesc}
369
Fred Drake93c86712001-03-02 20:39:34 +0000370\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
371 arg\optional{, \moreargs}}}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000372Handle an error of the given protocol. This will call the registered
Fred Drake399bc8c2001-11-09 03:49:29 +0000373error handlers for the given protocol with the given arguments (which
374are protocol specific). The HTTP protocol is a special case which
375uses the HTTP response code to determine the specific error handler;
376refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000377
378Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000379of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000380\end{methoddesc}
381
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000382OpenerDirector objects open URLs in three stages:
383
Andrew M. Kuchling300ce192004-07-10 18:28:33 +0000384The order in which these methods are called within each stage is
385determined by sorting the handler instances.
386
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000387\begin{enumerate}
388 \item Every handler with a method named like
389 \method{\var{protocol}_request()} has that method called to
390 pre-process the request.
391
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000392 \item Handlers with a method named like
393 \method{\var{protocol}_open()} are called to handle the request.
394 This stage ends when a handler either returns a
395 non-\constant{None} value (ie. a response), or raises an exception
396 (usually URLError). Exceptions are allowed to propagate.
397
398 In fact, the above algorithm is first tried for methods named
399 \method{default_open}. If all such methods return
400 \constant{None}, the algorithm is repeated for methods named like
401 \method{\var{protocol}_open()}. If all such methods return
402 \constant{None}, the algorithm is repeated for methods named
403 \method{unknown_open()}.
404
405 Note that the implementation of these methods may involve calls of
406 the parent \class{OpenerDirector} instance's \method{.open()} and
407 \method{.error()} methods.
408
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000409 \item Every handler with a method named like
410 \method{\var{protocol}_response()} has that method called to
411 post-process the response.
412
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000413\end{enumerate}
Fred Drake93c86712001-03-02 20:39:34 +0000414
415\subsection{BaseHandler Objects \label{base-handler-objects}}
416
417\class{BaseHandler} objects provide a couple of methods that are
418directly useful, and others that are meant to be used by derived
419classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000420
421\begin{methoddesc}[BaseHandler]{add_parent}{director}
422Add a director as parent.
423\end{methoddesc}
424
425\begin{methoddesc}[BaseHandler]{close}{}
426Remove any parents.
427\end{methoddesc}
428
Fred Drake399bc8c2001-11-09 03:49:29 +0000429The following members and methods should only be used by classes
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000430derived from \class{BaseHandler}. \note{The convention has been
431adopted that subclasses defining \method{\var{protocol}_request()} or
432\method{\var{protocol}_response()} methods are named
433\class{*Processor}; all others are named \class{*Handler}.}
434
Moshe Zadka8a18e992001-03-01 08:40:42 +0000435
436\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000437A valid \class{OpenerDirector}, which can be used to open using a
438different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000439\end{memberdesc}
440
441\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000442This method is \emph{not} defined in \class{BaseHandler}, but
443subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000444
Fred Drake399bc8c2001-11-09 03:49:29 +0000445This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000446\class{OpenerDirector}. It should return a file-like object as
447described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000448\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000449\exception{URLError}, unless a truly exceptional thing happens (for
450example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000451\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000452
453This method will be called before any protocol-specific open method.
454\end{methoddesc}
455
Fred Drake47852462001-05-11 15:46:45 +0000456\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000457This method is \emph{not} defined in \class{BaseHandler}, but
458subclasses should define it if they want to handle URLs with the given
459protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000460
Fred Drake399bc8c2001-11-09 03:49:29 +0000461This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000462\class{OpenerDirector}. Return values should be the same as for
463\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000464\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000465
466\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000467This method is \var{not} defined in \class{BaseHandler}, but
468subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000469specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000470
Fred Drake399bc8c2001-11-09 03:49:29 +0000471This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000472\class{OpenerDirector}. Return values should be the same as for
473\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000474\end{methoddesc}
475
476\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000477This method is \emph{not} defined in \class{BaseHandler}, but
478subclasses should override it if they intend to provide a catch-all
479for otherwise unhandled HTTP errors. It will be called automatically
480by the \class{OpenerDirector} getting the error, and should not
481normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000482
Fred Drake93c86712001-03-02 20:39:34 +0000483\var{req} will be a \class{Request} object, \var{fp} will be a
484file-like object with the HTTP error body, \var{code} will be the
485three-digit code of the error, \var{msg} will be the user-visible
486explanation of the code and \var{hdrs} will be a mapping object with
487the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000488
489Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000490of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000491\end{methoddesc}
492
Fred Drake93c86712001-03-02 20:39:34 +0000493\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
494\var{nnn} should be a three-digit HTTP error code. This method is
495also not defined in \class{BaseHandler}, but will be called, if it
496exists, on an instance of a subclass, when an HTTP error with code
497\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000498
Fred Drake93c86712001-03-02 20:39:34 +0000499Subclasses should override this method to handle specific HTTP
500errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000501
Fred Drake93c86712001-03-02 20:39:34 +0000502Arguments, return values and exceptions raised should be the same as
503for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000504\end{methoddesc}
505
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000506\begin{methoddescni}[BaseHandler]{\var{protocol}_request}{req}
507This method is \emph{not} defined in \class{BaseHandler}, but
508subclasses should define it if they want to pre-process requests of
509the given protocol.
510
511This method, if defined, will be called by the parent
512\class{OpenerDirector}. \var{req} will be a \class{Request} object.
513The return value should be a \class{Request} object.
514\end{methoddescni}
515
516\begin{methoddescni}[BaseHandler]{\var{protocol}_response}{req, response}
517This method is \emph{not} defined in \class{BaseHandler}, but
518subclasses should define it if they want to post-process responses of
519the given protocol.
520
521This method, if defined, will be called by the parent
522\class{OpenerDirector}. \var{req} will be a \class{Request} object.
523\var{response} will be an object implementing the same interface as
524the return value of \function{urlopen()}. The return value should
525implement the same interface as the return value of
526\function{urlopen()}.
527\end{methoddescni}
528
Fred Drake93c86712001-03-02 20:39:34 +0000529\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000530
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000531\note{Some HTTP redirections require action from this module's client
532 code. If this is the case, \exception{HTTPError} is raised. See
533 \rfc{2616} for details of the precise meanings of the various
534 redirection codes.}
535
536\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
537 fp, code, msg, hdrs}
538Return a \class{Request} or \code{None} in response to a redirect.
539This is called by the default implementations of the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000540\method{http_error_30*()} methods when a redirection is received from
541the server. If a redirection should take place, return a new
Fred Draked9cf8e72003-07-14 21:07:05 +0000542\class{Request} to allow \method{http_error_30*()} to perform the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000543redirect. Otherwise, raise \exception{HTTPError} if no other handler
544should try to handle this URL, or return \code{None} if you can't but
545another handler might.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000546
Fred Draked9cf8e72003-07-14 21:07:05 +0000547\begin{notice}
548 The default implementation of this method does not strictly
549 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000550 requests must not be automatically redirected without confirmation by
551 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000552 these responses, changing the POST to a \code{GET}, and the default
553 implementation reproduces this behavior.
554\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000555\end{methoddesc}
556
Moshe Zadka8a18e992001-03-01 08:40:42 +0000557
Fred Drake93c86712001-03-02 20:39:34 +0000558\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
559 fp, code, msg, hdrs}
560Redirect to the \code{Location:} URL. This method is called by
561the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000562`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000563\end{methoddesc}
564
Fred Drake93c86712001-03-02 20:39:34 +0000565\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
566 fp, code, msg, hdrs}
567The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000568`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000569\end{methoddesc}
570
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000571\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
572 fp, code, msg, hdrs}
573The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000574`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000575\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000576
Martin v. Löwis162f0812003-07-12 07:33:32 +0000577\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
578 fp, code, msg, hdrs}
579The same as \method{http_error_301()}, but called for the
580`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000581\end{methoddesc}
582
Martin v. Löwis162f0812003-07-12 07:33:32 +0000583
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000584\subsection{HTTPCookieProcessor Objects \label{http-cookie-processor}}
585
Andrew M. Kuchlingd54a0ae2005-12-04 20:25:23 +0000586\versionadded{2.4}
587
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000588\class{HTTPCookieProcessor} instances have one attribute:
589
590\begin{memberdesc}{cookiejar}
591The \class{cookielib.CookieJar} in which cookies are stored.
592\end{memberdesc}
593
594
Fred Drake93c86712001-03-02 20:39:34 +0000595\subsection{ProxyHandler Objects \label{proxy-handler}}
596
Fred Drake47852462001-05-11 15:46:45 +0000597\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000598The \class{ProxyHandler} will have a method
599\method{\var{protocol}_open()} for every \var{protocol} which has a
600proxy in the \var{proxies} dictionary given in the constructor. The
601method will modify requests to go through the proxy, by calling
602\code{request.set_proxy()}, and call the next handler in the chain to
603actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000604\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000605
606
607\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
608
609These methods are available on \class{HTTPPasswordMgr} and
610\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000611
612\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
Fred Drakebb066cf2004-05-12 03:07:27 +0000613\var{uri} can be either a single URI, or a sequence of URIs. \var{realm},
Moshe Zadka8a18e992001-03-01 08:40:42 +0000614\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000615\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000616when authentication for \var{realm} and a super-URI of any of the
617given URIs is given.
618\end{methoddesc}
619
620\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000621Get user/password for given realm and URI, if any. This method will
622return \code{(None, None)} if there is no matching user/password.
623
624For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
625\code{None} will be searched if the given \var{realm} has no matching
626user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000627\end{methoddesc}
628
Moshe Zadka8a18e992001-03-01 08:40:42 +0000629
Fred Drake93c86712001-03-02 20:39:34 +0000630\subsection{AbstractBasicAuthHandler Objects
631 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000632
633\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
634 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000635Handle an authentication request by getting a user/password pair, and
636re-trying the request. \var{authreq} should be the name of the header
637where the information about the realm is included in the request,
638\var{host} is the host to authenticate to, \var{req} should be the
639(failed) \class{Request} object, and \var{headers} should be the error
640headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000641\end{methoddesc}
642
Fred Drake93c86712001-03-02 20:39:34 +0000643
644\subsection{HTTPBasicAuthHandler Objects
645 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000646
647\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
648 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000649Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000650\end{methoddesc}
651
Fred Drake93c86712001-03-02 20:39:34 +0000652
653\subsection{ProxyBasicAuthHandler Objects
654 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000655
656\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
657 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000658Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000659\end{methoddesc}
660
Moshe Zadka8a18e992001-03-01 08:40:42 +0000661
Fred Drake93c86712001-03-02 20:39:34 +0000662\subsection{AbstractDigestAuthHandler Objects
663 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000664
Fred Drake93c86712001-03-02 20:39:34 +0000665\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000666 {authreq, host, req, headers}
667\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000668the realm is included in the request, \var{host} should be the host to
669authenticate to, \var{req} should be the (failed) \class{Request}
670object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000671\end{methoddesc}
672
Fred Drake93c86712001-03-02 20:39:34 +0000673
674\subsection{HTTPDigestAuthHandler Objects
675 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000676
677\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
678 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000679Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000680\end{methoddesc}
681
Fred Drake93c86712001-03-02 20:39:34 +0000682
683\subsection{ProxyDigestAuthHandler Objects
684 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000685
686\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
687 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000688Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000689\end{methoddesc}
690
Fred Drake93c86712001-03-02 20:39:34 +0000691
692\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000693
694\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000695Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000696\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000697\end{methoddesc}
698
Fred Drake93c86712001-03-02 20:39:34 +0000699
700\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000701
702\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000703Send an HTTPS request, which can be either GET or POST, depending on
704\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000705\end{methoddesc}
706
Moshe Zadka8a18e992001-03-01 08:40:42 +0000707
Fred Drake93c86712001-03-02 20:39:34 +0000708\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000709
710\begin{methoddesc}[FileHandler]{file_open}{req}
711Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000712the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000713protocol to \code{ftp} otherwise, and retry opening
714it using \member{parent}.
715\end{methoddesc}
716
Fred Drake93c86712001-03-02 20:39:34 +0000717
718\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000719
720\begin{methoddesc}[FTPHandler]{ftp_open}{req}
721Open the FTP file indicated by \var{req}.
722The login is always done with empty username and password.
723\end{methoddesc}
724
Moshe Zadka8a18e992001-03-01 08:40:42 +0000725
Fred Drake93c86712001-03-02 20:39:34 +0000726\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
727
728\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
729the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000730
731\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
732Set timeout of connections to \var{t} seconds.
733\end{methoddesc}
734
735\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
736Set maximum number of cached connections to \var{m}.
737\end{methoddesc}
738
Fred Drake93c86712001-03-02 20:39:34 +0000739
740\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000741
742\begin{methoddesc}[GopherHandler]{gopher_open}{req}
743Open the gopher resource indicated by \var{req}.
744\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000745
746
747\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
748
Fred Drakea9399112001-07-05 21:14:03 +0000749\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000750Raise a \exception{URLError} exception.
751\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000752
753
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000754\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
755
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000756\versionadded{2.4}
757
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000758\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
759Process HTTP error responses.
760
761For 200 error codes, the response object is returned immediately.
762
763For non-200 error codes, this simply passes the job on to the
764\method{\var{protocol}_error_\var{code}()} handler methods, via
765\method{OpenerDirector.error()}. Eventually,
766\class{urllib2.HTTPDefaultErrorHandler} will raise an
767\exception{HTTPError} if no other handler handles the error.
768\end{methoddesc}
769
770
Fred Drake53e5b712003-04-25 15:27:33 +0000771\subsection{Examples \label{urllib2-examples}}
772
773This example gets the python.org main page and displays the first 100
774bytes of it:
775
776\begin{verbatim}
777>>> import urllib2
778>>> f = urllib2.urlopen('http://www.python.org/')
779>>> print f.read(100)
780<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
781<?xml-stylesheet href="./css/ht2html
782\end{verbatim}
783
784Here we are sending a data-stream to the stdin of a CGI and reading
Georg Brandla2764ad2005-12-26 23:36:32 +0000785the data it returns to us. Note that this example will only work when the
786Python installation supports SSL.
Fred Drake53e5b712003-04-25 15:27:33 +0000787
788\begin{verbatim}
789>>> import urllib2
790>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
791... data='This data is passed to stdin of the CGI')
792>>> f = urllib2.urlopen(req)
793>>> print f.read()
794Got Data: "This data is passed to stdin of the CGI"
795\end{verbatim}
796
797The code for the sample CGI used in the above example is:
798
799\begin{verbatim}
800#!/usr/bin/env python
801import sys
802data = sys.stdin.read()
Raymond Hettinger5de33782004-02-08 20:25:01 +0000803print 'Content-type: text-plain\n\nGot Data: "%s"' % data
Fred Drake53e5b712003-04-25 15:27:33 +0000804\end{verbatim}
Martin v. Löwisbe837372004-08-25 11:24:42 +0000805
806
807Use of Basic HTTP Authentication:
808
809\begin{verbatim}
810import urllib2
811# Create an OpenerDirector with support for Basic HTTP Authentication...
812auth_handler = urllib2.HTTPBasicAuthHandler()
813auth_handler.add_password('realm', 'host', 'username', 'password')
814opener = urllib2.build_opener(auth_handler)
815# ...and install it globally so it can be used with urlopen.
816urllib2.install_opener(opener)
817urllib2.urlopen('http://www.example.com/login.html')
818\end{verbatim}
819
820\function{build_opener()} provides many handlers by default, including a
821\class{ProxyHandler}. By default, \class{ProxyHandler} uses the
822environment variables named \code{<scheme>_proxy}, where \code{<scheme>}
823is the URL scheme involved. For example, the \envvar{http_proxy}
824environment variable is read to obtain the HTTP proxy's URL.
825
826This example replaces the default \class{ProxyHandler} with one that uses
827programatically-supplied proxy URLs, and adds proxy authorization support
828with \class{ProxyBasicAuthHandler}.
829
830\begin{verbatim}
831proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
832proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
833proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
834
835opener = build_opener(proxy_handler, proxy_auth_handler)
836# This time, rather than install the OpenerDirector, we use it directly:
837opener.open('http://www.example.com/login.html')
838\end{verbatim}
839
840
841Adding HTTP headers:
842
843Use the \var{headers} argument to the \class{Request} constructor, or:
844
845\begin{verbatim}
846import urllib2
847req = urllib2.Request('http://www.example.com/')
848req.add_header('Referer', 'http://www.python.org/')
849r = urllib2.urlopen(req)
850\end{verbatim}
851
852\class{OpenerDirector} automatically adds a \mailheader{User-Agent}
853header to every \class{Request}. To change this:
854
855\begin{verbatim}
856import urllib2
857opener = urllib2.build_opener()
858opener.addheaders = [('User-agent', 'Mozilla/5.0')]
859opener.open('http://www.example.com/')
860\end{verbatim}
861
862Also, remember that a few standard headers
863(\mailheader{Content-Length}, \mailheader{Content-Type} and
864\mailheader{Host}) are added when the \class{Request} is passed to
865\function{urlopen()} (or \method{OpenerDirector.open()}).