blob: f77ed2535362f3b549e63ecab847eda407412d32 [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
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000068position in the handlers list.
Moshe Zadka8a18e992001-03-01 08:40:42 +000069\end{funcdesc}
70
Fred Drake93c86712001-03-02 20:39:34 +000071
72The following exceptions are raised as appropriate:
73
Moshe Zadka8a18e992001-03-01 08:40:42 +000074\begin{excdesc}{URLError}
Fred Drake399bc8c2001-11-09 03:49:29 +000075The handlers raise this exception (or derived exceptions) when they
76run into a problem. It is a subclass of \exception{IOError}.
Moshe Zadka8a18e992001-03-01 08:40:42 +000077\end{excdesc}
78
79\begin{excdesc}{HTTPError}
80A subclass of \exception{URLError}, it can also function as a
Fred Drake93c86712001-03-02 20:39:34 +000081non-exceptional file-like return value (the same thing that
82\function{urlopen()} returns). This is useful when handling exotic
83HTTP errors, such as requests for authentication.
Moshe Zadka8a18e992001-03-01 08:40:42 +000084\end{excdesc}
85
86\begin{excdesc}{GopherError}
87A subclass of \exception{URLError}, this is the error raised by the
88Gopher handler.
89\end{excdesc}
90
Fred Drake93c86712001-03-02 20:39:34 +000091
92The following classes are provided:
93
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000094\begin{classdesc}{Request}{url\optional{, data}\optional{, headers}
95 \optional{, origin_req_host}\optional{, unverifiable}}
Moshe Zadka8a18e992001-03-01 08:40:42 +000096This class is an abstraction of a URL request.
97
Fred Drake399bc8c2001-11-09 03:49:29 +000098\var{url} should be a string which is a valid URL. For a description
Fred Drake93c86712001-03-02 20:39:34 +000099of \var{data} see the \method{add_data()} description.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000100\var{headers} should be a dictionary, and will be treated as if
Fred Drake93c86712001-03-02 20:39:34 +0000101\method{add_header()} was called with each key and value as arguments.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000102
103The final two arguments are only of interest for correct handling of
104third-party HTTP cookies:
105
106\var{origin_req_host} should be the request-host of the origin
107transaction, as defined by \rfc{2965}. It defaults to
108\code{cookielib.request_host(self)}. This is the host name or IP
109address of the original request that was initiated by the user. For
110example, if the request is for an image in an HTML document, this
111should be the request-host of the request for the page containing the
112image.
113
114\var{unverifiable} should indicate whether the request is
115unverifiable, as defined by RFC 2965. It defaults to False. An
116unverifiable request is one whose URL the user did not have the option
117to approve. For example, if the request is for an image in an HTML
118document, and the user had no option to approve the automatic fetching
119of the image, this should be true.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000120\end{classdesc}
121
Fred Drake93c86712001-03-02 20:39:34 +0000122\begin{classdesc}{OpenerDirector}{}
123The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s
124chained together. It manages the chaining of handlers, and recovery
125from errors.
126\end{classdesc}
127
128\begin{classdesc}{BaseHandler}{}
129This is the base class for all registered handlers --- and handles only
130the simple mechanics of registration.
131\end{classdesc}
132
133\begin{classdesc}{HTTPDefaultErrorHandler}{}
134A class which defines a default handler for HTTP error responses; all
135responses are turned into \exception{HTTPError} exceptions.
136\end{classdesc}
137
138\begin{classdesc}{HTTPRedirectHandler}{}
139A class to handle redirections.
140\end{classdesc}
141
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000142\begin{classdesc}{HTTPCookieProcessor}{\optional{cookiejar}}
143A class to handle HTTP Cookies.
144\end{classdesc}
145
Fred Drake93c86712001-03-02 20:39:34 +0000146\begin{classdesc}{ProxyHandler}{\optional{proxies}}
147Cause requests to go through a proxy.
148If \var{proxies} is given, it must be a dictionary mapping
149protocol names to URLs of proxies.
150The default is to read the list of proxies from the environment
Martin v. Löwisbe837372004-08-25 11:24:42 +0000151variables \envvar{<protocol>_proxy}.
Fred Drake93c86712001-03-02 20:39:34 +0000152\end{classdesc}
153
154\begin{classdesc}{HTTPPasswordMgr}{}
155Keep a database of
156\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})}
157mappings.
158\end{classdesc}
159
160\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
161Keep a database of
162\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mappings.
163A realm of \code{None} is considered a catch-all realm, which is searched
164if no other realm fits.
165\end{classdesc}
166
167\begin{classdesc}{AbstractBasicAuthHandler}{\optional{password_mgr}}
168This is a mixin class that helps with HTTP authentication, both
169to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000170\var{password_mgr}, if given, should be something that is compatible
171with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
172for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000173\end{classdesc}
174
175\begin{classdesc}{HTTPBasicAuthHandler}{\optional{password_mgr}}
176Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000177\var{password_mgr}, if given, should be something that is compatible
178with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
179for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000180\end{classdesc}
181
182\begin{classdesc}{ProxyBasicAuthHandler}{\optional{password_mgr}}
183Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000184\var{password_mgr}, if given, should be something that is compatible
185with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
186for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000187\end{classdesc}
188
189\begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000190This is a mixin class that helps with HTTP authentication, both
Fred Drake93c86712001-03-02 20:39:34 +0000191to the remote host and to a proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000192\var{password_mgr}, if given, should be something that is compatible
193with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
194for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000195\end{classdesc}
196
197\begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
198Handle authentication with the remote host.
Fred Drake399bc8c2001-11-09 03:49:29 +0000199\var{password_mgr}, if given, should be something that is compatible
200with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
201for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000202\end{classdesc}
203
204\begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
205Handle authentication with the proxy.
Fred Drake399bc8c2001-11-09 03:49:29 +0000206\var{password_mgr}, if given, should be something that is compatible
207with \class{HTTPPasswordMgr}; refer to section~\ref{http-password-mgr}
208for information on the interface that must be supported.
Fred Drake93c86712001-03-02 20:39:34 +0000209\end{classdesc}
210
211\begin{classdesc}{HTTPHandler}{}
212A class to handle opening of HTTP URLs.
213\end{classdesc}
214
215\begin{classdesc}{HTTPSHandler}{}
216A class to handle opening of HTTPS URLs.
217\end{classdesc}
218
219\begin{classdesc}{FileHandler}{}
220Open local files.
221\end{classdesc}
222
223\begin{classdesc}{FTPHandler}{}
224Open FTP URLs.
225\end{classdesc}
226
227\begin{classdesc}{CacheFTPHandler}{}
228Open FTP URLs, keeping a cache of open FTP connections to minimize
229delays.
230\end{classdesc}
231
232\begin{classdesc}{GopherHandler}{}
233Open gopher URLs.
234\end{classdesc}
235
236\begin{classdesc}{UnknownHandler}{}
237A catch-all class to handle unknown URLs.
238\end{classdesc}
239
240
241\subsection{Request Objects \label{request-objects}}
242
Moshe Zadka8a18e992001-03-01 08:40:42 +0000243The following methods describe all of \class{Request}'s public interface,
244and so all must be overridden in subclasses.
245
246\begin{methoddesc}[Request]{add_data}{data}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000247Set the \class{Request} data to \var{data}. This is ignored by all
248handlers except HTTP handlers --- and there it should be a byte
249string, and will change the request to be \code{POST} rather than
250\code{GET}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000251\end{methoddesc}
252
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000253\begin{methoddesc}[Request]{get_method}{}
254Return a string indicating the HTTP request method. This is only
Fred Drakecc97ebf2005-04-13 01:08:23 +0000255meaningful for HTTP requests, and currently always returns
256\code{'GET'} or \code{'POST'}.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000257\end{methoddesc}
258
Fred Drake399bc8c2001-11-09 03:49:29 +0000259\begin{methoddesc}[Request]{has_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000260Return whether the instance has a non-\code{None} data.
261\end{methoddesc}
262
Fred Drake399bc8c2001-11-09 03:49:29 +0000263\begin{methoddesc}[Request]{get_data}{}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000264Return the instance's data.
265\end{methoddesc}
266
267\begin{methoddesc}[Request]{add_header}{key, val}
Fred Drake93c86712001-03-02 20:39:34 +0000268Add another header to the request. Headers are currently ignored by
269all handlers except HTTP handlers, where they are added to the list
Fred Drake399bc8c2001-11-09 03:49:29 +0000270of headers sent to the server. Note that there cannot be more than
Fred Drake93c86712001-03-02 20:39:34 +0000271one header with the same name, and later calls will overwrite
272previous calls in case the \var{key} collides. Currently, this is
273no loss of HTTP functionality, since all headers which have meaning
Fred Drake399bc8c2001-11-09 03:49:29 +0000274when used more than once have a (header-specific) way of gaining the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000275same functionality using only one header.
276\end{methoddesc}
277
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000278\begin{methoddesc}[Request]{add_unredirected_header}{key, header}
279Add a header that will not be added to a redirected request.
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000280\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000281\end{methoddesc}
282
283\begin{methoddesc}[Request]{has_header}{header}
284Return whether the instance has the named header (checks both regular
285and unredirected).
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000286\versionadded{2.4}
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000287\end{methoddesc}
288
Moshe Zadka8a18e992001-03-01 08:40:42 +0000289\begin{methoddesc}[Request]{get_full_url}{}
290Return the URL given in the constructor.
291\end{methoddesc}
292
293\begin{methoddesc}[Request]{get_type}{}
Fred Drake93c86712001-03-02 20:39:34 +0000294Return the type of the URL --- also known as the scheme.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000295\end{methoddesc}
296
297\begin{methoddesc}[Request]{get_host}{}
Fred Drake399bc8c2001-11-09 03:49:29 +0000298Return the host to which a connection will be made.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000299\end{methoddesc}
300
301\begin{methoddesc}[Request]{get_selector}{}
302Return the selector --- the part of the URL that is sent to
303the server.
304\end{methoddesc}
305
306\begin{methoddesc}[Request]{set_proxy}{host, type}
Fred Drake399bc8c2001-11-09 03:49:29 +0000307Prepare the request by connecting to a proxy server. The \var{host}
308and \var{type} will replace those of the instance, and the instance's
Fred Drake93c86712001-03-02 20:39:34 +0000309selector will be the original URL given in the constructor.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000310\end{methoddesc}
311
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000312\begin{methoddesc}[Request]{get_origin_req_host}{}
313Return the request-host of the origin transaction, as defined by
314\rfc{2965}. See the documentation for the \class{Request}
315constructor.
316\end{methoddesc}
317
318\begin{methoddesc}[Request]{is_unverifiable}{}
319Return whether the request is unverifiable, as defined by RFC 2965.
320See the documentation for the \class{Request} constructor.
321\end{methoddesc}
322
Fred Drake93c86712001-03-02 20:39:34 +0000323
324\subsection{OpenerDirector Objects \label{opener-director-objects}}
325
326\class{OpenerDirector} instances have the following methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000327
328\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
Fred Drake93c86712001-03-02 20:39:34 +0000329\var{handler} should be an instance of \class{BaseHandler}. The
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000330following methods are searched, and added to the possible chains (note
331that HTTP errors are a special case).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000332
333\begin{itemize}
Fred Drake93c86712001-03-02 20:39:34 +0000334 \item \method{\var{protocol}_open()} ---
335 signal that the handler knows how to open \var{protocol} URLs.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000336 \item \method{http_error_\var{type}()} ---
337 signal that the handler knows how to handle HTTP errors with HTTP
338 error code \var{type}.
339 \item \method{\var{protocol}_error()} ---
340 signal that the handler knows how to handle errors from
341 (non-\code{http}) \var{protocol}.
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000342 \item \method{\var{protocol}_request()} ---
343 signal that the handler knows how to pre-process \var{protocol}
344 requests.
345 \item \method{\var{protocol}_response()} ---
346 signal that the handler knows how to post-process \var{protocol}
347 responses.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000348\end{itemize}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000349\end{methoddesc}
350
Moshe Zadka8a18e992001-03-01 08:40:42 +0000351\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
Fred Drake399bc8c2001-11-09 03:49:29 +0000352Open the given \var{url} (which can be a request object or a string),
Moshe Zadka8a18e992001-03-01 08:40:42 +0000353optionally passing the given \var{data}.
354Arguments, return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000355of \function{urlopen()} (which simply calls the \method{open()} method
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000356on the currently installed global \class{OpenerDirector}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000357\end{methoddesc}
358
Fred Drake93c86712001-03-02 20:39:34 +0000359\begin{methoddesc}[OpenerDirector]{error}{proto\optional{,
360 arg\optional{, \moreargs}}}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000361Handle an error of the given protocol. This will call the registered
Fred Drake399bc8c2001-11-09 03:49:29 +0000362error handlers for the given protocol with the given arguments (which
363are protocol specific). The HTTP protocol is a special case which
364uses the HTTP response code to determine the specific error handler;
365refer to the \method{http_error_*()} methods of the handler classes.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000366
367Return values and exceptions raised are the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000368of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000369\end{methoddesc}
370
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000371OpenerDirector objects open URLs in three stages:
372
Andrew M. Kuchling300ce192004-07-10 18:28:33 +0000373The order in which these methods are called within each stage is
374determined by sorting the handler instances.
375
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000376\begin{enumerate}
377 \item Every handler with a method named like
378 \method{\var{protocol}_request()} has that method called to
379 pre-process the request.
380
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000381 \item Handlers with a method named like
382 \method{\var{protocol}_open()} are called to handle the request.
383 This stage ends when a handler either returns a
384 non-\constant{None} value (ie. a response), or raises an exception
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 (usually \exception{URLError}). Exceptions are allowed to propagate.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000386
387 In fact, the above algorithm is first tried for methods named
388 \method{default_open}. If all such methods return
389 \constant{None}, the algorithm is repeated for methods named like
390 \method{\var{protocol}_open()}. If all such methods return
391 \constant{None}, the algorithm is repeated for methods named
392 \method{unknown_open()}.
393
394 Note that the implementation of these methods may involve calls of
395 the parent \class{OpenerDirector} instance's \method{.open()} and
396 \method{.error()} methods.
397
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000398 \item Every handler with a method named like
399 \method{\var{protocol}_response()} has that method called to
400 post-process the response.
401
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000402\end{enumerate}
Fred Drake93c86712001-03-02 20:39:34 +0000403
404\subsection{BaseHandler Objects \label{base-handler-objects}}
405
406\class{BaseHandler} objects provide a couple of methods that are
407directly useful, and others that are meant to be used by derived
408classes. These are intended for direct use:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000409
410\begin{methoddesc}[BaseHandler]{add_parent}{director}
411Add a director as parent.
412\end{methoddesc}
413
414\begin{methoddesc}[BaseHandler]{close}{}
415Remove any parents.
416\end{methoddesc}
417
Fred Drake399bc8c2001-11-09 03:49:29 +0000418The following members and methods should only be used by classes
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000419derived from \class{BaseHandler}. \note{The convention has been
420adopted that subclasses defining \method{\var{protocol}_request()} or
421\method{\var{protocol}_response()} methods are named
422\class{*Processor}; all others are named \class{*Handler}.}
423
Moshe Zadka8a18e992001-03-01 08:40:42 +0000424
425\begin{memberdesc}[BaseHandler]{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000426A valid \class{OpenerDirector}, which can be used to open using a
427different protocol, or handle errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000428\end{memberdesc}
429
430\begin{methoddesc}[BaseHandler]{default_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000431This method is \emph{not} defined in \class{BaseHandler}, but
432subclasses should define it if they want to catch all URLs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000433
Fred Drake399bc8c2001-11-09 03:49:29 +0000434This method, if implemented, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000435\class{OpenerDirector}. It should return a file-like object as
436described in the return value of the \method{open()} of
Fred Drake399bc8c2001-11-09 03:49:29 +0000437\class{OpenerDirector}, or \code{None}. It should raise
Fred Drake93c86712001-03-02 20:39:34 +0000438\exception{URLError}, unless a truly exceptional thing happens (for
439example, \exception{MemoryError} should not be mapped to
Fred Drake399bc8c2001-11-09 03:49:29 +0000440\exception{URLError}).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000441
442This method will be called before any protocol-specific open method.
443\end{methoddesc}
444
Fred Drake47852462001-05-11 15:46:45 +0000445\begin{methoddescni}[BaseHandler]{\var{protocol}_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000446This method is \emph{not} defined in \class{BaseHandler}, but
447subclasses should define it if they want to handle URLs with the given
448protocol.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000449
Fred Drake399bc8c2001-11-09 03:49:29 +0000450This method, if defined, will be called by the parent
Fred Drake93c86712001-03-02 20:39:34 +0000451\class{OpenerDirector}. Return values should be the same as for
452\method{default_open()}.
Fred Drake47852462001-05-11 15:46:45 +0000453\end{methoddescni}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000454
455\begin{methoddesc}[BaseHandler]{unknown_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000456This method is \var{not} defined in \class{BaseHandler}, but
457subclasses should define it if they want to catch all URLs with no
Fred Drake399bc8c2001-11-09 03:49:29 +0000458specific registered handler to open it.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000459
Fred Drake399bc8c2001-11-09 03:49:29 +0000460This method, if implemented, will be called by the \member{parent}
Fred Drake93c86712001-03-02 20:39:34 +0000461\class{OpenerDirector}. Return values should be the same as for
462\method{default_open()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000463\end{methoddesc}
464
465\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000466This method is \emph{not} defined in \class{BaseHandler}, but
467subclasses should override it if they intend to provide a catch-all
468for otherwise unhandled HTTP errors. It will be called automatically
469by the \class{OpenerDirector} getting the error, and should not
470normally be called in other circumstances.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000471
Fred Drake93c86712001-03-02 20:39:34 +0000472\var{req} will be a \class{Request} object, \var{fp} will be a
473file-like object with the HTTP error body, \var{code} will be the
474three-digit code of the error, \var{msg} will be the user-visible
475explanation of the code and \var{hdrs} will be a mapping object with
476the headers of the error.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000477
478Return values and exceptions raised should be the same as those
Fred Drake93c86712001-03-02 20:39:34 +0000479of \function{urlopen()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000480\end{methoddesc}
481
Fred Drake93c86712001-03-02 20:39:34 +0000482\begin{methoddesc}[BaseHandler]{http_error_\var{nnn}}{req, fp, code, msg, hdrs}
483\var{nnn} should be a three-digit HTTP error code. This method is
484also not defined in \class{BaseHandler}, but will be called, if it
485exists, on an instance of a subclass, when an HTTP error with code
486\var{nnn} occurs.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000487
Fred Drake93c86712001-03-02 20:39:34 +0000488Subclasses should override this method to handle specific HTTP
489errors.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000490
Fred Drake93c86712001-03-02 20:39:34 +0000491Arguments, return values and exceptions raised should be the same as
492for \method{http_error_default()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000493\end{methoddesc}
494
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000495\begin{methoddescni}[BaseHandler]{\var{protocol}_request}{req}
496This method is \emph{not} defined in \class{BaseHandler}, but
497subclasses should define it if they want to pre-process requests of
498the given protocol.
499
500This method, if defined, will be called by the parent
501\class{OpenerDirector}. \var{req} will be a \class{Request} object.
502The return value should be a \class{Request} object.
503\end{methoddescni}
504
505\begin{methoddescni}[BaseHandler]{\var{protocol}_response}{req, response}
506This method is \emph{not} defined in \class{BaseHandler}, but
507subclasses should define it if they want to post-process responses of
508the given protocol.
509
510This method, if defined, will be called by the parent
511\class{OpenerDirector}. \var{req} will be a \class{Request} object.
512\var{response} will be an object implementing the same interface as
513the return value of \function{urlopen()}. The return value should
514implement the same interface as the return value of
515\function{urlopen()}.
516\end{methoddescni}
517
Fred Drake93c86712001-03-02 20:39:34 +0000518\subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000519
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000520\note{Some HTTP redirections require action from this module's client
521 code. If this is the case, \exception{HTTPError} is raised. See
522 \rfc{2616} for details of the precise meanings of the various
523 redirection codes.}
524
525\begin{methoddesc}[HTTPRedirectHandler]{redirect_request}{req,
526 fp, code, msg, hdrs}
527Return a \class{Request} or \code{None} in response to a redirect.
528This is called by the default implementations of the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000529\method{http_error_30*()} methods when a redirection is received from
530the server. If a redirection should take place, return a new
Fred Draked9cf8e72003-07-14 21:07:05 +0000531\class{Request} to allow \method{http_error_30*()} to perform the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000532redirect. Otherwise, raise \exception{HTTPError} if no other handler
533should try to handle this URL, or return \code{None} if you can't but
534another handler might.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000535
Fred Draked9cf8e72003-07-14 21:07:05 +0000536\begin{notice}
537 The default implementation of this method does not strictly
538 follow \rfc{2616}, which says that 301 and 302 responses to \code{POST}
Martin v. Löwis162f0812003-07-12 07:33:32 +0000539 requests must not be automatically redirected without confirmation by
540 the user. In reality, browsers do allow automatic redirection of
Fred Draked9cf8e72003-07-14 21:07:05 +0000541 these responses, changing the POST to a \code{GET}, and the default
542 implementation reproduces this behavior.
543\end{notice}
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000544\end{methoddesc}
545
Moshe Zadka8a18e992001-03-01 08:40:42 +0000546
Fred Drake93c86712001-03-02 20:39:34 +0000547\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req,
548 fp, code, msg, hdrs}
549Redirect to the \code{Location:} URL. This method is called by
550the parent \class{OpenerDirector} when getting an HTTP
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000551`moved permanently' response.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000552\end{methoddesc}
553
Fred Drake93c86712001-03-02 20:39:34 +0000554\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req,
555 fp, code, msg, hdrs}
556The same as \method{http_error_301()}, but called for the
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000557`found' response.
Fred Drake93c86712001-03-02 20:39:34 +0000558\end{methoddesc}
559
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000560\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
561 fp, code, msg, hdrs}
562The same as \method{http_error_301()}, but called for the
Martin v. Löwis162f0812003-07-12 07:33:32 +0000563`see other' response.
Raymond Hettinger024aaa12003-04-24 15:32:12 +0000564\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000565
Martin v. Löwis162f0812003-07-12 07:33:32 +0000566\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
567 fp, code, msg, hdrs}
568The same as \method{http_error_301()}, but called for the
569`temporary redirect' response.
Fred Drake9753ae12003-07-14 20:53:57 +0000570\end{methoddesc}
571
Martin v. Löwis162f0812003-07-12 07:33:32 +0000572
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000573\subsection{HTTPCookieProcessor Objects \label{http-cookie-processor}}
574
Andrew M. Kuchlingd54a0ae2005-12-04 20:25:23 +0000575\versionadded{2.4}
576
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000577\class{HTTPCookieProcessor} instances have one attribute:
578
579\begin{memberdesc}{cookiejar}
580The \class{cookielib.CookieJar} in which cookies are stored.
581\end{memberdesc}
582
583
Fred Drake93c86712001-03-02 20:39:34 +0000584\subsection{ProxyHandler Objects \label{proxy-handler}}
585
Fred Drake47852462001-05-11 15:46:45 +0000586\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}
Fred Drake93c86712001-03-02 20:39:34 +0000587The \class{ProxyHandler} will have a method
588\method{\var{protocol}_open()} for every \var{protocol} which has a
589proxy in the \var{proxies} dictionary given in the constructor. The
590method will modify requests to go through the proxy, by calling
591\code{request.set_proxy()}, and call the next handler in the chain to
592actually execute the protocol.
Fred Drake47852462001-05-11 15:46:45 +0000593\end{methoddescni}
Fred Drake93c86712001-03-02 20:39:34 +0000594
595
596\subsection{HTTPPasswordMgr Objects \label{http-password-mgr}}
597
598These methods are available on \class{HTTPPasswordMgr} and
599\class{HTTPPasswordMgrWithDefaultRealm} objects.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000600
601\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
Fred Drakebb066cf2004-05-12 03:07:27 +0000602\var{uri} can be either a single URI, or a sequence of URIs. \var{realm},
Moshe Zadka8a18e992001-03-01 08:40:42 +0000603\var{user} and \var{passwd} must be strings. This causes
Fred Drake93c86712001-03-02 20:39:34 +0000604\code{(\var{user}, \var{passwd})} to be used as authentication tokens
Moshe Zadka8a18e992001-03-01 08:40:42 +0000605when authentication for \var{realm} and a super-URI of any of the
606given URIs is given.
607\end{methoddesc}
608
609\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
Fred Drake93c86712001-03-02 20:39:34 +0000610Get user/password for given realm and URI, if any. This method will
611return \code{(None, None)} if there is no matching user/password.
612
613For \class{HTTPPasswordMgrWithDefaultRealm} objects, the realm
614\code{None} will be searched if the given \var{realm} has no matching
615user/password.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000616\end{methoddesc}
617
Moshe Zadka8a18e992001-03-01 08:40:42 +0000618
Fred Drake93c86712001-03-02 20:39:34 +0000619\subsection{AbstractBasicAuthHandler Objects
620 \label{abstract-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000621
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622\begin{methoddesc}[AbstractBasicAuthHandler]{http_error_auth_reqed}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000623 {authreq, host, req, headers}
Fred Drake399bc8c2001-11-09 03:49:29 +0000624Handle an authentication request by getting a user/password pair, and
625re-trying the request. \var{authreq} should be the name of the header
626where the information about the realm is included in the request,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627\var{host} specifies the URL and path to authenticate for, \var{req}
628should be the (failed) \class{Request} object, and \var{headers}
629should be the error headers.
630
631\var{host} is either an authority (e.g. \code{"python.org"}) or a URL
632containing an authority component (e.g. \code{"http://python.org/"}).
633In either case, the authority must not contain a userinfo component
634(so, \code{"python.org"} and \code{"python.org:80"} are fine,
635\code{"joe:password@python.org"} is not).
Moshe Zadka8a18e992001-03-01 08:40:42 +0000636\end{methoddesc}
637
Fred Drake93c86712001-03-02 20:39:34 +0000638
639\subsection{HTTPBasicAuthHandler Objects
640 \label{http-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000641
642\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
643 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000644Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000645\end{methoddesc}
646
Fred Drake93c86712001-03-02 20:39:34 +0000647
648\subsection{ProxyBasicAuthHandler Objects
649 \label{proxy-basic-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000650
651\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
652 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000653Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000654\end{methoddesc}
655
Moshe Zadka8a18e992001-03-01 08:40:42 +0000656
Fred Drake93c86712001-03-02 20:39:34 +0000657\subsection{AbstractDigestAuthHandler Objects
658 \label{abstract-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000659
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660\begin{methoddesc}[AbstractDigestAuthHandler]{http_error_auth_reqed}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000661 {authreq, host, req, headers}
662\var{authreq} should be the name of the header where the information about
Fred Drake399bc8c2001-11-09 03:49:29 +0000663the realm is included in the request, \var{host} should be the host to
664authenticate to, \var{req} should be the (failed) \class{Request}
665object, and \var{headers} should be the error headers.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000666\end{methoddesc}
667
Fred Drake93c86712001-03-02 20:39:34 +0000668
669\subsection{HTTPDigestAuthHandler Objects
670 \label{http-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000671
672\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
673 msg, hdrs}
Fred Drake399bc8c2001-11-09 03:49:29 +0000674Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000675\end{methoddesc}
676
Fred Drake93c86712001-03-02 20:39:34 +0000677
678\subsection{ProxyDigestAuthHandler Objects
679 \label{proxy-digest-auth-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000680
681\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
682 msg, hdrs}
Fred Drake93c86712001-03-02 20:39:34 +0000683Retry the request with authentication information, if available.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000684\end{methoddesc}
685
Fred Drake93c86712001-03-02 20:39:34 +0000686
687\subsection{HTTPHandler Objects \label{http-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000688
689\begin{methoddesc}[HTTPHandler]{http_open}{req}
Fred Drake399bc8c2001-11-09 03:49:29 +0000690Send an HTTP request, which can be either GET or POST, depending on
Fred Drake93c86712001-03-02 20:39:34 +0000691\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000692\end{methoddesc}
693
Fred Drake93c86712001-03-02 20:39:34 +0000694
695\subsection{HTTPSHandler Objects \label{https-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000696
697\begin{methoddesc}[HTTPSHandler]{https_open}{req}
Fred Drake93c86712001-03-02 20:39:34 +0000698Send an HTTPS request, which can be either GET or POST, depending on
699\code{\var{req}.has_data()}.
Moshe Zadka8a18e992001-03-01 08:40:42 +0000700\end{methoddesc}
701
Moshe Zadka8a18e992001-03-01 08:40:42 +0000702
Fred Drake93c86712001-03-02 20:39:34 +0000703\subsection{FileHandler Objects \label{file-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000704
705\begin{methoddesc}[FileHandler]{file_open}{req}
706Open the file locally, if there is no host name, or
Fred Drake93c86712001-03-02 20:39:34 +0000707the host name is \code{'localhost'}. Change the
Moshe Zadka8a18e992001-03-01 08:40:42 +0000708protocol to \code{ftp} otherwise, and retry opening
709it using \member{parent}.
710\end{methoddesc}
711
Fred Drake93c86712001-03-02 20:39:34 +0000712
713\subsection{FTPHandler Objects \label{ftp-handler-objects}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000714
715\begin{methoddesc}[FTPHandler]{ftp_open}{req}
716Open the FTP file indicated by \var{req}.
717The login is always done with empty username and password.
718\end{methoddesc}
719
Moshe Zadka8a18e992001-03-01 08:40:42 +0000720
Fred Drake93c86712001-03-02 20:39:34 +0000721\subsection{CacheFTPHandler Objects \label{cacheftp-handler-objects}}
722
723\class{CacheFTPHandler} objects are \class{FTPHandler} objects with
724the following additional methods:
Moshe Zadka8a18e992001-03-01 08:40:42 +0000725
726\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
727Set timeout of connections to \var{t} seconds.
728\end{methoddesc}
729
730\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
731Set maximum number of cached connections to \var{m}.
732\end{methoddesc}
733
Fred Drake93c86712001-03-02 20:39:34 +0000734
735\subsection{GopherHandler Objects \label{gopher-handler}}
Moshe Zadka8a18e992001-03-01 08:40:42 +0000736
737\begin{methoddesc}[GopherHandler]{gopher_open}{req}
738Open the gopher resource indicated by \var{req}.
739\end{methoddesc}
Fred Drake93c86712001-03-02 20:39:34 +0000740
741
742\subsection{UnknownHandler Objects \label{unknown-handler-objects}}
743
Fred Drakea9399112001-07-05 21:14:03 +0000744\begin{methoddesc}[UnknownHandler]{unknown_open}{}
Fred Drake93c86712001-03-02 20:39:34 +0000745Raise a \exception{URLError} exception.
746\end{methoddesc}
Fred Drake53e5b712003-04-25 15:27:33 +0000747
748
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000749\subsection{HTTPErrorProcessor Objects \label{http-error-processor-objects}}
750
Neal Norwitzfb0521f2004-02-28 16:00:23 +0000751\versionadded{2.4}
752
Jeremy Hyltonc1be59f2003-12-14 05:27:34 +0000753\begin{methoddesc}[HTTPErrorProcessor]{unknown_open}{}
754Process HTTP error responses.
755
756For 200 error codes, the response object is returned immediately.
757
758For non-200 error codes, this simply passes the job on to the
759\method{\var{protocol}_error_\var{code}()} handler methods, via
760\method{OpenerDirector.error()}. Eventually,
761\class{urllib2.HTTPDefaultErrorHandler} will raise an
762\exception{HTTPError} if no other handler handles the error.
763\end{methoddesc}
764
765
Fred Drake53e5b712003-04-25 15:27:33 +0000766\subsection{Examples \label{urllib2-examples}}
767
768This example gets the python.org main page and displays the first 100
769bytes of it:
770
771\begin{verbatim}
772>>> import urllib2
773>>> f = urllib2.urlopen('http://www.python.org/')
774>>> print f.read(100)
775<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
776<?xml-stylesheet href="./css/ht2html
777\end{verbatim}
778
779Here we are sending a data-stream to the stdin of a CGI and reading
Georg Brandla2764ad2005-12-26 23:36:32 +0000780the data it returns to us. Note that this example will only work when the
781Python installation supports SSL.
Fred Drake53e5b712003-04-25 15:27:33 +0000782
783\begin{verbatim}
784>>> import urllib2
785>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
786... data='This data is passed to stdin of the CGI')
787>>> f = urllib2.urlopen(req)
788>>> print f.read()
789Got Data: "This data is passed to stdin of the CGI"
790\end{verbatim}
791
792The code for the sample CGI used in the above example is:
793
794\begin{verbatim}
795#!/usr/bin/env python
796import sys
797data = sys.stdin.read()
Raymond Hettinger5de33782004-02-08 20:25:01 +0000798print 'Content-type: text-plain\n\nGot Data: "%s"' % data
Fred Drake53e5b712003-04-25 15:27:33 +0000799\end{verbatim}
Martin v. Löwisbe837372004-08-25 11:24:42 +0000800
801
802Use of Basic HTTP Authentication:
803
804\begin{verbatim}
805import urllib2
806# Create an OpenerDirector with support for Basic HTTP Authentication...
807auth_handler = urllib2.HTTPBasicAuthHandler()
808auth_handler.add_password('realm', 'host', 'username', 'password')
809opener = urllib2.build_opener(auth_handler)
810# ...and install it globally so it can be used with urlopen.
811urllib2.install_opener(opener)
812urllib2.urlopen('http://www.example.com/login.html')
813\end{verbatim}
814
815\function{build_opener()} provides many handlers by default, including a
816\class{ProxyHandler}. By default, \class{ProxyHandler} uses the
817environment variables named \code{<scheme>_proxy}, where \code{<scheme>}
818is the URL scheme involved. For example, the \envvar{http_proxy}
819environment variable is read to obtain the HTTP proxy's URL.
820
821This example replaces the default \class{ProxyHandler} with one that uses
822programatically-supplied proxy URLs, and adds proxy authorization support
823with \class{ProxyBasicAuthHandler}.
824
825\begin{verbatim}
826proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
827proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
828proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
829
830opener = build_opener(proxy_handler, proxy_auth_handler)
831# This time, rather than install the OpenerDirector, we use it directly:
832opener.open('http://www.example.com/login.html')
833\end{verbatim}
834
835
836Adding HTTP headers:
837
838Use the \var{headers} argument to the \class{Request} constructor, or:
839
840\begin{verbatim}
841import urllib2
842req = urllib2.Request('http://www.example.com/')
843req.add_header('Referer', 'http://www.python.org/')
844r = urllib2.urlopen(req)
845\end{verbatim}
846
847\class{OpenerDirector} automatically adds a \mailheader{User-Agent}
848header to every \class{Request}. To change this:
849
850\begin{verbatim}
851import urllib2
852opener = urllib2.build_opener()
853opener.addheaders = [('User-agent', 'Mozilla/5.0')]
854opener.open('http://www.example.com/')
855\end{verbatim}
856
857Also, remember that a few standard headers
858(\mailheader{Content-Length}, \mailheader{Content-Type} and
859\mailheader{Host}) are added when the \class{Request} is passed to
860\function{urlopen()} (or \method{OpenerDirector.open()}).