blob: 20e2796afc91681855ff3680faf02a78170c46c9 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{urllib} ---
Fred Drakedfca4dc2000-08-25 05:13:42 +00002 Open arbitrary resources by URL}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake38e5d272000-04-03 20:13:55 +00004\declaremodule{standard}{urllib}
5\modulesynopsis{Open an arbitrary network resource by URL (requires sockets).}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossuma8db1df1995-02-16 16:29:46 +00007\index{WWW}
Fred Drake8ee679f2001-07-14 02:50:55 +00008\index{World Wide Web}
Guido van Rossum61d34f41995-02-27 17:51:51 +00009\index{URL}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000010
Guido van Rossum86751151995-02-28 17:14:32 +000011
Guido van Rossuma8db1df1995-02-16 16:29:46 +000012This module provides a high-level interface for fetching data across
Fred Drake8ee679f2001-07-14 02:50:55 +000013the World Wide Web. In particular, the \function{urlopen()} function
Fred Drake6ef871c1998-03-12 06:52:05 +000014is similar to the built-in function \function{open()}, but accepts
15Universal Resource Locators (URLs) instead of filenames. Some
16restrictions apply --- it can only open URLs for reading, and no seek
17operations are available.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000018
Fred Drakef5eaa2e1997-12-15 22:13:50 +000019It defines the following public functions:
Guido van Rossuma8db1df1995-02-16 16:29:46 +000020
Fred Drake5ca3a082002-04-04 20:34:36 +000021\begin{funcdesc}{urlopen}{url\optional{, data\optional{, proxies}}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000022Open a network object denoted by a URL for reading. If the URL does
Fred Drake6ef871c1998-03-12 06:52:05 +000023not have a scheme identifier, or if it has \file{file:} as its scheme
Guido van Rossuma8db1df1995-02-16 16:29:46 +000024identifier, this opens a local file; otherwise it opens a socket to a
25server somewhere on the network. If the connection cannot be made, or
Fred Drake6ef871c1998-03-12 06:52:05 +000026if the server returns an error code, the \exception{IOError} exception
27is raised. If all went well, a file-like object is returned. This
28supports the following methods: \method{read()}, \method{readline()},
Fred Drake1ec71cb1999-02-22 22:42:14 +000029\method{readlines()}, \method{fileno()}, \method{close()},
Raymond Hettinger42182eb2003-03-09 05:33:33 +000030\method{info()} and \method{geturl()}. It also has proper support for
31the iterator protocol.
Guido van Rossum0af2f631998-07-22 21:34:21 +000032
Fred Drake1ec71cb1999-02-22 22:42:14 +000033Except for the \method{info()} and \method{geturl()} methods,
Guido van Rossum0af2f631998-07-22 21:34:21 +000034these methods have the same interface as for
Fred Drake6ef871c1998-03-12 06:52:05 +000035file objects --- see section \ref{bltin-file-objects} in this
36manual. (It is not a built-in file object, however, so it can't be
Guido van Rossum470be141995-03-17 16:07:09 +000037used at those few places where a true built-in file object is
38required.)
Guido van Rossuma8db1df1995-02-16 16:29:46 +000039
Fred Drake6ef871c1998-03-12 06:52:05 +000040The \method{info()} method returns an instance of the class
Guido van Rossum954b9ad1998-09-28 14:08:29 +000041\class{mimetools.Message} containing meta-information associated
42with the URL. When the method is HTTP, these headers are those
43returned by the server at the head of the retrieved HTML page
44(including Content-Length and Content-Type). When the method is FTP,
45a Content-Length header will be present if (as is now usual) the
46server passed back a file length in response to the FTP retrieval
Guido van Rossum88e0b5b2001-08-23 13:38:15 +000047request. A Content-Type header will be present if the MIME type can
48be guessed. When the method is local-file, returned headers will include
Guido van Rossum954b9ad1998-09-28 14:08:29 +000049a Date representing the file's last-modified time, a Content-Length
50giving file size, and a Content-Type containing a guess at the file's
51type. See also the description of the
Fred Drake1ec71cb1999-02-22 22:42:14 +000052\refmodule{mimetools}\refstmodindex{mimetools} module.
53
54The \method{geturl()} method returns the real URL of the page. In
55some cases, the HTTP server redirects a client to another URL. The
56\function{urlopen()} function handles this transparently, but in some
57cases the caller needs to know which URL the client was redirected
58to. The \method{geturl()} method can be used to get at this
59redirected URL.
Guido van Rossum0af2f631998-07-22 21:34:21 +000060
61If the \var{url} uses the \file{http:} scheme identifier, the optional
62\var{data} argument may be given to specify a \code{POST} request
63(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +000064must in standard \mimetype{application/x-www-form-urlencoded} format;
Guido van Rossum0af2f631998-07-22 21:34:21 +000065see the \function{urlencode()} function below.
66
Fred Drakeaef0e892000-08-31 17:23:35 +000067The \function{urlopen()} function works transparently with proxies
Fred Drake81c17352000-09-15 04:12:56 +000068which do not require authentication. In a \UNIX{} or Windows
Fred Drakeaef0e892000-08-31 17:23:35 +000069environment, set the \envvar{http_proxy}, \envvar{ftp_proxy} or
70\envvar{gopher_proxy} environment variables to a URL that identifies
71the proxy server before starting the Python interpreter. For example
72(the \character{\%} is the command prompt):
Fred Drake38e5d272000-04-03 20:13:55 +000073
74\begin{verbatim}
75% http_proxy="http://www.someproxy.com:3128"
76% export http_proxy
77% python
78...
79\end{verbatim}
80
Fred Draked2167032002-04-04 20:09:50 +000081In a Windows environment, if no proxy envvironment variables are set,
82proxy settings are obtained from the registry's Internet Settings
83section.
84
Fred Drake38e5d272000-04-03 20:13:55 +000085In a Macintosh environment, \function{urlopen()} will retrieve proxy
86information from Internet\index{Internet Config} Config.
87
Fred Drake5ca3a082002-04-04 20:34:36 +000088Alternatively, the optional \var{proxies} argument may be used to
89explicitly specify proxies. It must be a dictionary mapping scheme
90names to proxy URLs, where an empty dictionary causes no proxies to be
91used, and \code{None} (the default value) causes environmental proxy
92settings to be used as discussed above. For example:
93
94\begin{verbatim}
95# Use http://www.someproxy.com:3128 for http proxying
96proxies = proxies={'http': 'http://www.someproxy.com:3128'}
97filehandle = urllib.urlopen(some_url, proxies=proxies)
98# Don't use any proxies
99filehandle = urllib.urlopen(some_url, proxies={})
100# Use proxies from environment - both versions are equivalent
101filehandle = urllib.urlopen(some_url, proxies=None)
102filehandle = urllib.urlopen(some_url)
103\end{verbatim}
104
Fred Draked2167032002-04-04 20:09:50 +0000105The \function{urlopen()} function does not support explicit proxy
106specification. If you need to override environmental proxy settings,
107use \class{URLopener}, or a subclass such as \class{FancyURLopener}.
108
Fred Drakeaef0e892000-08-31 17:23:35 +0000109Proxies which require authentication for use are not currently
110supported; this is considered an implementation limitation.
Fred Drakea2c25952002-04-04 20:58:02 +0000111
112\versionchanged[Added the \var{proxies} support]{2.3}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000113\end{funcdesc}
114
Fred Drake51001332000-12-15 23:57:51 +0000115\begin{funcdesc}{urlretrieve}{url\optional{, filename\optional{,
116 reporthook\optional{, data}}}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000117Copy a network object denoted by a URL to a local file, if necessary.
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000118If the URL points to a local file, or a valid cached copy of the
Fred Drake6ef871c1998-03-12 06:52:05 +0000119object exists, the object is not copied. Return a tuple
120\code{(\var{filename}, \var{headers})} where \var{filename} is the
121local file name under which the object can be found, and \var{headers}
122is either \code{None} (for a local object) or whatever the
123\method{info()} method of the object returned by \function{urlopen()}
124returned (for a remote object, possibly cached). Exceptions are the
125same as for \function{urlopen()}.
Guido van Rossum954b9ad1998-09-28 14:08:29 +0000126
127The second argument, if present, specifies the file location to copy
128to (if absent, the location will be a tempfile with a generated name).
129The third argument, if present, is a hook function that will be called
130once on establishment of the network connection and once after each
131block read thereafter. The hook will be passed three arguments; a
132count of blocks transferred so far, a block size in bytes, and the
Fred Drake09b29571998-10-01 20:43:13 +0000133total size of the file. The third argument may be \code{-1} on older
134FTP servers which do not return a file size in response to a retrieval
Guido van Rossum954b9ad1998-09-28 14:08:29 +0000135request.
Fred Drake9fa4d612000-08-24 01:06:40 +0000136
137If the \var{url} uses the \file{http:} scheme identifier, the optional
138\var{data} argument may be given to specify a \code{POST} request
139(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +0000140must in standard \mimetype{application/x-www-form-urlencoded} format;
Fred Drake9fa4d612000-08-24 01:06:40 +0000141see the \function{urlencode()} function below.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000142\end{funcdesc}
143
Fred Drake88f015d2002-10-22 21:58:06 +0000144\begin{datadesc}{_urlopener}
145The public functions \function{urlopen()} and
146\function{urlretrieve()} create an instance of the
147\class{FancyURLopener} class and use it to perform their requested
148actions. To override this functionality, programmers can create a
149subclass of \class{URLopener} or \class{FancyURLopener}, then assign
150that an instance of that class to the
151\code{urllib._urlopener} variable before calling the desired function.
152For example, applications may want to specify a different
153\mailheader{User-Agent} header than \class{URLopener} defines. This
154can be accomplished with the following code:
155
156\begin{verbatim}
157import urllib
158
159class AppURLopener(urllib.FancyURLopener):
160 def __init__(self, *args):
161 self.version = "App/1.7"
162 urllib.FancyURLopener.__init__(self, *args)
163
164urllib._urlopener = AppURLopener()
165\end{verbatim}
166\end{datadesc}
167
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000168\begin{funcdesc}{urlcleanup}{}
169Clear the cache that may have been built up by previous calls to
Fred Drake6ef871c1998-03-12 06:52:05 +0000170\function{urlretrieve()}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000171\end{funcdesc}
172
Guido van Rossum0af2f631998-07-22 21:34:21 +0000173\begin{funcdesc}{quote}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +0000174Replace special characters in \var{string} using the \samp{\%xx} escape.
175Letters, digits, and the characters \character{_,.-} are never quoted.
Guido van Rossum0af2f631998-07-22 21:34:21 +0000176The optional \var{safe} parameter specifies additional characters
Guido van Rossum61d34f41995-02-27 17:51:51 +0000177that should not be quoted --- its default value is \code{'/'}.
178
Fred Drake10853c92000-07-28 13:51:27 +0000179Example: \code{quote('/\~{}connolly/')} yields \code{'/\%7econnolly/'}.
Guido van Rossum8d40c841996-12-13 14:48:47 +0000180\end{funcdesc}
181
Guido van Rossum0af2f631998-07-22 21:34:21 +0000182\begin{funcdesc}{quote_plus}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +0000183Like \function{quote()}, but also replaces spaces by plus signs, as
Guido van Rossum0af2f631998-07-22 21:34:21 +0000184required for quoting HTML form values. Plus signs in the original
185string are escaped unless they are included in \var{safe}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000186\end{funcdesc}
187
188\begin{funcdesc}{unquote}{string}
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000189Replace \samp{\%xx} escapes by their single-character equivalent.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000190
Fred Drake10853c92000-07-28 13:51:27 +0000191Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~{}connolly/'}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000192\end{funcdesc}
193
Guido van Rossum8d40c841996-12-13 14:48:47 +0000194\begin{funcdesc}{unquote_plus}{string}
Fred Drake6ef871c1998-03-12 06:52:05 +0000195Like \function{unquote()}, but also replaces plus signs by spaces, as
Guido van Rossum8d40c841996-12-13 14:48:47 +0000196required for unquoting HTML form values.
197\end{funcdesc}
198
Skip Montanaro4fda21b2001-01-28 21:18:16 +0000199\begin{funcdesc}{urlencode}{query\optional{, doseq}}
200Convert a mapping object or a sequence of two-element tuples to a
201``url-encoded'' string, suitable to pass to
Guido van Rossum0af2f631998-07-22 21:34:21 +0000202\function{urlopen()} above as the optional \var{data} argument. This
203is useful to pass a dictionary of form fields to a \code{POST}
Fred Drake09b29571998-10-01 20:43:13 +0000204request. The resulting string is a series of
205\code{\var{key}=\var{value}} pairs separated by \character{\&}
206characters, where both \var{key} and \var{value} are quoted using
Skip Montanaroeda28442001-01-24 06:36:06 +0000207\function{quote_plus()} above. If the optional parameter \var{doseq} is
208present and evaluates to true, individual \code{\var{key}=\var{value}} pairs
209are generated for each element of the sequence.
Skip Montanaro4fda21b2001-01-28 21:18:16 +0000210When a sequence of two-element tuples is used as the \var{query} argument,
211the first element of each tuple is a key and the second is a value. The
212order of parameters in the encoded string will match the order of parameter
213tuples in the sequence.
Guido van Rossum0af2f631998-07-22 21:34:21 +0000214\end{funcdesc}
215
Fred Drake88f015d2002-10-22 21:58:06 +0000216\begin{funcdesc}{pathname2url}{path}
217Convert the pathname \var{path} from the local syntax for a path to
218the form used in the path component of a URL. This does not produce a
219complete URL. The return value will already be quoted using the
220\function{quote()} function.
221\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000222
Fred Drake88f015d2002-10-22 21:58:06 +0000223\begin{funcdesc}{url2pathname}{path}
224Convert the path component \var{path} from an encoded URL to the local
225syntax for a path. This does not accept a complete URL. This
226function uses \function{unquote()} to decode \var{path}.
227\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000228
229\begin{classdesc}{URLopener}{\optional{proxies\optional{, **x509}}}
230Base class for opening and reading URLs. Unless you need to support
231opening objects using schemes other than \file{http:}, \file{ftp:},
232\file{gopher:} or \file{file:}, you probably want to use
233\class{FancyURLopener}.
234
235By default, the \class{URLopener} class sends a
Fred Draked86038d2001-08-03 18:39:36 +0000236\mailheader{User-Agent} header of \samp{urllib/\var{VVV}}, where
Fred Drake38e5d272000-04-03 20:13:55 +0000237\var{VVV} is the \module{urllib} version number. Applications can
Fred Draked86038d2001-08-03 18:39:36 +0000238define their own \mailheader{User-Agent} header by subclassing
Fred Drake38e5d272000-04-03 20:13:55 +0000239\class{URLopener} or \class{FancyURLopener} and setting the instance
Fred Drakedfca4dc2000-08-25 05:13:42 +0000240attribute \member{version} to an appropriate string value before the
Fred Drake38e5d272000-04-03 20:13:55 +0000241\method{open()} method is called.
242
Fred Draked2167032002-04-04 20:09:50 +0000243The optional \var{proxies} parameter should be a dictionary mapping
244scheme names to proxy URLs, where an empty dictionary turns proxies
Fred Drake5ca3a082002-04-04 20:34:36 +0000245off completely. Its default value is \code{None}, in which case
Fred Draked2167032002-04-04 20:09:50 +0000246environmental proxy settings will be used if present, as discussed in
247the definition of \function{urlopen()}, above.
248
Fred Drake38e5d272000-04-03 20:13:55 +0000249Additional keyword parameters, collected in \var{x509}, are used for
250authentication with the \file{https:} scheme. The keywords
251\var{key_file} and \var{cert_file} are supported; both are needed to
252actually retrieve a resource at an \file{https:} URL.
253\end{classdesc}
254
255\begin{classdesc}{FancyURLopener}{...}
256\class{FancyURLopener} subclasses \class{URLopener} providing default
257handling for the following HTTP response codes: 301, 302 or 401. For
Fred Draked86038d2001-08-03 18:39:36 +0000258301 and 302 response codes, the \mailheader{Location} header is used to
Fred Drake38e5d272000-04-03 20:13:55 +0000259fetch the actual URL. For 401 response codes (authentication
Skip Montanaro04f1d372001-02-15 17:00:40 +0000260required), basic HTTP authentication is performed. For 301 and 302 response
261codes, recursion is bounded by the value of the \var{maxtries} attribute,
262which defaults 10.
Fred Drake38e5d272000-04-03 20:13:55 +0000263
264The parameters to the constructor are the same as those for
265\class{URLopener}.
Fred Drake47f11ce2001-04-12 20:26:49 +0000266
Fred Drake0aa811c2001-10-20 04:24:09 +0000267\note{When performing basic authentication, a
Fred Drake47f11ce2001-04-12 20:26:49 +0000268\class{FancyURLopener} instance calls its
269\method{prompt_user_passwd()} method. The default implementation asks
270the users for the required information on the controlling terminal. A
271subclass may override this method to support more appropriate behavior
Fred Drake0aa811c2001-10-20 04:24:09 +0000272if needed.}
Fred Drake38e5d272000-04-03 20:13:55 +0000273\end{classdesc}
274
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000275Restrictions:
276
277\begin{itemize}
278
279\item
280Currently, only the following protocols are supported: HTTP, (versions
2810.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
Fred Drake6ef871c1998-03-12 06:52:05 +0000282\indexii{HTTP}{protocol}
283\indexii{Gopher}{protocol}
284\indexii{FTP}{protocol}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000285
286\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000287The caching feature of \function{urlretrieve()} has been disabled
288until I find the time to hack proper processing of Expiration time
289headers.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000290
291\item
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000292There should be a function to query whether a particular URL is in
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000293the cache.
294
295\item
296For backward compatibility, if a URL appears to point to a local file
297but the file can't be opened, the URL is re-interpreted using the FTP
298protocol. This can sometimes cause confusing error messages.
299
300\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000301The \function{urlopen()} and \function{urlretrieve()} functions can
302cause arbitrarily long delays while waiting for a network connection
303to be set up. This means that it is difficult to build an interactive
Fred Drake8ee679f2001-07-14 02:50:55 +0000304Web client using these functions without using threads.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000305
306\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000307The data returned by \function{urlopen()} or \function{urlretrieve()}
308is the raw data returned by the server. This may be binary data
Fred Drake1ec71cb1999-02-22 22:42:14 +0000309(e.g. an image), plain text or (for example) HTML\index{HTML}. The
310HTTP\indexii{HTTP}{protocol} protocol provides type information in the
311reply header, which can be inspected by looking at the
Fred Draked86038d2001-08-03 18:39:36 +0000312\mailheader{Content-Type} header. For the
313Gopher\indexii{Gopher}{protocol} protocol, type information is encoded
314in the URL; there is currently no easy way to extract it. If the
315returned data is HTML, you can use the module
316\refmodule{htmllib}\refstmodindex{htmllib} to parse it.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000317
318\item
Fred Drake81c17352000-09-15 04:12:56 +0000319This module does not support the use of proxies which require
320authentication. This may be implemented in the future.
321
322\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000323Although the \module{urllib} module contains (undocumented) routines
324to parse and unparse URL strings, the recommended interface for URL
Fred Drake1ec71cb1999-02-22 22:42:14 +0000325manipulation is in module \refmodule{urlparse}\refstmodindex{urlparse}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000326
327\end{itemize}
Fred Drake38e5d272000-04-03 20:13:55 +0000328
329
330\subsection{URLopener Objects \label{urlopener-objs}}
331\sectionauthor{Skip Montanaro}{skip@mojam.com}
332
333\class{URLopener} and \class{FancyURLopener} objects have the
Fred Drakedfca4dc2000-08-25 05:13:42 +0000334following attributes.
Fred Drake38e5d272000-04-03 20:13:55 +0000335
Fred Drakedfca4dc2000-08-25 05:13:42 +0000336\begin{methoddesc}[URLopener]{open}{fullurl\optional{, data}}
Fred Drake38e5d272000-04-03 20:13:55 +0000337Open \var{fullurl} using the appropriate protocol. This method sets
338up cache and proxy information, then calls the appropriate open method with
339its input arguments. If the scheme is not recognized,
340\method{open_unknown()} is called. The \var{data} argument
341has the same meaning as the \var{data} argument of \function{urlopen()}.
342\end{methoddesc}
343
Fred Drakedfca4dc2000-08-25 05:13:42 +0000344\begin{methoddesc}[URLopener]{open_unknown}{fullurl\optional{, data}}
Fred Drake38e5d272000-04-03 20:13:55 +0000345Overridable interface to open unknown URL types.
346\end{methoddesc}
347
Fred Drakedfca4dc2000-08-25 05:13:42 +0000348\begin{methoddesc}[URLopener]{retrieve}{url\optional{,
349 filename\optional{,
350 reporthook\optional{, data}}}}
Fred Drake38e5d272000-04-03 20:13:55 +0000351Retrieves the contents of \var{url} and places it in \var{filename}. The
352return value is a tuple consisting of a local filename and either a
353\class{mimetools.Message} object containing the response headers (for remote
Fred Drake5ca3a082002-04-04 20:34:36 +0000354URLs) or \code{None} (for local URLs). The caller must then open and read the
Fred Drake38e5d272000-04-03 20:13:55 +0000355contents of \var{filename}. If \var{filename} is not given and the URL
356refers to a local file, the input filename is returned. If the URL is
357non-local and \var{filename} is not given, the filename is the output of
358\function{tempfile.mktemp()} with a suffix that matches the suffix of the last
359path component of the input URL. If \var{reporthook} is given, it must be
360a function accepting three numeric parameters. It will be called after each
361chunk of data is read from the network. \var{reporthook} is ignored for
362local URLs.
Fred Drake9fa4d612000-08-24 01:06:40 +0000363
364If the \var{url} uses the \file{http:} scheme identifier, the optional
365\var{data} argument may be given to specify a \code{POST} request
366(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +0000367must in standard \mimetype{application/x-www-form-urlencoded} format;
Fred Drake9fa4d612000-08-24 01:06:40 +0000368see the \function{urlencode()} function below.
Fred Drake38e5d272000-04-03 20:13:55 +0000369\end{methoddesc}
370
Fred Drakedfca4dc2000-08-25 05:13:42 +0000371\begin{memberdesc}[URLopener]{version}
372Variable that specifies the user agent of the opener object. To get
373\refmodule{urllib} to tell servers that it is a particular user agent,
374set this in a subclass as a class variable or in the constructor
375before calling the base constructor.
376\end{memberdesc}
377
Fred Drake47f11ce2001-04-12 20:26:49 +0000378The \class{FancyURLopener} class offers one additional method that
379should be overloaded to provide the appropriate behavior:
380
381\begin{methoddesc}[FancyURLopener]{prompt_user_passwd}{host, realm}
382Return information needed to authenticate the user at the given host
383in the specified security realm. The return value should be a tuple,
384\code{(\var{user}, \var{password})}, which can be used for basic
385authentication.
386
387The implementation prompts for this information on the terminal; an
388application should override this method to use an appropriate
389interaction model in the local environment.
390\end{methoddesc}
391
Fred Drake38e5d272000-04-03 20:13:55 +0000392
393\subsection{Examples}
394\nodename{Urllib Examples}
395
396Here is an example session that uses the \samp{GET} method to retrieve
397a URL containing parameters:
398
399\begin{verbatim}
400>>> import urllib
401>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
402>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
403>>> print f.read()
404\end{verbatim}
405
406The following example uses the \samp{POST} method instead:
407
408\begin{verbatim}
409>>> import urllib
410>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
411>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
412>>> print f.read()
413\end{verbatim}
Fred Draked2167032002-04-04 20:09:50 +0000414
415The following example uses an explicitly specified HTTP proxy,
416overriding environment settings:
417
418\begin{verbatim}
419>>> import urllib
420>>> proxies = {'http': 'http://proxy.example.com:8080/'}
421>>> opener = urllib.FancyURLopener(proxies)
422>>> f = opener.open("http://www.python.org")
423>>> f.read()
424\end{verbatim}
425
426The following example uses no proxies at all, overriding environment
427settings:
428
429\begin{verbatim}
430>>> import urllib
431>>> opener = urllib.FancyURLopener({})
432>>> f = opener.open("http://www.python.org/")
433>>> f.read()
434\end{verbatim}