blob: 706c54b4e8c22c37aee849a1dcd9fd5cdb587ad8 [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
74The following exceptions are raised as appropriate:
75
Moshe Zadka8a18e992001-03-01 08:40:42 +000076\begin{excdesc}{URLError}
Fred Drake399bc8c2001-11-09 03:49:29 +000077The handlers raise this exception (or derived exceptions) when they
78run into a problem. It is a subclass of \exception{IOError}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000079\end{excdesc}
80
81\begin{excdesc}{HTTPError}
82A subclass of \exception{URLError}, it can also function as a
Fred Drake93c86712001-03-02 20:39:34 +000083non-exceptional file-like return value (the same thing that
84\function{urlopen()} returns). This is useful when handling exotic
85HTTP errors, such as requests for authentication.
Moshe Zadka8a18e992001-03-01 08:40:42 +000086\end{excdesc}
87
88\begin{excdesc}{GopherError}
89A subclass of \exception{URLError}, this is the error raised by the
90Gopher handler.
91\end{excdesc}
92
Fred Drake93c86712001-03-02 20:39:34 +000093
94The following classes are provided:
95
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000096\begin{classdesc}{Request}{url\optional{, data}\optional{, headers}
97 \optional{, origin_req_host}\optional{, unverifiable}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000098This class is an abstraction of a URL request.
99
Fred Drake399bc8c2001-11-09 03:49:29 +0000100\var{url} should be a string which is a valid URL. For a description
Fred Drake93c86712001-03-02 20:39:34 +0000101of \var{data} see the \method{add_data()} description.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000102\var{headers} should be a dictionary, and will be treated as if
Fred Drake93c86712001-03-02 20:39:34 +0000103\method{add_header()} was called with each key and value as arguments.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000104
105The final two arguments are only of interest for correct handling of
106third-party HTTP cookies:
107
108\var{origin_req_host} should be the request-host of the origin
109transaction, as defined by \rfc{2965}. It defaults to
110\code{cookielib.request_host(self)}. This is the host name or IP
111address of the original request that was initiated by the user. For
112example, if the request is for an image in an HTML document, this
113should be the request-host of the request for the page containing the
114image.
115
116\var{unverifiable} should indicate whether the request is
117unverifiable, as defined by RFC 2965. It defaults to False. An
118unverifiable request is one whose URL the user did not have the option
119to approve. For example, if the request is for an image in an HTML
120document, and the user had no option to approve the automatic fetching
121of the image, this should be true.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000122\end{classdesc}
123
Fred Drake93c86712001-03-02 20:39:34 +0000124\begin{classdesc}{OpenerDirector}{}
125The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
126chained together. It manages the chaining of handlers, and recovery
127from errors.
128\end{classdesc}
129
130\begin{classdesc}{BaseHandler}{}
131This is the base class for all registered handlers --- and handles only
132the simple mechanics of registration.
133\end{classdesc}
134
135\begin{classdesc}{HTTPDefaultErrorHandler}{}
136A class which defines a default handler for HTTP error responses; all
137responses are turned into \exception{HTTPError} exceptions.
138\end{classdesc}
139
140\begin{classdesc}{HTTPRedirectHandler}{}
141A class to handle redirections.
142\end{classdesc}
143
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000144\begin{classdesc}{HTTPCookieProcessor}{\optional{cookiejar}}
145A class to handle HTTP Cookies.
146\end{classdesc}
147
Fred Drake93c86712001-03-02 20:39:34 +0000148\begin{classdesc}{ProxyHandler}{\optional{proxies}}
149Cause requests to go through a proxy.
150If \var{proxies} is given, it must be a dictionary mapping
151protocol names to URLs of proxies.
152The default is to read the list of proxies from the environment
Martin v. Löwisbe837372004-08-25 11:24:42 +0000153variables \envvar{<protocol>_proxy}.
Fred Drake93c86712001-03-02 20:39:34 +0000154\end{classdesc}
155
156\begin{classdesc}{HTTPPasswordMgr}{}
157Keep a database of
158\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
159mappings.
160\end{classdesc}
161
162\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
163Keep a database of
164\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
165A realm of \code{None} is considered a catch-all realm, which is searched
166if no other realm fits.
167\end{classdesc}
168
169\begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
170This is a mixin class that helps with HTTP authentication, both
171to the remote host and to a proxy.
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}{HTTPBasicAuthHandler}{\optional{password_mgr}}
178Handle authentication with the remote host.
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}{ProxyBasicAuthHandler}{\optional{password_mgr}}
185Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000186\var{password_mgr}, if given, should be something that is compatible
187with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
188for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000189\end{classdesc}
190
191\begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000192This is a mixin class that helps with HTTP authentication, both
Fred Drake93c86712001-03-02 20:39:34 +0000193to the remote host and to a proxy.
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}{HTTPDigestAuthHandler}{\optional{password_mgr}}
200Handle authentication with the remote host.
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}{ProxyDigestAuthHandler}{\optional{password_mgr}}
207Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000208\var{password_mgr}, if given, should be something that is compatible
209with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
210for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000211\end{classdesc}
212
213\begin{classdesc}{HTTPHandler}{}
214A class to handle opening of HTTP URLs.
215\end{classdesc}
216
217\begin{classdesc}{HTTPSHandler}{}
218A class to handle opening of HTTPS URLs.
219\end{classdesc}
220
221\begin{classdesc}{FileHandler}{}
222Open local files.
223\end{classdesc}
224
225\begin{classdesc}{FTPHandler}{}
226Open FTP URLs.
227\end{classdesc}
228
229\begin{classdesc}{CacheFTPHandler}{}
230Open FTP URLs, keeping a cache of open FTP connections to minimize
231delays.
232\end{classdesc}
233
234\begin{classdesc}{GopherHandler}{}
235Open gopher URLs.
236\end{classdesc}
237
238\begin{classdesc}{UnknownHandler}{}
239A catch-all class to handle unknown URLs.
240\end{classdesc}
241
242
243\subsection{Request Objects \label{request-objects}}
244
Moshe Zadka8a18e992001-03-01 08:40:42 +0000245The following methods describe all of \class{Request}'s public interface,
246and so all must be overridden in subclasses.
247
248\begin{methoddesc}[Request]{add_data}{data}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000249Set the \class{Request} data to \var{data}. This is ignored by all
250handlers except HTTP handlers --- and there it should be a byte
251string, and will change the request to be \code{POST} rather than
252\code{GET}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000253\end{methoddesc}
254
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000255\begin{methoddesc}[Request]{get_method}{}
256Return a string indicating the HTTP request method. This is only
Fred Drakecc97ebf2005-04-13 01:08:23 +0000257meaningful for HTTP requests, and currently always returns
258\code{'GET'} or \code{'POST'}.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000259\end{methoddesc}
260
Fred Drake399bc8c2001-11-09 03:49:29 +0000261\begin{methoddesc}[Request]{has_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000262Return whether the instance has a non-\code{None} data.
263\end{methoddesc}
264
Fred Drake399bc8c2001-11-09 03:49:29 +0000265\begin{methoddesc}[Request]{get_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000266Return the instance's data.
267\end{methoddesc}
268
269\begin{methoddesc}[Request]{add_header}{key, val}
Fred Drake93c86712001-03-02 20:39:34 +0000270Add another header to the request. Headers are currently ignored by
271all handlers except HTTP handlers, where they are added to the list
Fred Drake399bc8c2001-11-09 03:49:29 +0000272of headers sent to the server. Note that there cannot be more than
Fred Drake93c86712001-03-02 20:39:34 +0000273one header with the same name, and later calls will overwrite
274previous calls in case the \var{key} collides. Currently, this is
275no loss of HTTP functionality, since all headers which have meaning
Fred Drake399bc8c2001-11-09 03:49:29 +0000276when used more than once have a (header-specific) way of gaining the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000277same functionality using only one header.
278\end{methoddesc}
279
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000280\begin{methoddesc}[Request]{add_unredirected_header}{key, header}
281Add a header that will not be added to a redirected request.
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000282\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000283\end{methoddesc}
284
285\begin{methoddesc}[Request]{has_header}{header}
286Return whether the instance has the named header (checks both regular
287and unredirected).
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000288\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000289\end{methoddesc}
290
Moshe Zadka8a18e992001-03-01 08:40:42 +0000291\begin{methoddesc}[Request]{get_full_url}{}
292Return the URL given in the constructor.
293\end{methoddesc}
294
295\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000296Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000297\end{methoddesc}
298
299\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000300Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000301\end{methoddesc}
302
303\begin{methoddesc}[Request]{get_selector}{}
304Return the selector --- the part of the URL that is sent to
305the server.
306\end{methoddesc}
307
308\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000309Prepare the request by connecting to a proxy server. The \var{host}
310and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000311selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000312\end{methoddesc}
313
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000314\begin{methoddesc}[Request]{get_origin_req_host}{}
315Return the request-host of the origin transaction, as defined by
316\rfc{2965}. See the documentation for the \class{Request}
317constructor.
318\end{methoddesc}
319
320\begin{methoddesc}[Request]{is_unverifiable}{}
321Return whether the request is unverifiable, as defined by RFC 2965.
322See the documentation for the \class{Request} constructor.
323\end{methoddesc}
324
Fred Drake93c86712001-03-02 20:39:34 +0000325
326\subsection{OpenerDirector Objects \label{opener-director-objects}}
327
328\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000329
330\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000331\var{handler} should be an instance of \class{BaseHandler}. The
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000332following methods are searched, and added to the possible chains (note
333that HTTP errors are a special case).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000334
335\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000336 \item \method{\var{protocol}_open()} ---
337 signal that the handler knows how to open \var{protocol} URLs.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000338 \item \method{http_error_\var{type}()} ---
339 signal that the handler knows how to handle HTTP errors with HTTP
340 error code \var{type}.
341 \item \method{\var{protocol}_error()} ---
342 signal that the handler knows how to handle errors from
343 (non-\code{http}) \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000344 \item \method{\var{protocol}_request()} ---
345 signal that the handler knows how to pre-process \var{protocol}
346 requests.
347 \item \method{\var{protocol}_response()} ---
348 signal that the handler knows how to post-process \var{protocol}
349 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000350\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000351\end{methoddesc}
352
Moshe Zadka8a18e992001-03-01 08:40:42 +0000353\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000354Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000355optionally passing the given \var{data}.
356Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000357of \function{urlopen()} (which simply calls the \method{open()} method
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000358on the currently installed global \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000359\end{methoddesc}
360
Fred Drake93c86712001-03-02 20:39:34 +0000361\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
362 arg\optional{, \moreargs}}}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000363Handle an error of the given protocol. This will call the registered
Fred Drake399bc8c2001-11-09 03:49:29 +0000364error handlers for the given protocol with the given arguments (which
365are protocol specific). The HTTP protocol is a special case which
366uses the HTTP response code to determine the specific error handler;
367refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000368
369Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000370of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000371\end{methoddesc}
372
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000373OpenerDirector objects open URLs in three stages:
374
Andrew M. Kuchling300ce192004-07-10 18:28:33 +0000375The order in which these methods are called within each stage is
376determined by sorting the handler instances.
377
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000378\begin{enumerate}
379 \item Every handler with a method named like
380 \method{\var{protocol}_request()} has that method called to
381 pre-process the request.
382
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000383 \item Handlers with a method named like
384 \method{\var{protocol}_open()} are called to handle the request.
385 This stage ends when a handler either returns a
386 non-\constant{None} value (ie. a response), or raises an exception
387 (usually URLError). Exceptions are allowed to propagate.
388
389 In fact, the above algorithm is first tried for methods named
390 \method{default_open}. If all such methods return
391 \constant{None}, the algorithm is repeated for methods named like
392 \method{\var{protocol}_open()}. If all such methods return
393 \constant{None}, the algorithm is repeated for methods named
394 \method{unknown_open()}.
395
396 Note that the implementation of these methods may involve calls of
397 the parent \class{OpenerDirector} instance's \method{.open()} and
398 \method{.error()} methods.
399
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000400 \item Every handler with a method named like
401 \method{\var{protocol}_response()} has that method called to
402 post-process the response.
403
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000404\end{enumerate}
Fred Drake93c86712001-03-02 20:39:34 +0000405
406\subsection{BaseHandler Objects \label{base-handler-objects}}
407
408\class{BaseHandler} objects provide a couple of methods that are
409directly useful, and others that are meant to be used by derived
410classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000411
412\begin{methoddesc}[BaseHandler]{add_parent}{director}
413Add a director as parent.
414\end{methoddesc}
415
416\begin{methoddesc}[BaseHandler]{close}{}
417Remove any parents.
418\end{methoddesc}
419
Fred Drake399bc8c2001-11-09 03:49:29 +0000420The following members and methods should only be used by classes
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000421derived from \class{BaseHandler}. \note{The convention has been
422adopted that subclasses defining \method{\var{protocol}_request()} or
423\method{\var{protocol}_response()} methods are named
424\class{*Processor}; all others are named \class{*Handler}.}
425
Moshe Zadka8a18e992001-03-01 08:40:42 +0000426
427\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000428A valid \class{OpenerDirector}, which can be used to open using a
429different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000430\end{memberdesc}
431
432\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000433This method is \emph{not} defined in \class{BaseHandler}, but
434subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000435
Fred Drake399bc8c2001-11-09 03:49:29 +0000436This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000437\class{OpenerDirector}. It should return a file-like object as
438described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000439\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000440\exception{URLError}, unless a truly exceptional thing happens (for
441example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000442\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000443
444This method will be called before any protocol-specific open method.
445\end{methoddesc}
446
Fred Drake47852462001-05-11 15:46:45 +0000447\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000448This method is \emph{not} defined in \class{BaseHandler}, but
449subclasses should define it if they want to handle URLs with the given
450protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000451
Fred Drake399bc8c2001-11-09 03:49:29 +0000452This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000453\class{OpenerDirector}. Return values should be the same as for
454\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000455\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000456
457\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000458This method is \var{not} defined in \class{BaseHandler}, but
459subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000460specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000461
Fred Drake399bc8c2001-11-09 03:49:29 +0000462This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000463\class{OpenerDirector}. Return values should be the same as for
464\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000465\end{methoddesc}
466
467\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000468This method is \emph{not} defined in \class{BaseHandler}, but
469subclasses should override it if they intend to provide a catch-all
470for otherwise unhandled HTTP errors. It will be called automatically
471by the \class{OpenerDirector} getting the error, and should not
472normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000473
Fred Drake93c86712001-03-02 20:39:34 +0000474\var{req} will be a \class{Request} object, \var{fp} will be a
475file-like object with the HTTP error body, \var{code} will be the
476three-digit code of the error, \var{msg} will be the user-visible
477explanation of the code and \var{hdrs} will be a mapping object with
478the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000479
480Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000481of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000482\end{methoddesc}
483
Fred Drake93c86712001-03-02 20:39:34 +0000484\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
485\var{nnn} should be a three-digit HTTP error code. This method is
486also not defined in \class{BaseHandler}, but will be called, if it
487exists, on an instance of a subclass, when an HTTP error with code
488\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000489
Fred Drake93c86712001-03-02 20:39:34 +0000490Subclasses should override this method to handle specific HTTP
491errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000492
Fred Drake93c86712001-03-02 20:39:34 +0000493Arguments, return values and exceptions raised should be the same as
494for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000495\end{methoddesc}
496
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000497\begin{methoddescni}[BaseHandler]{\var{protocol}_request}{req}
498This method is \emph{not} defined in \class{BaseHandler}, but
499subclasses should define it if they want to pre-process requests of
500the given protocol.
501
502This method, if defined, will be called by the parent
503\class{OpenerDirector}. \var{req} will be a \class{Request} object.
504The return value should be a \class{Request} object.
505\end{methoddescni}
506
507\begin{methoddescni}[BaseHandler]{\var{protocol}_response}{req, response}
508This method is \emph{not} defined in \class{BaseHandler}, but
509subclasses should define it if they want to post-process responses of
510the given protocol.
511
512This method, if defined, will be called by the parent
513\class{OpenerDirector}. \var{req} will be a \class{Request} object.
514\var{response} will be an object implementing the same interface as
515the return value of \function{urlopen()}. The return value should
516implement the same interface as the return value of
517\function{urlopen()}.
518\end{methoddescni}
519
Fred Drake93c86712001-03-02 20:39:34 +0000520\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000521
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000522\note{Some HTTP redirections require action from this module's client
523 code. If this is the case, \exception{HTTPError} is raised. See
524 \rfc{2616} for details of the precise meanings of the various
525 redirection codes.}
526
527\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
528 fp, code, msg, hdrs}
529Return a \class{Request} or \code{None} in response to a redirect.
530This is called by the default implementations of the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000531\method{http_error_30*()} methods when a redirection is received from
532the server. If a redirection should take place, return a new
Fred Draked9cf8e72003-07-14 21:07:05 +0000533\class{Request} to allow \method{http_error_30*()} to perform the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000534redirect. Otherwise, raise \exception{HTTPError} if no other handler
535should try to handle this URL, or return \code{None} if you can't but
536another handler might.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000537
Fred Draked9cf8e72003-07-14 21:07:05 +0000538\begin{notice}
539 The default implementation of this method does not strictly
540 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000541 requests must not be automatically redirected without confirmation by
542 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000543 these responses, changing the POST to a \code{GET}, and the default
544 implementation reproduces this behavior.
545\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000546\end{methoddesc}
547
Moshe Zadka8a18e992001-03-01 08:40:42 +0000548
Fred Drake93c86712001-03-02 20:39:34 +0000549\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
550 fp, code, msg, hdrs}
551Redirect to the \code{Location:} URL. This method is called by
552the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000553`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000554\end{methoddesc}
555
Fred Drake93c86712001-03-02 20:39:34 +0000556\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
557 fp, code, msg, hdrs}
558The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000559`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000560\end{methoddesc}
561
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000562\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
563 fp, code, msg, hdrs}
564The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000565`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000566\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000567
Martin v. Löwis162f0812003-07-12 07:33:32 +0000568\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
569 fp, code, msg, hdrs}
570The same as \method{http_error_301()}, but called for the
571`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000572\end{methoddesc}
573
Martin v. Löwis162f0812003-07-12 07:33:32 +0000574
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000575\subsection{HTTPCookieProcessor Objects \label{http-cookie-processor}}
576
Andrew M. Kuchlingd54a0ae2005-12-04 20:25:23 +0000577\versionadded{2.4}
578
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000579\class{HTTPCookieProcessor} instances have one attribute:
580
581\begin{memberdesc}{cookiejar}
582The \class{cookielib.CookieJar} in which cookies are stored.
583\end{memberdesc}
584
585
Fred Drake93c86712001-03-02 20:39:34 +0000586\subsection{ProxyHandler Objects \label{proxy-handler}}
587
Fred Drake47852462001-05-11 15:46:45 +0000588\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000589The \class{ProxyHandler} will have a method
590\method{\var{protocol}_open()} for every \var{protocol} which has a
591proxy in the \var{proxies} dictionary given in the constructor. The
592method will modify requests to go through the proxy, by calling
593\code{request.set_proxy()}, and call the next handler in the chain to
594actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000595\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000596
597
598\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
599
600These methods are available on \class{HTTPPasswordMgr} and
601\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000602
603\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
Fred Drakebb066cf2004-05-12 03:07:27 +0000604\var{uri} can be either a single URI, or a sequence of URIs. \var{realm},
Moshe Zadka8a18e992001-03-01 08:40:42 +0000605\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000606\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000607when authentication for \var{realm} and a super-URI of any of the
608given URIs is given.
609\end{methoddesc}
610
611\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000612Get user/password for given realm and URI, if any. This method will
613return \code{(None, None)} if there is no matching user/password.
614
615For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
616\code{None} will be searched if the given \var{realm} has no matching
617user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000618\end{methoddesc}
619
Moshe Zadka8a18e992001-03-01 08:40:42 +0000620
Fred Drake93c86712001-03-02 20:39:34 +0000621\subsection{AbstractBasicAuthHandler Objects
622 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000623
624\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
625 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000626Handle an authentication request by getting a user/password pair, and
627re-trying the request. \var{authreq} should be the name of the header
628where the information about the realm is included in the request,
629\var{host} is the host to authenticate to, \var{req} should be the
630(failed) \class{Request} object, and \var{headers} should be the error
631headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000632\end{methoddesc}
633
Fred Drake93c86712001-03-02 20:39:34 +0000634
635\subsection{HTTPBasicAuthHandler Objects
636 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000637
638\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{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
Fred Drake93c86712001-03-02 20:39:34 +0000643
644\subsection{ProxyBasicAuthHandler Objects
645 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000646
647\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{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
Moshe Zadka8a18e992001-03-01 08:40:42 +0000652
Fred Drake93c86712001-03-02 20:39:34 +0000653\subsection{AbstractDigestAuthHandler Objects
654 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000655
Fred Drake93c86712001-03-02 20:39:34 +0000656\begin{methoddesc}[AbstractDigestAuthHandler]{handle_authentication_request}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000657 {authreq, host, req, headers}
658\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000659the realm is included in the request, \var{host} should be the host to
660authenticate to, \var{req} should be the (failed) \class{Request}
661object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000662\end{methoddesc}
663
Fred Drake93c86712001-03-02 20:39:34 +0000664
665\subsection{HTTPDigestAuthHandler Objects
666 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000667
668\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
669 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +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{ProxyDigestAuthHandler Objects
675 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000676
677\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
678 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +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{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000684
685\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000686Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000687\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000688\end{methoddesc}
689
Fred Drake93c86712001-03-02 20:39:34 +0000690
691\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000692
693\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000694Send an HTTPS request, which can be either GET or POST, depending on
695\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000696\end{methoddesc}
697
Moshe Zadka8a18e992001-03-01 08:40:42 +0000698
Fred Drake93c86712001-03-02 20:39:34 +0000699\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000700
701\begin{methoddesc}[FileHandler]{file_open}{req}
702Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000703the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000704protocol to \code{ftp} otherwise, and retry opening
705it using \member{parent}.
706\end{methoddesc}
707
Fred Drake93c86712001-03-02 20:39:34 +0000708
709\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000710
711\begin{methoddesc}[FTPHandler]{ftp_open}{req}
712Open the FTP file indicated by \var{req}.
713The login is always done with empty username and password.
714\end{methoddesc}
715
Moshe Zadka8a18e992001-03-01 08:40:42 +0000716
Fred Drake93c86712001-03-02 20:39:34 +0000717\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
718
719\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
720the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000721
722\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
723Set timeout of connections to \var{t} seconds.
724\end{methoddesc}
725
726\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
727Set maximum number of cached connections to \var{m}.
728\end{methoddesc}
729
Fred Drake93c86712001-03-02 20:39:34 +0000730
731\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000732
733\begin{methoddesc}[GopherHandler]{gopher_open}{req}
734Open the gopher resource indicated by \var{req}.
735\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000736
737
738\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
739
Fred Drakea9399112001-07-05 21:14:03 +0000740\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000741Raise a \exception{URLError} exception.
742\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000743
744
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000745\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
746
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000747\versionadded{2.4}
748
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000749\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
750Process HTTP error responses.
751
752For 200 error codes, the response object is returned immediately.
753
754For non-200 error codes, this simply passes the job on to the
755\method{\var{protocol}_error_\var{code}()} handler methods, via
756\method{OpenerDirector.error()}. Eventually,
757\class{urllib2.HTTPDefaultErrorHandler} will raise an
758\exception{HTTPError} if no other handler handles the error.
759\end{methoddesc}
760
761
Fred Drake53e5b712003-04-25 15:27:33 +0000762\subsection{Examples \label{urllib2-examples}}
763
764This example gets the python.org main page and displays the first 100
765bytes of it:
766
767\begin{verbatim}
768>>> import urllib2
769>>> f = urllib2.urlopen('http://www.python.org/')
770>>> print f.read(100)
771<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
772<?xml-stylesheet href="./css/ht2html
773\end{verbatim}
774
775Here we are sending a data-stream to the stdin of a CGI and reading
Georg Brandla2764ad2005-12-26 23:36:32 +0000776the data it returns to us. Note that this example will only work when the
777Python installation supports SSL.
Fred Drake53e5b712003-04-25 15:27:33 +0000778
779\begin{verbatim}
780>>> import urllib2
781>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
782... data='This data is passed to stdin of the CGI')
783>>> f = urllib2.urlopen(req)
784>>> print f.read()
785Got Data: "This data is passed to stdin of the CGI"
786\end{verbatim}
787
788The code for the sample CGI used in the above example is:
789
790\begin{verbatim}
791#!/usr/bin/env python
792import sys
793data = sys.stdin.read()
Raymond Hettinger5de33782004-02-08 20:25:01 +0000794print 'Content-type: text-plain\n\nGot Data: "%s"' % data
Fred Drake53e5b712003-04-25 15:27:33 +0000795\end{verbatim}
Martin v. Löwisbe837372004-08-25 11:24:42 +0000796
797
798Use of Basic HTTP Authentication:
799
800\begin{verbatim}
801import urllib2
802# Create an OpenerDirector with support for Basic HTTP Authentication...
803auth_handler = urllib2.HTTPBasicAuthHandler()
804auth_handler.add_password('realm', 'host', 'username', 'password')
805opener = urllib2.build_opener(auth_handler)
806# ...and install it globally so it can be used with urlopen.
807urllib2.install_opener(opener)
808urllib2.urlopen('http://www.example.com/login.html')
809\end{verbatim}
810
811\function{build_opener()} provides many handlers by default, including a
812\class{ProxyHandler}. By default, \class{ProxyHandler} uses the
813environment variables named \code{<scheme>_proxy}, where \code{<scheme>}
814is the URL scheme involved. For example, the \envvar{http_proxy}
815environment variable is read to obtain the HTTP proxy's URL.
816
817This example replaces the default \class{ProxyHandler} with one that uses
818programatically-supplied proxy URLs, and adds proxy authorization support
819with \class{ProxyBasicAuthHandler}.
820
821\begin{verbatim}
822proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
823proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
824proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
825
826opener = build_opener(proxy_handler, proxy_auth_handler)
827# This time, rather than install the OpenerDirector, we use it directly:
828opener.open('http://www.example.com/login.html')
829\end{verbatim}
830
831
832Adding HTTP headers:
833
834Use the \var{headers} argument to the \class{Request} constructor, or:
835
836\begin{verbatim}
837import urllib2
838req = urllib2.Request('http://www.example.com/')
839req.add_header('Referer', 'http://www.python.org/')
840r = urllib2.urlopen(req)
841\end{verbatim}
842
843\class{OpenerDirector} automatically adds a \mailheader{User-Agent}
844header to every \class{Request}. To change this:
845
846\begin{verbatim}
847import urllib2
848opener = urllib2.build_opener()
849opener.addheaders = [('User-agent', 'Mozilla/5.0')]
850opener.open('http://www.example.com/')
851\end{verbatim}
852
853Also, remember that a few standard headers
854(\mailheader{Content-Length}, \mailheader{Content-Type} and
855\mailheader{Host}) are added when the \class{Request} is passed to
856\function{urlopen()} (or \method{OpenerDirector.open()}).