blob: 6793593f9ec6345e55b0f4c203b0e7731c243a9e [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
Guido van Rossum0af2f631998-07-22 21:34:21 +000021\begin{funcdesc}{urlopen}{url\optional{, data}}
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()},
30\method{info()} and \method{geturl()}.
Guido van Rossum0af2f631998-07-22 21:34:21 +000031
Fred Drake1ec71cb1999-02-22 22:42:14 +000032Except for the \method{info()} and \method{geturl()} methods,
Guido van Rossum0af2f631998-07-22 21:34:21 +000033these methods have the same interface as for
Fred Drake6ef871c1998-03-12 06:52:05 +000034file objects --- see section \ref{bltin-file-objects} in this
35manual. (It is not a built-in file object, however, so it can't be
Guido van Rossum470be141995-03-17 16:07:09 +000036used at those few places where a true built-in file object is
37required.)
Guido van Rossuma8db1df1995-02-16 16:29:46 +000038
Fred Drake6ef871c1998-03-12 06:52:05 +000039The \method{info()} method returns an instance of the class
Guido van Rossum954b9ad1998-09-28 14:08:29 +000040\class{mimetools.Message} containing meta-information associated
41with the URL. When the method is HTTP, these headers are those
42returned by the server at the head of the retrieved HTML page
43(including Content-Length and Content-Type). When the method is FTP,
44a Content-Length header will be present if (as is now usual) the
45server passed back a file length in response to the FTP retrieval
Guido van Rossum88e0b5b2001-08-23 13:38:15 +000046request. A Content-Type header will be present if the MIME type can
47be guessed. When the method is local-file, returned headers will include
Guido van Rossum954b9ad1998-09-28 14:08:29 +000048a Date representing the file's last-modified time, a Content-Length
49giving file size, and a Content-Type containing a guess at the file's
50type. See also the description of the
Fred Drake1ec71cb1999-02-22 22:42:14 +000051\refmodule{mimetools}\refstmodindex{mimetools} module.
52
53The \method{geturl()} method returns the real URL of the page. In
54some cases, the HTTP server redirects a client to another URL. The
55\function{urlopen()} function handles this transparently, but in some
56cases the caller needs to know which URL the client was redirected
57to. The \method{geturl()} method can be used to get at this
58redirected URL.
Guido van Rossum0af2f631998-07-22 21:34:21 +000059
60If the \var{url} uses the \file{http:} scheme identifier, the optional
61\var{data} argument may be given to specify a \code{POST} request
62(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +000063must in standard \mimetype{application/x-www-form-urlencoded} format;
Guido van Rossum0af2f631998-07-22 21:34:21 +000064see the \function{urlencode()} function below.
65
Fred Drakeaef0e892000-08-31 17:23:35 +000066The \function{urlopen()} function works transparently with proxies
Fred Drake81c17352000-09-15 04:12:56 +000067which do not require authentication. In a \UNIX{} or Windows
Fred Drakeaef0e892000-08-31 17:23:35 +000068environment, set the \envvar{http_proxy}, \envvar{ftp_proxy} or
69\envvar{gopher_proxy} environment variables to a URL that identifies
70the proxy server before starting the Python interpreter. For example
71(the \character{\%} is the command prompt):
Fred Drake38e5d272000-04-03 20:13:55 +000072
73\begin{verbatim}
74% http_proxy="http://www.someproxy.com:3128"
75% export http_proxy
76% python
77...
78\end{verbatim}
79
Fred Draked2167032002-04-04 20:09:50 +000080In a Windows environment, if no proxy envvironment variables are set,
81proxy settings are obtained from the registry's Internet Settings
82section.
83
Fred Drake38e5d272000-04-03 20:13:55 +000084In a Macintosh environment, \function{urlopen()} will retrieve proxy
85information from Internet\index{Internet Config} Config.
86
Fred Draked2167032002-04-04 20:09:50 +000087The \function{urlopen()} function does not support explicit proxy
88specification. If you need to override environmental proxy settings,
89use \class{URLopener}, or a subclass such as \class{FancyURLopener}.
90
Fred Drakeaef0e892000-08-31 17:23:35 +000091Proxies which require authentication for use are not currently
92supported; this is considered an implementation limitation.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000093\end{funcdesc}
94
Fred Drake51001332000-12-15 23:57:51 +000095\begin{funcdesc}{urlretrieve}{url\optional{, filename\optional{,
96 reporthook\optional{, data}}}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000097Copy a network object denoted by a URL to a local file, if necessary.
Guido van Rossum6c4f0031995-03-07 10:14:09 +000098If the URL points to a local file, or a valid cached copy of the
Fred Drake6ef871c1998-03-12 06:52:05 +000099object exists, the object is not copied. Return a tuple
100\code{(\var{filename}, \var{headers})} where \var{filename} is the
101local file name under which the object can be found, and \var{headers}
102is either \code{None} (for a local object) or whatever the
103\method{info()} method of the object returned by \function{urlopen()}
104returned (for a remote object, possibly cached). Exceptions are the
105same as for \function{urlopen()}.
Guido van Rossum954b9ad1998-09-28 14:08:29 +0000106
107The second argument, if present, specifies the file location to copy
108to (if absent, the location will be a tempfile with a generated name).
109The third argument, if present, is a hook function that will be called
110once on establishment of the network connection and once after each
111block read thereafter. The hook will be passed three arguments; a
112count of blocks transferred so far, a block size in bytes, and the
Fred Drake09b29571998-10-01 20:43:13 +0000113total size of the file. The third argument may be \code{-1} on older
114FTP servers which do not return a file size in response to a retrieval
Guido van Rossum954b9ad1998-09-28 14:08:29 +0000115request.
Fred Drake9fa4d612000-08-24 01:06:40 +0000116
117If the \var{url} uses the \file{http:} scheme identifier, the optional
118\var{data} argument may be given to specify a \code{POST} request
119(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +0000120must in standard \mimetype{application/x-www-form-urlencoded} format;
Fred Drake9fa4d612000-08-24 01:06:40 +0000121see the \function{urlencode()} function below.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000122\end{funcdesc}
123
124\begin{funcdesc}{urlcleanup}{}
125Clear the cache that may have been built up by previous calls to
Fred Drake6ef871c1998-03-12 06:52:05 +0000126\function{urlretrieve()}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000127\end{funcdesc}
128
Guido van Rossum0af2f631998-07-22 21:34:21 +0000129\begin{funcdesc}{quote}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +0000130Replace special characters in \var{string} using the \samp{\%xx} escape.
131Letters, digits, and the characters \character{_,.-} are never quoted.
Guido van Rossum0af2f631998-07-22 21:34:21 +0000132The optional \var{safe} parameter specifies additional characters
Guido van Rossum61d34f41995-02-27 17:51:51 +0000133that should not be quoted --- its default value is \code{'/'}.
134
Fred Drake10853c92000-07-28 13:51:27 +0000135Example: \code{quote('/\~{}connolly/')} yields \code{'/\%7econnolly/'}.
Guido van Rossum8d40c841996-12-13 14:48:47 +0000136\end{funcdesc}
137
Guido van Rossum0af2f631998-07-22 21:34:21 +0000138\begin{funcdesc}{quote_plus}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +0000139Like \function{quote()}, but also replaces spaces by plus signs, as
Guido van Rossum0af2f631998-07-22 21:34:21 +0000140required for quoting HTML form values. Plus signs in the original
141string are escaped unless they are included in \var{safe}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000142\end{funcdesc}
143
144\begin{funcdesc}{unquote}{string}
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000145Replace \samp{\%xx} escapes by their single-character equivalent.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000146
Fred Drake10853c92000-07-28 13:51:27 +0000147Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~{}connolly/'}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000148\end{funcdesc}
149
Guido van Rossum8d40c841996-12-13 14:48:47 +0000150\begin{funcdesc}{unquote_plus}{string}
Fred Drake6ef871c1998-03-12 06:52:05 +0000151Like \function{unquote()}, but also replaces plus signs by spaces, as
Guido van Rossum8d40c841996-12-13 14:48:47 +0000152required for unquoting HTML form values.
153\end{funcdesc}
154
Skip Montanaro4fda21b2001-01-28 21:18:16 +0000155\begin{funcdesc}{urlencode}{query\optional{, doseq}}
156Convert a mapping object or a sequence of two-element tuples to a
157``url-encoded'' string, suitable to pass to
Guido van Rossum0af2f631998-07-22 21:34:21 +0000158\function{urlopen()} above as the optional \var{data} argument. This
159is useful to pass a dictionary of form fields to a \code{POST}
Fred Drake09b29571998-10-01 20:43:13 +0000160request. The resulting string is a series of
161\code{\var{key}=\var{value}} pairs separated by \character{\&}
162characters, where both \var{key} and \var{value} are quoted using
Skip Montanaroeda28442001-01-24 06:36:06 +0000163\function{quote_plus()} above. If the optional parameter \var{doseq} is
164present and evaluates to true, individual \code{\var{key}=\var{value}} pairs
165are generated for each element of the sequence.
Skip Montanaro4fda21b2001-01-28 21:18:16 +0000166When a sequence of two-element tuples is used as the \var{query} argument,
167the first element of each tuple is a key and the second is a value. The
168order of parameters in the encoded string will match the order of parameter
169tuples in the sequence.
Guido van Rossum0af2f631998-07-22 21:34:21 +0000170\end{funcdesc}
171
Fred Drakedfca4dc2000-08-25 05:13:42 +0000172The public functions \function{urlopen()} and
173\function{urlretrieve()} create an instance of the
174\class{FancyURLopener} class and use it to perform their requested
175actions. To override this functionality, programmers can create a
176subclass of \class{URLopener} or \class{FancyURLopener}, then assign
177that an instance of that class to the
178\code{urllib._urlopener} variable before calling the desired function.
179For example, applications may want to specify a different
Fred Draked86038d2001-08-03 18:39:36 +0000180\mailheader{User-Agent} header than \class{URLopener} defines. This
181can be accomplished with the following code:
Fred Drake38e5d272000-04-03 20:13:55 +0000182
183\begin{verbatim}
184class AppURLopener(urllib.FancyURLopener):
185 def __init__(self, *args):
Fred Drake38e5d272000-04-03 20:13:55 +0000186 self.version = "App/1.7"
Fred Drake63bc2e02001-07-23 19:16:22 +0000187 urllib.FancyURLopener.__init__(self, *args)
Fred Drake38e5d272000-04-03 20:13:55 +0000188
Fred Drake6c160192000-05-30 14:39:45 +0000189urllib._urlopener = AppURLopener()
Fred Drake38e5d272000-04-03 20:13:55 +0000190\end{verbatim}
191
192\begin{classdesc}{URLopener}{\optional{proxies\optional{, **x509}}}
193Base class for opening and reading URLs. Unless you need to support
194opening objects using schemes other than \file{http:}, \file{ftp:},
195\file{gopher:} or \file{file:}, you probably want to use
196\class{FancyURLopener}.
197
198By default, the \class{URLopener} class sends a
Fred Draked86038d2001-08-03 18:39:36 +0000199\mailheader{User-Agent} header of \samp{urllib/\var{VVV}}, where
Fred Drake38e5d272000-04-03 20:13:55 +0000200\var{VVV} is the \module{urllib} version number. Applications can
Fred Draked86038d2001-08-03 18:39:36 +0000201define their own \mailheader{User-Agent} header by subclassing
Fred Drake38e5d272000-04-03 20:13:55 +0000202\class{URLopener} or \class{FancyURLopener} and setting the instance
Fred Drakedfca4dc2000-08-25 05:13:42 +0000203attribute \member{version} to an appropriate string value before the
Fred Drake38e5d272000-04-03 20:13:55 +0000204\method{open()} method is called.
205
Fred Draked2167032002-04-04 20:09:50 +0000206The optional \var{proxies} parameter should be a dictionary mapping
207scheme names to proxy URLs, where an empty dictionary turns proxies
208off completely. Its default value is None, in which case
209environmental proxy settings will be used if present, as discussed in
210the definition of \function{urlopen()}, above.
211
Fred Drake38e5d272000-04-03 20:13:55 +0000212Additional keyword parameters, collected in \var{x509}, are used for
213authentication with the \file{https:} scheme. The keywords
214\var{key_file} and \var{cert_file} are supported; both are needed to
215actually retrieve a resource at an \file{https:} URL.
216\end{classdesc}
217
218\begin{classdesc}{FancyURLopener}{...}
219\class{FancyURLopener} subclasses \class{URLopener} providing default
220handling for the following HTTP response codes: 301, 302 or 401. For
Fred Draked86038d2001-08-03 18:39:36 +0000221301 and 302 response codes, the \mailheader{Location} header is used to
Fred Drake38e5d272000-04-03 20:13:55 +0000222fetch the actual URL. For 401 response codes (authentication
Skip Montanaro04f1d372001-02-15 17:00:40 +0000223required), basic HTTP authentication is performed. For 301 and 302 response
224codes, recursion is bounded by the value of the \var{maxtries} attribute,
225which defaults 10.
Fred Drake38e5d272000-04-03 20:13:55 +0000226
227The parameters to the constructor are the same as those for
228\class{URLopener}.
Fred Drake47f11ce2001-04-12 20:26:49 +0000229
Fred Drake0aa811c2001-10-20 04:24:09 +0000230\note{When performing basic authentication, a
Fred Drake47f11ce2001-04-12 20:26:49 +0000231\class{FancyURLopener} instance calls its
232\method{prompt_user_passwd()} method. The default implementation asks
233the users for the required information on the controlling terminal. A
234subclass may override this method to support more appropriate behavior
Fred Drake0aa811c2001-10-20 04:24:09 +0000235if needed.}
Fred Drake38e5d272000-04-03 20:13:55 +0000236\end{classdesc}
237
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000238Restrictions:
239
240\begin{itemize}
241
242\item
243Currently, only the following protocols are supported: HTTP, (versions
2440.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
Fred Drake6ef871c1998-03-12 06:52:05 +0000245\indexii{HTTP}{protocol}
246\indexii{Gopher}{protocol}
247\indexii{FTP}{protocol}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000248
249\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000250The caching feature of \function{urlretrieve()} has been disabled
251until I find the time to hack proper processing of Expiration time
252headers.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000253
254\item
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000255There should be a function to query whether a particular URL is in
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000256the cache.
257
258\item
259For backward compatibility, if a URL appears to point to a local file
260but the file can't be opened, the URL is re-interpreted using the FTP
261protocol. This can sometimes cause confusing error messages.
262
263\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000264The \function{urlopen()} and \function{urlretrieve()} functions can
265cause arbitrarily long delays while waiting for a network connection
266to be set up. This means that it is difficult to build an interactive
Fred Drake8ee679f2001-07-14 02:50:55 +0000267Web client using these functions without using threads.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000268
269\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000270The data returned by \function{urlopen()} or \function{urlretrieve()}
271is the raw data returned by the server. This may be binary data
Fred Drake1ec71cb1999-02-22 22:42:14 +0000272(e.g. an image), plain text or (for example) HTML\index{HTML}. The
273HTTP\indexii{HTTP}{protocol} protocol provides type information in the
274reply header, which can be inspected by looking at the
Fred Draked86038d2001-08-03 18:39:36 +0000275\mailheader{Content-Type} header. For the
276Gopher\indexii{Gopher}{protocol} protocol, type information is encoded
277in the URL; there is currently no easy way to extract it. If the
278returned data is HTML, you can use the module
279\refmodule{htmllib}\refstmodindex{htmllib} to parse it.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000280
281\item
Fred Drake81c17352000-09-15 04:12:56 +0000282This module does not support the use of proxies which require
283authentication. This may be implemented in the future.
284
285\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000286Although the \module{urllib} module contains (undocumented) routines
287to parse and unparse URL strings, the recommended interface for URL
Fred Drake1ec71cb1999-02-22 22:42:14 +0000288manipulation is in module \refmodule{urlparse}\refstmodindex{urlparse}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000289
290\end{itemize}
Fred Drake38e5d272000-04-03 20:13:55 +0000291
292
293\subsection{URLopener Objects \label{urlopener-objs}}
294\sectionauthor{Skip Montanaro}{skip@mojam.com}
295
296\class{URLopener} and \class{FancyURLopener} objects have the
Fred Drakedfca4dc2000-08-25 05:13:42 +0000297following attributes.
Fred Drake38e5d272000-04-03 20:13:55 +0000298
Fred Drakedfca4dc2000-08-25 05:13:42 +0000299\begin{methoddesc}[URLopener]{open}{fullurl\optional{, data}}
Fred Drake38e5d272000-04-03 20:13:55 +0000300Open \var{fullurl} using the appropriate protocol. This method sets
301up cache and proxy information, then calls the appropriate open method with
302its input arguments. If the scheme is not recognized,
303\method{open_unknown()} is called. The \var{data} argument
304has the same meaning as the \var{data} argument of \function{urlopen()}.
305\end{methoddesc}
306
Fred Drakedfca4dc2000-08-25 05:13:42 +0000307\begin{methoddesc}[URLopener]{open_unknown}{fullurl\optional{, data}}
Fred Drake38e5d272000-04-03 20:13:55 +0000308Overridable interface to open unknown URL types.
309\end{methoddesc}
310
Fred Drakedfca4dc2000-08-25 05:13:42 +0000311\begin{methoddesc}[URLopener]{retrieve}{url\optional{,
312 filename\optional{,
313 reporthook\optional{, data}}}}
Fred Drake38e5d272000-04-03 20:13:55 +0000314Retrieves the contents of \var{url} and places it in \var{filename}. The
315return value is a tuple consisting of a local filename and either a
316\class{mimetools.Message} object containing the response headers (for remote
317URLs) or None (for local URLs). The caller must then open and read the
318contents of \var{filename}. If \var{filename} is not given and the URL
319refers to a local file, the input filename is returned. If the URL is
320non-local and \var{filename} is not given, the filename is the output of
321\function{tempfile.mktemp()} with a suffix that matches the suffix of the last
322path component of the input URL. If \var{reporthook} is given, it must be
323a function accepting three numeric parameters. It will be called after each
324chunk of data is read from the network. \var{reporthook} is ignored for
325local URLs.
Fred Drake9fa4d612000-08-24 01:06:40 +0000326
327If the \var{url} uses the \file{http:} scheme identifier, the optional
328\var{data} argument may be given to specify a \code{POST} request
329(normally the request type is \code{GET}). The \var{data} argument
Fred Drake51001332000-12-15 23:57:51 +0000330must in standard \mimetype{application/x-www-form-urlencoded} format;
Fred Drake9fa4d612000-08-24 01:06:40 +0000331see the \function{urlencode()} function below.
Fred Drake38e5d272000-04-03 20:13:55 +0000332\end{methoddesc}
333
Fred Drakedfca4dc2000-08-25 05:13:42 +0000334\begin{memberdesc}[URLopener]{version}
335Variable that specifies the user agent of the opener object. To get
336\refmodule{urllib} to tell servers that it is a particular user agent,
337set this in a subclass as a class variable or in the constructor
338before calling the base constructor.
339\end{memberdesc}
340
Fred Drake47f11ce2001-04-12 20:26:49 +0000341The \class{FancyURLopener} class offers one additional method that
342should be overloaded to provide the appropriate behavior:
343
344\begin{methoddesc}[FancyURLopener]{prompt_user_passwd}{host, realm}
345Return information needed to authenticate the user at the given host
346in the specified security realm. The return value should be a tuple,
347\code{(\var{user}, \var{password})}, which can be used for basic
348authentication.
349
350The implementation prompts for this information on the terminal; an
351application should override this method to use an appropriate
352interaction model in the local environment.
353\end{methoddesc}
354
Fred Drake38e5d272000-04-03 20:13:55 +0000355
356\subsection{Examples}
357\nodename{Urllib Examples}
358
359Here is an example session that uses the \samp{GET} method to retrieve
360a URL containing parameters:
361
362\begin{verbatim}
363>>> import urllib
364>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
365>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
366>>> print f.read()
367\end{verbatim}
368
369The following example uses the \samp{POST} method instead:
370
371\begin{verbatim}
372>>> import urllib
373>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
374>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
375>>> print f.read()
376\end{verbatim}
Fred Draked2167032002-04-04 20:09:50 +0000377
378The following example uses an explicitly specified HTTP proxy,
379overriding environment settings:
380
381\begin{verbatim}
382>>> import urllib
383>>> proxies = {'http': 'http://proxy.example.com:8080/'}
384>>> opener = urllib.FancyURLopener(proxies)
385>>> f = opener.open("http://www.python.org")
386>>> f.read()
387\end{verbatim}
388
389The following example uses no proxies at all, overriding environment
390settings:
391
392\begin{verbatim}
393>>> import urllib
394>>> opener = urllib.FancyURLopener({})
395>>> f = opener.open("http://www.python.org/")
396>>> f.read()
397\end{verbatim}