blob: 90243a5a238d4ae9383e128492734ad748dcde1e [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}
5
6\moduleauthor{Jeremy Hylton}{jhylton@users.sourceforge.net}
7\sectionauthor{Moshe Zadka}{moshez@users.sourceforge.net}
8
9\modulesynopsis{An extensible library for opening URLs using a variety of
10 protocols}
11
12The \module{urllib2} module defines functions and classes which help
13in opening URLs (mostly HTTP) in a complex world -- basic and digest
14authentication, redirections and more.
15
16The \module{urllib2} module defines the following functions:
17
18\begin{funcdesc}{urlopen}{url\optional{, data}}
19Open the url \var{url}, which can either a string or a \class{Request}
20object (currently the code checks that it really is a \class{Request}
21instance, or an instance of a subclass of \class{Request}.
22
23\var{data} should be a string, which specifies additional data to
24send to the server. In HTTP requests, which are the only ones that
25support \var{data}, it should be a buffer in the format of
26\code{application/x-www-form-urlencoded}, for example one returned
27from \function{urllib.urlencode}.
28
29This function returns a file-like object with two additional methods:
30
31\begin{itemize}
32
33 \item \code{geturl()} --- return the URL of the resource retrieved
34 \item \code{info()} --- return the meta-information of the page, as
35 a dictionary-like object
36\end{itemize}
37
38Raises \exception{URLError} on errors.
39\end{funcdesc}
40
41\begin{funcdesc}{install_opener}{opener}
42Install a \class{OpenerDirector} instance as the default opener.
43The code does not check for a real \class{OpenerDirector}, and any
44class with the appropriate interface will work.
45\end{funcdesc}
46
47\begin{funcdesc}{build_opener}{\optional{handler\optional{,
48 handler\optional{, ...}}}}
49Return an \class{OpenerDirector} instance, which chains the
50handlers in the order given. \var{handler}s can be either instances
51of \class{BaseHandler}, or subclasses of \class{BaseHandler} (in
52which case it must be possible to call the constructor without
53any parameters. Instances of the following classes will be in
54the front of the \var{handler}s, unless the \var{handler}s contain
55them, instances of them or subclasses of them:
56
57\code{ProxyHandler, UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler,
58 HTTPRedirectHandler, FTPHandler, FileHandler}
59
60If the Python installation has SSL support (\code{socket.ssl} exists),
61\class{HTTPSHandler} will also be added.
62\end{funcdesc}
63
64\begin{excdesc}{URLError}
65The error handlers raise when they run into a problem. It is a subclass
66of \exception{IOError}.
67\end{excdesc}
68
69\begin{excdesc}{HTTPError}
70A subclass of \exception{URLError}, it can also function as a
71non-exceptional file-like return value (the same thing that \function{urlopen}
72returns). This is useful when handling exotic HTTP errors, such as
73requests for authentications.
74\end{excdesc}
75
76\begin{excdesc}{GopherError}
77A subclass of \exception{URLError}, this is the error raised by the
78Gopher handler.
79\end{excdesc}
80
81\begin{classdesc}{Request}{url\optional{data, \optional{, headers}}}
82This class is an abstraction of a URL request.
83
84\var{url} should be a string which is a valid URL. For descrtion
85of \var{data} see the \method{add_data} description.
86\var{headers} should be a dictionary, and will be treated as if
87\method{add_header} was called with each key and value as arguments.
88\end{classdesc}
89
90The following methods describe all of \class{Request}'s public interface,
91and so all must be overridden in subclasses.
92
93\begin{methoddesc}[Request]{add_data}{data}
94Set the \class{Request} data to \var{data} is ignored
95by all handlers except HTTP handlers --- and there it should be an
96\code{application/x-www-form-encoded} buffer, and will change the
97request to be \code{POST} rather then \code{GET}.
98\end{methoddesc}
99
100\begin{methoddesc}[Request]{has_data}{data}
101Return whether the instance has a non-\code{None} data.
102\end{methoddesc}
103
104\begin{methoddesc}[Request]{get_data}{data}
105Return the instance's data.
106\end{methoddesc}
107
108\begin{methoddesc}[Request]{add_header}{key, val}
109Add another header to the request. Headers
110are currently ignored by all handlers except HTTP handlers, where they
111are added to the list of headers sent to the server. Note that there
112cannot be more then one header with the same name, and later calls
113will overwrite previous calls in case the \var{key} collides. Currently,
114this is no loss of HTTP functionality, since all headers which have meaning
115when used more then once have a (header-specific) way of gaining the
116same functionality using only one header.
117\end{methoddesc}
118
119\begin{methoddesc}[Request]{get_full_url}{}
120Return the URL given in the constructor.
121\end{methoddesc}
122
123\begin{methoddesc}[Request]{get_type}{}
124Return the type of the URL --- also known as the schema.
125\end{methoddesc}
126
127\begin{methoddesc}[Request]{get_host}{}
128Return the host to which connection will be made.
129\end{methoddesc}
130
131\begin{methoddesc}[Request]{get_selector}{}
132Return the selector --- the part of the URL that is sent to
133the server.
134\end{methoddesc}
135
136\begin{methoddesc}[Request]{set_proxy}{host, type}
137Make the request by connecting to a proxy server. The \var{host} and \var{type}
138will replace those of the instance, and the instance's selector will be
139the original URL given in the constructor.
140\end{methoddesc}
141
142\begin{classdesc}{OpenerDirector}{}
143The \class{OpenerDirector} class opens URLs via \class{BaseHandler}s chained
144together. It manages the chaining of handlers, and recovery from errors.
145\end{classdesc}
146
147\begin{methoddesc}[OpenerDirector]{add_handler}{handler}
148\var{handler} should be an instance of \class{BaseHandler}. The following
149methods are searched, and added to the possible chains.
150
151\begin{itemize}
152 \item \code{{\em protocol}_open} --- signal that the handler knows how
153 to open {\em protocol} URLs.
154 \item \code{{\em protocol}_error_{\em type}} -- signal that the handler
155 knows how to handle {\em type}
156 errors from {\em protocol}.
157\end{itemize}
158
159\end{methoddesc}
160
161\begin{methoddesc}[OpenerDirector]{close}{}
162Explicitly break cycles, and delete all the handlers.
163Because the \class{OpenerDirector} needs to know the registered handlers,
164and a handler needs to know who the \class{OpenerDirector} who called
165it is, there is a reference cycles. Even though recent versions of Python
166have cycle-collection, it is sometimes preferable to explicitly break
167the cycles.
168\end{methoddesc}
169
170\begin{methoddesc}[OpenerDirector]{open}{url\optional{, data}}
171Open the given \var{url}. (which can be a request object or a string),
172optionally passing the given \var{data}.
173Arguments, return values and exceptions raised are the same as those
174of \function{urlopen} (which simply calls the \method{open()} method
175on the default installed \class{OpenerDirector}.
176\end{methoddesc}
177
178\begin{methoddesc}[OpenerDirector]{error}{proto\optional{, arg\optional{, ...}}}
179Handle an error in a given protocol. The HTTP protocol is special cased to
180use the code as the error. This will call the registered error handlers
181for the given protocol with the given arguments (which are protocol specific).
182
183Return values and exceptions raised are the same as those
184of \function{urlopen}.
185\end{methoddesc}
186
187\begin{classdesc}{BaseHandler}{}
188This is the base class for all registered handlers --- and handles only
189the simple mechanics of registration.
190\end{classdesc}
191
192\begin{methoddesc}[BaseHandler]{add_parent}{director}
193Add a director as parent.
194\end{methoddesc}
195
196\begin{methoddesc}[BaseHandler]{close}{}
197Remove any parents.
198\end{methoddesc}
199
200The following members and methods should be used only be classes derived
201from \class{BaseHandler}:
202
203\begin{memberdesc}[BaseHandler]{parent}
204A valid \class{OpenerDirector}, which can be used to open using a different
205protocol, or handle errors.
206\end{memberdesc}
207
208\begin{methoddesc}[BaseHandler]{default_open}{req}
209This method is {\em not} defined in \class{BaseHandler}, but subclasses
210should define it if they want to catch all URLs.
211
212This method, if exists, will be called by the \member{parent}
213\class{OpenerDirector}. It should return a file-like object as described
214in the return value of the \method{open} of \class{OpenerDirector} or
215\code{None}. It should raise \exception{URLError}, unless a truly exceptional
216thing happens (for example, \exception{MemoryError} should not be mapped
217to \exception{URLError}.
218
219This method will be called before any protocol-specific open method.
220\end{methoddesc}
221
222\begin{methoddesc}[BaseHandler]{{\em protocol}_open}{req}
223This method is {\em not} defined in \class{BaseHandler}, but subclasses
224should define it if they want to handle URLs with the given protocol.
225
226This method, if exists, will be called by the \member{parent}
227\class{OpenerDirector}. Return values should be the same as for
228\method{default_open}.
229\end{methoddesc}
230
231\begin{methoddesc}[BaseHandler]{unknown_open}{req}
232This method is {\em not} defined in \class{BaseHandler}, but subclasses
233should define it if they want to catch all URLs with no specific
234registerd handler to open it.
235
236This method, if exists, will be called by the \member{parent}
237\class{OpenerDirector}. Return values should be the same as for
238\method{default_open}.
239\end{methoddesc}
240
241\begin{methoddesc}[BaseHandler]{http_error_default}{req, fp, code, msg, hdrs}
242This method is {\em not} defined in \class{BaseHandler}, but subclasses
243should override it if they intend to provide a catch-all for otherwise
244unhandled HTTP errors. It will be called automatically by the
245\class{OpenerDirector} getting the error, and should not normally be called
246in other circumstances.
247
248\var{req} will be a \class{Request} object, \var{fp} will be a file-like
249object with the HTTP error body, \var{code} will be the three-digit code
250of the error, \var{msg} will be the user-visible explanation of the
251code and \var{hdrs} will be a dictionary-like object with the headers of
252the error.
253
254Return values and exceptions raised should be the same as those
255of \function{urlopen}.
256\end{methoddesc}
257
258\begin{methoddesc}[BaseHandler]{http_error_{\em nnn}}{req, fp, code, msg, hdrs}
259\code{nnn} should be a three-digit HTTP error code. This method is also
260not defined in \class{BaseHandler}, but will be called, if it exists, on
261an instance of a subclass, when an HTTP error with code \code{nnn} occurse.
262
263Subclasses should override this method to handle specific HTTP errors.
264
265Arguments, return values and exceptions raised shoudl be the same as for
266\method{http_error_default}
267\end{methoddesc}
268
269
270\begin{classdesc}{HTTPDefaultErrorHandler}{}
271A class which catches all HTTP errors.
272\end{classdesc}
273
274\begin{methoddesc}[HTTPDefaultErrorHandler]{http_error_default}{req, fp, code,
275 msg, hdrs}
276Raise an \exception{HTTPError}
277\end{methoddesc}
278
279\begin{classdesc}{HTTPRedirectHandler}{}
280A class to handle redirections.
281\end{classdesc}
282
283\begin{methoddesc}[HTTPRedirectHandler]{http_error_301}{req, fp, code,
284 msg, hdrs}
285Redirect to the \code{Location:} URL. This method gets called by
286the parent \class{OpenerDirector} when getting an HTTP permanent-redirect
287error.
288\end{methoddesc}
289
290\begin{methoddesc}[HTTPRedirectHandler]{http_error_302}{req, fp, code,
291 msg, hdrs}
292The same as \method{http_error_301}.
293\end{methoddesc}
294
295\strong{Note:} 303 redirection is not supported by this version of
296\module{urllib2}.
297
298\begin{classdesc}{ProxyHandler}{\optional{proxies}}
299Cause requests to go through a proxy.
300If \var{proxies} is given, it must be a dictionary mapping
301protocol names to URLs of proxies.
302The default is to read the list of proxies from the environment
303variables \code{{\em protocol}_proxy}.
304\end{classdesc}
305
306\begin{methoddesc}[ProxyHandler]{{\em protocol}_open}{request}
307The \class{ProxyHandler} will have a method \code{{\em protocol}_open} for
308every {\em protocol} which has a proxy in the \var{proxies} dictionary
309given in the constructor. The method will modify requests to go
310through the proxy, by calling \code{request.set_proxy()}, and call the next
311handler in the chain to actually execute the protocol.
312\end{methoddesc}
313
314\begin{classdesc}{HTTPPasswordMgr}{}
315Keep a database of
316\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mapping.
317\end{classdesc}
318
319\begin{methoddesc}[HTTPPasswordMgr]{add_password}{realm, uri, user, passwd}
320\var{uri} can be either a single URI, or a sequene of URIs. \var{realm},
321\var{user} and \var{passwd} must be strings. This causes
322 \code{(\var{user}, \var{passwd})} to be used as authentication tokens
323when authentication for \var{realm} and a super-URI of any of the
324given URIs is given.
325\end{methoddesc}
326
327\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
328Get user/password for given realm and URI, if any. This method will
329return \code{(None, None)} if there is no user/password is known.
330\end{methoddesc}
331
332\begin{classdesc}{HTTPPasswordMgrWithDefaultRealm}{}
333Keep a database of
334\code{(\var{realm}, \var{uri}) -> (\var{user}, \var{password})} mapping.
335A realm of \code{None} is considered a catch-all realm, which is searched
336if no other realm fits.
337\end{classdesc}
338
339\begin{methoddesc}[HTTPPasswordMgrWithDefaultRealm]{add_password}
340 {realm, uri, user, passwd}
341\var{uri} can be either a single URI, or a sequene of URIs. \var{realm},
342\var{user} and \var{passwd} must be strings. This causes
343 \code{(\var{user}, \var{passwd})} to be used as authentication tokens
344when authentication for \var{realm} and a super-URI of any of the
345given URIs is given.
346\end{methoddesc}
347
348\begin{methoddesc}[HTTPPasswordMgr]{find_user_password}{realm, authuri}
349Get user/password for given realm and URI, if any. This method will
350return \code{(None, None)} if there is no user/password is known.
351If the given \var{realm} has no user/password, the realm \code{None}
352will be searched.
353\end{methoddesc}
354
355\begin{classdesc}[AbstractBasicAuthHandler]{\optional{password_mgr}}
356This is a mixin class, that helps with HTTP authentication, both
357to the remote host and to a proxy.
358
359\var{password_mgr} should be something that is compatible with
360\class{HTTPPasswordMgr} --- supplies the documented interface above.
361\end{classdesc}
362
363\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
364 {authreq, host, req, headers}
365Handle an authentication request by getting user/password pair, and retrying.
366\var{authreq} should be the name of the header where the information about
367the realm, \var{host} is the host to authenticate too, \var{req} should be the
368(failed) \class{Request} object, and \var{headers} should be the error headers.
369\end{methoddesc}
370
371\begin{classdesc}{HTTPBasicAuthHandler}{\optional{password_mgr}}
372Handle authentication with the remote host.
373Valid \var{password_mgr}, if given, are the same as for
374\class{AbstractBasicAuthHandler}.
375\end{classdesc}
376
377\begin{methoddesc}[HTTPBasicAuthHandler]{http_error_401}{req, fp, code,
378 msg, hdrs}
379Retry the request with authentication info, if available.
380\end{methoddesc}
381
382\begin{classdesc}{ProxyBasicAuthHandler}{\optional{password_mgr}}
383Handle authentication with the proxy.
384Valid \var{password_mgr}, if given, are the same as for
385\class{AbstractBasicAuthHandler}.
386\end{classdesc}
387
388\begin{methoddesc}[ProxyBasicAuthHandler]{http_error_407}{req, fp, code,
389 msg, hdrs}
390Retry the request with authentication info, if available.
391\end{methoddesc}
392
393\begin{classdesc}{AbstractDigestAuthHandler}{\optional{password_mgr}}
394This is a mixin class, that helps with HTTP authentication, both
395to the remote host and to a proxy.
396
397\var{password_mgr} should be something that is compatible with
398\class{HTTPPasswordMgr} --- supplies the documented interface above.
399\end{classdesc}
400
401\begin{methoddesc}[AbstractBasicAuthHandler]{handle_authentication_request}
402 {authreq, host, req, headers}
403\var{authreq} should be the name of the header where the information about
404the realm, \var{host} should be the host to authenticate too, \var{req}
405should be the (failed) \class{Request} object, and \var{headers} should be the
406error headers.
407\end{methoddesc}
408
409\begin{classdesc}{HTTPDigestAuthHandler}{\optional{password_mgr}}
410Handle authentication with the remote host.
411Valid \var{password_mgr}, if given, are the same as for
412\class{AbstractBasicAuthHandler}.
413\end{classdesc}
414
415\begin{methoddesc}[HTTPDigestAuthHandler]{http_error_401}{req, fp, code,
416 msg, hdrs}
417Retry the request with authentication info, if available.
418\end{methoddesc}
419
420\begin{classdesc}{ProxyDigestAuthHandler}{\optional{password_mgr}}
421Handle authentication with the proxy.
422\var{password_mgr}, if given, shoudl be the same as for
423the constructor of \class{AbstractDigestAuthHandler}.
424\end{classdesc}
425
426\begin{methoddesc}[ProxyDigestAuthHandler]{http_error_407}{req, fp, code,
427 msg, hdrs}
428Retry the request with authentication info, if available.
429\end{methoddesc}
430
431\begin{classdesc}{HTTPHandler}{}
432A class to handle opening of HTTP URLs
433\end{classdesc}
434
435\begin{methoddesc}[HTTPHandler]{http_open}{req}
436Send an HTTP request (either GET or POST, depending on whether
437\code{req.has_data()}.
438\end{methoddesc}
439
440\begin{classdesc}{HTTPSHandler}{}
441A class to handle opening of HTTPS URLs
442\end{classdesc}
443
444\begin{methoddesc}[HTTPSHandler]{https_open}{req}
445Send an HTTPS request (either GET or POST, depending on whether
446\code{req.has_data()}.
447\end{methoddesc}
448
449\begin{classdesc}{UknownHandler}{}
450A catch-all class to handle unknown URLs.
451\end{classdesc}
452
453\begin{methoddesc}[UknownHandler]{unknown_open}
454Raise a \exception{URLError} exception
455\end{methoddesc}
456
457\begin{classdesc}{FileHandler}{}
458Open local files.
459\end{classdesc}
460
461\begin{methoddesc}[FileHandler]{file_open}{req}
462Open the file locally, if there is no host name, or
463the host name is \code{"localhost"}. Change the
464protocol to \code{ftp} otherwise, and retry opening
465it using \member{parent}.
466\end{methoddesc}
467
468\begin{classdesc}{FTPHandler}{}
469Open FTP URLs.
470\end{classdesc}
471
472\begin{methoddesc}[FTPHandler]{ftp_open}{req}
473Open the FTP file indicated by \var{req}.
474The login is always done with empty username and password.
475\end{methoddesc}
476
477\begin{classdesc}{CacheFTPHandler}{}
478Open FTP URLs, keeping a cache of open FTP connections to minimize
479delays.
480\end{classdesc}
481
482\begin{methoddesc}[CacheFTPHandler]{ftp_open}{req}
483Open the FTP file indicated by \var{req}.
484The login is always done with empty username and password.
485\end{methoddesc}
486
487\begin{methoddesc}[CacheFTPHandler]{setTimeout}{t}
488Set timeout of connections to \var{t} seconds.
489\end{methoddesc}
490
491\begin{methoddesc}[CacheFTPHandler]{setMaxConns}{m}
492Set maximum number of cached connections to \var{m}.
493\end{methoddesc}
494
495\begin{classdesc}{GopherHandler}{}
496Open gopher URLs.
497\end{classdesc}
498
499\begin{methoddesc}[GopherHandler]{gopher_open}{req}
500Open the gopher resource indicated by \var{req}.
501\end{methoddesc}