blob: b2a817db469f89ce757c0e20f9add4ab0ee83d42 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`urllib` --- Open arbitrary resources by URL
2=================================================
3
4.. module:: urllib
5 :synopsis: Open an arbitrary network resource by URL (requires sockets).
6
Brett Cannon8bb8fa52008-07-02 01:57:08 +00007.. note::
8 The :mod:`urllib` module has been split into parts and renamed in
Ezio Melotti510ff542012-05-03 19:21:40 +03009 Python 3 to :mod:`urllib.request`, :mod:`urllib.parse`,
Brett Cannon8bb8fa52008-07-02 01:57:08 +000010 and :mod:`urllib.error`. The :term:`2to3` tool will automatically adapt
Ezio Melotti510ff542012-05-03 19:21:40 +030011 imports when converting your sources to Python 3.
Ezio Melotti9c96f0b2014-02-10 09:59:04 +020012 Also note that the :func:`urllib.request.urlopen` function in Python 3 is
13 equivalent to :func:`urllib2.urlopen` and that :func:`urllib.urlopen` has
14 been removed.
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
16.. index::
17 single: WWW
18 single: World Wide Web
19 single: URL
20
21This module provides a high-level interface for fetching data across the World
22Wide Web. In particular, the :func:`urlopen` function is similar to the
23built-in function :func:`open`, but accepts Universal Resource Locators (URLs)
24instead of filenames. Some restrictions apply --- it can only open URLs for
25reading, and no seek operations are available.
26
Sandro Tosi71a5ea02011-08-12 19:11:24 +020027.. warning:: When opening HTTPS URLs, it does not attempt to validate the
Antoine Pitrou66bfda82010-09-29 11:30:52 +000028 server certificate. Use at your own risk!
29
30
Georg Brandl62647652008-01-07 18:23:27 +000031High-level interface
32--------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
Benjamin Petersonb2064732014-11-23 20:55:24 -060034.. function:: urlopen(url[, data[, proxies[, context]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
R David Murrayc7b8f802012-08-15 11:22:58 -040036 Open a network object denoted by a URL for reading. If the URL does not
37 have a scheme identifier, or if it has :file:`file:` as its scheme
38 identifier, this opens a local file (without :term:`universal newlines`);
39 otherwise it opens a socket to a server somewhere on the network. If the
40 connection cannot be made the :exc:`IOError` exception is raised. If all
41 went well, a file-like object is returned. This supports the following
42 methods: :meth:`read`, :meth:`readline`, :meth:`readlines`, :meth:`fileno`,
43 :meth:`close`, :meth:`info`, :meth:`getcode` and :meth:`geturl`. It also
44 has proper support for the :term:`iterator` protocol. One caveat: the
45 :meth:`read` method, if the size argument is omitted or negative, may not
46 read until the end of the data stream; there is no good way to determine
Georg Brandl8ec7f652007-08-15 14:28:01 +000047 that the entire stream from a socket has been read in the general case.
48
Georg Brandl9b0d46d2008-01-20 11:43:03 +000049 Except for the :meth:`info`, :meth:`getcode` and :meth:`geturl` methods,
50 these methods have the same interface as for file objects --- see section
51 :ref:`bltin-file-objects` in this manual. (It is not a built-in file object,
52 however, so it can't be used at those few places where a true built-in file
53 object is required.)
Georg Brandl8ec7f652007-08-15 14:28:01 +000054
55 .. index:: module: mimetools
56
57 The :meth:`info` method returns an instance of the class
Senthil Kumaran1c919a62010-06-29 13:28:20 +000058 :class:`mimetools.Message` containing meta-information associated with the
Georg Brandl8ec7f652007-08-15 14:28:01 +000059 URL. When the method is HTTP, these headers are those returned by the server
60 at the head of the retrieved HTML page (including Content-Length and
61 Content-Type). When the method is FTP, a Content-Length header will be
62 present if (as is now usual) the server passed back a file length in response
63 to the FTP retrieval request. A Content-Type header will be present if the
64 MIME type can be guessed. When the method is local-file, returned headers
65 will include a Date representing the file's last-modified time, a
66 Content-Length giving file size, and a Content-Type containing a guess at the
67 file's type. See also the description of the :mod:`mimetools` module.
68
69 The :meth:`geturl` method returns the real URL of the page. In some cases, the
70 HTTP server redirects a client to another URL. The :func:`urlopen` function
71 handles this transparently, but in some cases the caller needs to know which URL
72 the client was redirected to. The :meth:`geturl` method can be used to get at
73 this redirected URL.
74
Georg Brandl9b0d46d2008-01-20 11:43:03 +000075 The :meth:`getcode` method returns the HTTP status code that was sent with the
76 response, or ``None`` if the URL is no HTTP URL.
77
Georg Brandl8ec7f652007-08-15 14:28:01 +000078 If the *url* uses the :file:`http:` scheme identifier, the optional *data*
79 argument may be given to specify a ``POST`` request (normally the request type
80 is ``GET``). The *data* argument must be in standard
81 :mimetype:`application/x-www-form-urlencoded` format; see the :func:`urlencode`
82 function below.
83
84 The :func:`urlopen` function works transparently with proxies which do not
85 require authentication. In a Unix or Windows environment, set the
86 :envvar:`http_proxy`, or :envvar:`ftp_proxy` environment variables to a URL that
87 identifies the proxy server before starting the Python interpreter. For example
88 (the ``'%'`` is the command prompt)::
89
90 % http_proxy="http://www.someproxy.com:3128"
91 % export http_proxy
92 % python
93 ...
94
Georg Brandl22350112008-01-20 12:05:43 +000095 The :envvar:`no_proxy` environment variable can be used to specify hosts which
96 shouldn't be reached via proxy; if set, it should be a comma-separated list
97 of hostname suffixes, optionally with ``:port`` appended, for example
98 ``cern.ch,ncsa.uiuc.edu,some.host:8080``.
99
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100 In a Windows environment, if no proxy environment variables are set, proxy
101 settings are obtained from the registry's Internet Settings section.
102
103 .. index:: single: Internet Config
104
Senthil Kumaran45a505f2009-10-18 01:24:41 +0000105 In a Mac OS X environment, :func:`urlopen` will retrieve proxy information
106 from the OS X System Configuration Framework, which can be managed with
107 Network System Preferences panel.
108
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
110 Alternatively, the optional *proxies* argument may be used to explicitly specify
111 proxies. It must be a dictionary mapping scheme names to proxy URLs, where an
112 empty dictionary causes no proxies to be used, and ``None`` (the default value)
113 causes environmental proxy settings to be used as discussed above. For
114 example::
115
116 # Use http://www.someproxy.com:3128 for http proxying
117 proxies = {'http': 'http://www.someproxy.com:3128'}
118 filehandle = urllib.urlopen(some_url, proxies=proxies)
119 # Don't use any proxies
120 filehandle = urllib.urlopen(some_url, proxies={})
121 # Use proxies from environment - both versions are equivalent
122 filehandle = urllib.urlopen(some_url, proxies=None)
123 filehandle = urllib.urlopen(some_url)
124
Benjamin Petersonb2064732014-11-23 20:55:24 -0600125 Proxies which require authentication for use are not currently supported;
126 this is considered an implementation limitation.
127
128 The *context* parameter may be set to a :class:`ssl.SSLContext` instance to
129 configure the SSL settings that are used if :func:`urlopen` makes a HTTPS
130 connection.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
132 .. versionchanged:: 2.3
133 Added the *proxies* support.
134
Georg Brandl22350112008-01-20 12:05:43 +0000135 .. versionchanged:: 2.6
136 Added :meth:`getcode` to returned object and support for the
137 :envvar:`no_proxy` environment variable.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000138
Benjamin Petersonb2064732014-11-23 20:55:24 -0600139 .. versionchanged:: 2.7.9
140 The *context* parameter was added.
141
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000142 .. deprecated:: 2.6
Ezio Melotti510ff542012-05-03 19:21:40 +0300143 The :func:`urlopen` function has been removed in Python 3 in favor
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000144 of :func:`urllib2.urlopen`.
Georg Brandl22350112008-01-20 12:05:43 +0000145
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
147.. function:: urlretrieve(url[, filename[, reporthook[, data]]])
148
149 Copy a network object denoted by a URL to a local file, if necessary. If the URL
150 points to a local file, or a valid cached copy of the object exists, the object
151 is not copied. Return a tuple ``(filename, headers)`` where *filename* is the
152 local file name under which the object can be found, and *headers* is whatever
153 the :meth:`info` method of the object returned by :func:`urlopen` returned (for
154 a remote object, possibly cached). Exceptions are the same as for
155 :func:`urlopen`.
156
157 The second argument, if present, specifies the file location to copy to (if
158 absent, the location will be a tempfile with a generated name). The third
159 argument, if present, is a hook function that will be called once on
160 establishment of the network connection and once after each block read
161 thereafter. The hook will be passed three arguments; a count of blocks
162 transferred so far, a block size in bytes, and the total size of the file. The
163 third argument may be ``-1`` on older FTP servers which do not return a file
164 size in response to a retrieval request.
165
166 If the *url* uses the :file:`http:` scheme identifier, the optional *data*
167 argument may be given to specify a ``POST`` request (normally the request type
168 is ``GET``). The *data* argument must in standard
169 :mimetype:`application/x-www-form-urlencoded` format; see the :func:`urlencode`
170 function below.
171
172 .. versionchanged:: 2.5
173 :func:`urlretrieve` will raise :exc:`ContentTooShortError` when it detects that
174 the amount of data available was less than the expected amount (which is the
175 size reported by a *Content-Length* header). This can occur, for example, when
176 the download is interrupted.
177
178 The *Content-Length* is treated as a lower bound: if there's more data to read,
Eli Benderskyad72bb12011-04-16 15:28:42 +0300179 :func:`urlretrieve` reads more data, but if less data is available, it raises
180 the exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181
182 You can still retrieve the downloaded data in this case, it is stored in the
183 :attr:`content` attribute of the exception instance.
184
Eli Benderskyad72bb12011-04-16 15:28:42 +0300185 If no *Content-Length* header was supplied, :func:`urlretrieve` can not check
186 the size of the data it has downloaded, and just returns it. In this case you
187 just have to assume that the download was successful.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188
189
190.. data:: _urlopener
191
192 The public functions :func:`urlopen` and :func:`urlretrieve` create an instance
193 of the :class:`FancyURLopener` class and use it to perform their requested
194 actions. To override this functionality, programmers can create a subclass of
195 :class:`URLopener` or :class:`FancyURLopener`, then assign an instance of that
196 class to the ``urllib._urlopener`` variable before calling the desired function.
197 For example, applications may want to specify a different
198 :mailheader:`User-Agent` header than :class:`URLopener` defines. This can be
199 accomplished with the following code::
200
201 import urllib
202
203 class AppURLopener(urllib.FancyURLopener):
204 version = "App/1.7"
205
206 urllib._urlopener = AppURLopener()
207
208
209.. function:: urlcleanup()
210
211 Clear the cache that may have been built up by previous calls to
212 :func:`urlretrieve`.
213
214
Georg Brandl62647652008-01-07 18:23:27 +0000215Utility functions
216-----------------
217
Senthil Kumaran880685f2010-07-22 01:47:30 +0000218.. function:: quote(string[, safe])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000219
220 Replace special characters in *string* using the ``%xx`` escape. Letters,
Senthil Kumaran90161372009-08-31 16:40:27 +0000221 digits, and the characters ``'_.-'`` are never quoted. By default, this
R David Murray1d336512011-06-22 20:00:27 -0400222 function is intended for quoting the path section of the URL. The optional
Senthil Kumaran90161372009-08-31 16:40:27 +0000223 *safe* parameter specifies additional characters that should not be quoted
224 --- its default value is ``'/'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225
226 Example: ``quote('/~connolly/')`` yields ``'/%7econnolly/'``.
227
228
Senthil Kumaran880685f2010-07-22 01:47:30 +0000229.. function:: quote_plus(string[, safe])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000230
231 Like :func:`quote`, but also replaces spaces by plus signs, as required for
Georg Brandl8d31f542009-07-28 18:55:32 +0000232 quoting HTML form values when building up a query string to go into a URL.
233 Plus signs in the original string are escaped unless they are included in
234 *safe*. It also does not have *safe* default to ``'/'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000235
236
237.. function:: unquote(string)
238
239 Replace ``%xx`` escapes by their single-character equivalent.
240
241 Example: ``unquote('/%7Econnolly/')`` yields ``'/~connolly/'``.
242
243
244.. function:: unquote_plus(string)
245
246 Like :func:`unquote`, but also replaces plus signs by spaces, as required for
247 unquoting HTML form values.
248
249
250.. function:: urlencode(query[, doseq])
251
Benjamin Peterson53e812a2010-06-06 00:50:58 +0000252 Convert a mapping object or a sequence of two-element tuples to a
Senthil Kumaranbd13f452010-08-09 20:14:11 +0000253 "percent-encoded" string, suitable to pass to :func:`urlopen` above as the
Senthil Kumaran98bc31f2010-06-02 02:19:15 +0000254 optional *data* argument. This is useful to pass a dictionary of form
255 fields to a ``POST`` request. The resulting string is a series of
256 ``key=value`` pairs separated by ``'&'`` characters, where both *key* and
257 *value* are quoted using :func:`quote_plus` above. When a sequence of
258 two-element tuples is used as the *query* argument, the first element of
259 each tuple is a key and the second is a value. The value element in itself
260 can be a sequence and in that case, if the optional parameter *doseq* is
Benjamin Peterson11591c32010-06-06 00:54:29 +0000261 evaluates to *True*, individual ``key=value`` pairs separated by ``'&'`` are
Senthil Kumaran98bc31f2010-06-02 02:19:15 +0000262 generated for each element of the value sequence for the key. The order of
263 parameters in the encoded string will match the order of parameter tuples in
264 the sequence. The :mod:`urlparse` module provides the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +0000265 :func:`parse_qs` and :func:`parse_qsl` which are used to parse query strings
266 into Python data structures.
267
268
269.. function:: pathname2url(path)
270
271 Convert the pathname *path* from the local syntax for a path to the form used in
272 the path component of a URL. This does not produce a complete URL. The return
273 value will already be quoted using the :func:`quote` function.
274
275
276.. function:: url2pathname(path)
277
Senthil Kumaranbd13f452010-08-09 20:14:11 +0000278 Convert the path component *path* from an percent-encoded URL to the local syntax for a
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279 path. This does not accept a complete URL. This function uses :func:`unquote`
280 to decode *path*.
281
282
Senthil Kumaranc9941862010-02-26 00:47:05 +0000283.. function:: getproxies()
284
285 This helper function returns a dictionary of scheme to proxy server URL
Senthil Kumaran8070ddc2012-01-11 01:35:02 +0800286 mappings. It scans the environment for variables named ``<scheme>_proxy``,
287 in case insensitive way, for all operating systems first, and when it cannot
288 find it, looks for proxy information from Mac OSX System Configuration for
289 Mac OS X and Windows Systems Registry for Windows.
Senthil Kumaranc9941862010-02-26 00:47:05 +0000290
Senthil Kumarana1fb6712013-05-02 05:50:21 -0700291.. note::
292 urllib also exposes certain utility functions like splittype, splithost and
293 others parsing url into various components. But it is recommended to use
294 :mod:`urlparse` for parsing urls than using these functions directly.
295 Python 3 does not expose these helper functions from :mod:`urllib.parse`
296 module.
297
Senthil Kumaranc9941862010-02-26 00:47:05 +0000298
Georg Brandl62647652008-01-07 18:23:27 +0000299URL Opener objects
300------------------
301
Benjamin Petersonb2064732014-11-23 20:55:24 -0600302.. class:: URLopener([proxies[, context[, **x509]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000303
304 Base class for opening and reading URLs. Unless you need to support opening
305 objects using schemes other than :file:`http:`, :file:`ftp:`, or :file:`file:`,
306 you probably want to use :class:`FancyURLopener`.
307
308 By default, the :class:`URLopener` class sends a :mailheader:`User-Agent` header
309 of ``urllib/VVV``, where *VVV* is the :mod:`urllib` version number.
310 Applications can define their own :mailheader:`User-Agent` header by subclassing
311 :class:`URLopener` or :class:`FancyURLopener` and setting the class attribute
312 :attr:`version` to an appropriate string value in the subclass definition.
313
314 The optional *proxies* parameter should be a dictionary mapping scheme names to
315 proxy URLs, where an empty dictionary turns proxies off completely. Its default
316 value is ``None``, in which case environmental proxy settings will be used if
317 present, as discussed in the definition of :func:`urlopen`, above.
318
Benjamin Petersonb2064732014-11-23 20:55:24 -0600319 The *context* parameter may be a :class:`ssl.SSLContext` instance. If given,
320 it defines the SSL settings the opener uses to make HTTPS connections.
321
Georg Brandl8ec7f652007-08-15 14:28:01 +0000322 Additional keyword parameters, collected in *x509*, may be used for
323 authentication of the client when using the :file:`https:` scheme. The keywords
324 *key_file* and *cert_file* are supported to provide an SSL key and certificate;
325 both are needed to support client authentication.
326
327 :class:`URLopener` objects will raise an :exc:`IOError` exception if the server
328 returns an error code.
329
Georg Brandl62647652008-01-07 18:23:27 +0000330 .. method:: open(fullurl[, data])
331
332 Open *fullurl* using the appropriate protocol. This method sets up cache and
333 proxy information, then calls the appropriate open method with its input
334 arguments. If the scheme is not recognized, :meth:`open_unknown` is called.
335 The *data* argument has the same meaning as the *data* argument of
336 :func:`urlopen`.
337
338
339 .. method:: open_unknown(fullurl[, data])
340
341 Overridable interface to open unknown URL types.
342
343
344 .. method:: retrieve(url[, filename[, reporthook[, data]]])
345
346 Retrieves the contents of *url* and places it in *filename*. The return value
347 is a tuple consisting of a local filename and either a
348 :class:`mimetools.Message` object containing the response headers (for remote
349 URLs) or ``None`` (for local URLs). The caller must then open and read the
350 contents of *filename*. If *filename* is not given and the URL refers to a
351 local file, the input filename is returned. If the URL is non-local and
352 *filename* is not given, the filename is the output of :func:`tempfile.mktemp`
353 with a suffix that matches the suffix of the last path component of the input
354 URL. If *reporthook* is given, it must be a function accepting three numeric
355 parameters. It will be called after each chunk of data is read from the
356 network. *reporthook* is ignored for local URLs.
357
358 If the *url* uses the :file:`http:` scheme identifier, the optional *data*
359 argument may be given to specify a ``POST`` request (normally the request type
360 is ``GET``). The *data* argument must in standard
361 :mimetype:`application/x-www-form-urlencoded` format; see the :func:`urlencode`
362 function below.
363
364
365 .. attribute:: version
366
367 Variable that specifies the user agent of the opener object. To get
368 :mod:`urllib` to tell servers that it is a particular user agent, set this in a
369 subclass as a class variable or in the constructor before calling the base
370 constructor.
371
Georg Brandl8ec7f652007-08-15 14:28:01 +0000372
373.. class:: FancyURLopener(...)
374
375 :class:`FancyURLopener` subclasses :class:`URLopener` providing default handling
376 for the following HTTP response codes: 301, 302, 303, 307 and 401. For the 30x
377 response codes listed above, the :mailheader:`Location` header is used to fetch
378 the actual URL. For 401 response codes (authentication required), basic HTTP
379 authentication is performed. For the 30x response codes, recursion is bounded
380 by the value of the *maxtries* attribute, which defaults to 10.
381
382 For all other response codes, the method :meth:`http_error_default` is called
383 which you can override in subclasses to handle the error appropriately.
384
385 .. note::
386
387 According to the letter of :rfc:`2616`, 301 and 302 responses to POST requests
388 must not be automatically redirected without confirmation by the user. In
389 reality, browsers do allow automatic redirection of these responses, changing
390 the POST to a GET, and :mod:`urllib` reproduces this behaviour.
391
392 The parameters to the constructor are the same as those for :class:`URLopener`.
393
394 .. note::
395
396 When performing basic authentication, a :class:`FancyURLopener` instance calls
397 its :meth:`prompt_user_passwd` method. The default implementation asks the
398 users for the required information on the controlling terminal. A subclass may
399 override this method to support more appropriate behavior if needed.
400
Georg Brandl62647652008-01-07 18:23:27 +0000401 The :class:`FancyURLopener` class offers one additional method that should be
402 overloaded to provide the appropriate behavior:
403
404 .. method:: prompt_user_passwd(host, realm)
405
406 Return information needed to authenticate the user at the given host in the
407 specified security realm. The return value should be a tuple, ``(user,
408 password)``, which can be used for basic authentication.
409
410 The implementation prompts for this information on the terminal; an application
411 should override this method to use an appropriate interaction model in the local
412 environment.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000413
414.. exception:: ContentTooShortError(msg[, content])
415
416 This exception is raised when the :func:`urlretrieve` function detects that the
417 amount of the downloaded data is less than the expected amount (given by the
418 *Content-Length* header). The :attr:`content` attribute stores the downloaded
419 (and supposedly truncated) data.
420
421 .. versionadded:: 2.5
422
Georg Brandl62647652008-01-07 18:23:27 +0000423
424:mod:`urllib` Restrictions
425--------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000426
427 .. index::
428 pair: HTTP; protocol
429 pair: FTP; protocol
430
431* Currently, only the following protocols are supported: HTTP, (versions 0.9 and
432 1.0), FTP, and local files.
433
434* The caching feature of :func:`urlretrieve` has been disabled until I find the
435 time to hack proper processing of Expiration time headers.
436
437* There should be a function to query whether a particular URL is in the cache.
438
439* For backward compatibility, if a URL appears to point to a local file but the
440 file can't be opened, the URL is re-interpreted using the FTP protocol. This
441 can sometimes cause confusing error messages.
442
443* The :func:`urlopen` and :func:`urlretrieve` functions can cause arbitrarily
444 long delays while waiting for a network connection to be set up. This means
445 that it is difficult to build an interactive Web client using these functions
446 without using threads.
447
448 .. index::
449 single: HTML
450 pair: HTTP; protocol
451 module: htmllib
452
453* The data returned by :func:`urlopen` or :func:`urlretrieve` is the raw data
454 returned by the server. This may be binary data (such as an image), plain text
455 or (for example) HTML. The HTTP protocol provides type information in the reply
456 header, which can be inspected by looking at the :mailheader:`Content-Type`
457 header. If the returned data is HTML, you can use the module :mod:`htmllib` to
458 parse it.
459
460 .. index:: single: FTP
461
462* The code handling the FTP protocol cannot differentiate between a file and a
463 directory. This can lead to unexpected behavior when attempting to read a URL
464 that points to a file that is not accessible. If the URL ends in a ``/``, it is
465 assumed to refer to a directory and will be handled accordingly. But if an
466 attempt to read a file leads to a 550 error (meaning the URL cannot be found or
467 is not accessible, often for permission reasons), then the path is treated as a
468 directory in order to handle the case when a directory is specified by a URL but
469 the trailing ``/`` has been left off. This can cause misleading results when
470 you try to fetch a file whose read permissions make it inaccessible; the FTP
471 code will try to read it, fail with a 550 error, and then perform a directory
472 listing for the unreadable file. If fine-grained control is needed, consider
Éric Araujoc75f2652011-03-20 18:34:24 +0100473 using the :mod:`ftplib` module, subclassing :class:`FancyURLopener`, or changing
Georg Brandl8ec7f652007-08-15 14:28:01 +0000474 *_urlopener* to meet your needs.
475
476* This module does not support the use of proxies which require authentication.
477 This may be implemented in the future.
478
479 .. index:: module: urlparse
480
481* Although the :mod:`urllib` module contains (undocumented) routines to parse
482 and unparse URL strings, the recommended interface for URL manipulation is in
483 module :mod:`urlparse`.
484
485
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486.. _urllib-examples:
487
488Examples
489--------
490
491Here is an example session that uses the ``GET`` method to retrieve a URL
492containing parameters::
493
494 >>> import urllib
495 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
496 >>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
497 >>> print f.read()
498
499The following example uses the ``POST`` method instead::
500
501 >>> import urllib
502 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
503 >>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
504 >>> print f.read()
505
506The following example uses an explicitly specified HTTP proxy, overriding
507environment settings::
508
509 >>> import urllib
510 >>> proxies = {'http': 'http://proxy.example.com:8080/'}
511 >>> opener = urllib.FancyURLopener(proxies)
512 >>> f = opener.open("http://www.python.org")
513 >>> f.read()
514
515The following example uses no proxies at all, overriding environment settings::
516
517 >>> import urllib
518 >>> opener = urllib.FancyURLopener({})
519 >>> f = opener.open("http://www.python.org/")
520 >>> f.read()
521