2008-06-23 01:41:59 -03:00
|
|
|
:mod:`urllib.parse` --- Parse URLs into components
|
|
|
|
==================================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
.. module:: urllib.parse
|
2007-08-15 11:28:22 -03:00
|
|
|
:synopsis: Parse URLs into or assemble them from components.
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/urllib/parse.py`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: WWW
|
|
|
|
single: World Wide Web
|
|
|
|
single: URL
|
|
|
|
pair: URL; parsing
|
|
|
|
pair: relative; URL
|
|
|
|
|
2011-08-18 19:49:18 -03:00
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
This module defines a standard interface to break Uniform Resource Locator (URL)
|
|
|
|
strings up in components (addressing scheme, network location, path etc.), to
|
|
|
|
combine the components back into a URL string, and to convert a "relative URL"
|
|
|
|
to an absolute URL given a "base URL."
|
|
|
|
|
|
|
|
The module has been designed to match the Internet RFC on Relative Uniform
|
2012-06-29 01:07:58 -03:00
|
|
|
Resource Locators. It supports the following URL schemes: ``file``, ``ftp``,
|
|
|
|
``gopher``, ``hdl``, ``http``, ``https``, ``imap``, ``mailto``, ``mms``,
|
|
|
|
``news``, ``nntp``, ``prospero``, ``rsync``, ``rtsp``, ``rtspu``, ``sftp``,
|
|
|
|
``shttp``, ``sip``, ``sips``, ``snews``, ``svn``, ``svn+ssh``, ``telnet``,
|
2016-09-16 08:43:58 -03:00
|
|
|
``wais``, ``ws``, ``wss``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-11-30 11:48:08 -04:00
|
|
|
The :mod:`urllib.parse` module defines functions that fall into two broad
|
|
|
|
categories: URL parsing and URL quoting. These are covered in detail in
|
|
|
|
the following sections.
|
|
|
|
|
|
|
|
URL Parsing
|
|
|
|
-----------
|
|
|
|
|
|
|
|
The URL parsing functions focus on splitting a URL string into its components,
|
|
|
|
or on combining URL components into a URL string.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-05-25 12:36:46 -03:00
|
|
|
.. function:: urlparse(urlstring, scheme='', allow_fragments=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-03-24 18:28:48 -03:00
|
|
|
Parse a URL into six components, returning a 6-item :term:`named tuple`. This
|
|
|
|
corresponds to the general structure of a URL:
|
|
|
|
``scheme://netloc/path;parameters?query#fragment``.
|
2007-08-15 11:28:22 -03:00
|
|
|
Each tuple item is a string, possibly empty. The components are not broken up in
|
|
|
|
smaller parts (for example, the network location is a single string), and %
|
|
|
|
escapes are not expanded. The delimiters as shown above are not part of the
|
|
|
|
result, except for a leading slash in the *path* component, which is retained if
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
present. For example:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
>>> from urllib.parse import urlparse
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
>>> o # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
|
|
|
|
params='', query='', fragment='')
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> o.scheme
|
|
|
|
'http'
|
|
|
|
>>> o.port
|
|
|
|
80
|
|
|
|
>>> o.geturl()
|
|
|
|
'http://www.cwi.nl:80/%7Eguido/Python.html'
|
|
|
|
|
2010-11-07 08:57:04 -04:00
|
|
|
Following the syntax specifications in :rfc:`1808`, urlparse recognizes
|
|
|
|
a netloc only if it is properly introduced by '//'. Otherwise the
|
|
|
|
input is presumed to be a relative URL and thus to start with
|
|
|
|
a path component.
|
2010-08-04 01:50:44 -03:00
|
|
|
|
2017-04-27 09:23:34 -03:00
|
|
|
.. doctest::
|
|
|
|
:options: +NORMALIZE_WHITESPACE
|
|
|
|
|
2011-06-19 17:52:49 -03:00
|
|
|
>>> from urllib.parse import urlparse
|
2010-08-04 01:50:44 -03:00
|
|
|
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
|
|
|
|
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
|
|
|
|
params='', query='', fragment='')
|
2013-02-26 05:02:58 -04:00
|
|
|
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
|
2013-10-01 02:12:16 -03:00
|
|
|
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
|
2010-08-04 01:50:44 -03:00
|
|
|
params='', query='', fragment='')
|
|
|
|
>>> urlparse('help/Python.html')
|
|
|
|
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
|
|
|
|
query='', fragment='')
|
|
|
|
|
2015-06-25 17:38:48 -03:00
|
|
|
The *scheme* argument gives the default addressing scheme, to be
|
|
|
|
used only if the URL does not specify one. It should be the same type
|
|
|
|
(text or bytes) as *urlstring*, except that the default value ``''`` is
|
|
|
|
always allowed, and is automatically converted to ``b''`` if appropriate.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
If the *allow_fragments* argument is false, fragment identifiers are not
|
2015-06-25 17:38:48 -03:00
|
|
|
recognized. Instead, they are parsed as part of the path, parameters
|
|
|
|
or query component, and :attr:`fragment` is set to the empty string in
|
|
|
|
the return value.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-03-24 18:28:48 -03:00
|
|
|
The return value is a :term:`named tuple`, which means that its items can
|
|
|
|
be accessed by index or as named attributes, which are:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| Attribute | Index | Value | Value if not present |
|
|
|
|
+==================+=======+==========================+======================+
|
2015-06-25 17:38:48 -03:00
|
|
|
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
|
2007-08-15 11:28:22 -03:00
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`netloc` | 1 | Network location part | empty string |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`path` | 2 | Hierarchical path | empty string |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`params` | 3 | Parameters for last path | empty string |
|
|
|
|
| | | element | |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`query` | 4 | Query component | empty string |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`fragment` | 5 | Fragment identifier | empty string |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`username` | | User name | :const:`None` |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`password` | | Password | :const:`None` |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`hostname` | | Host name (lower case) | :const:`None` |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
| :attr:`port` | | Port number as integer, | :const:`None` |
|
|
|
|
| | | if present | |
|
|
|
|
+------------------+-------+--------------------------+----------------------+
|
|
|
|
|
2015-08-09 18:53:30 -03:00
|
|
|
Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
|
|
|
|
an invalid port is specified in the URL. See section
|
|
|
|
:ref:`urlparse-result-object` for more information on the result object.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2017-05-16 01:48:16 -03:00
|
|
|
Unmatched square brackets in the :attr:`netloc` attribute will raise a
|
|
|
|
:exc:`ValueError`.
|
|
|
|
|
2019-03-07 12:02:26 -04:00
|
|
|
Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
|
normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
|
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
|
decomposed before parsing, no error will be raised.
|
|
|
|
|
2019-03-24 18:28:48 -03:00
|
|
|
As is the case with all named tuples, the subclass has a few additional methods
|
|
|
|
and attributes that are particularly useful. One such method is :meth:`_replace`.
|
|
|
|
The :meth:`_replace` method will return a new ParseResult object replacing specified
|
|
|
|
fields with new values.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
:options: +NORMALIZE_WHITESPACE
|
|
|
|
|
|
|
|
>>> from urllib.parse import urlparse
|
|
|
|
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
|
|
|
|
>>> u
|
|
|
|
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
|
|
|
|
params='', query='', fragment='')
|
|
|
|
>>> u._replace(scheme='http')
|
|
|
|
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
|
|
|
|
params='', query='', fragment='')
|
|
|
|
|
|
|
|
|
2010-04-22 09:19:46 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Added IPv6 URL parsing capabilities.
|
|
|
|
|
2012-09-29 03:59:23 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
The fragment is now parsed for all URL schemes (unless *allow_fragment* is
|
|
|
|
false), in accordance with :rfc:`3986`. Previously, a whitelist of
|
|
|
|
schemes that support fragments existed.
|
|
|
|
|
2015-08-09 18:53:30 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
Out-of-range port numbers now raise :exc:`ValueError`, instead of
|
|
|
|
returning :const:`None`.
|
|
|
|
|
2019-03-07 12:02:26 -04:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Characters that affect netloc parsing under NFKC normalization will
|
|
|
|
now raise :exc:`ValueError`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None)
|
2008-09-03 19:49:01 -03:00
|
|
|
|
|
|
|
Parse a query string given as a string argument (data of type
|
|
|
|
:mimetype:`application/x-www-form-urlencoded`). Data are returned as a
|
|
|
|
dictionary. The dictionary keys are the unique query variable names and the
|
|
|
|
values are lists of values for each name.
|
|
|
|
|
|
|
|
The optional argument *keep_blank_values* is a flag indicating whether blank
|
2010-08-09 16:53:52 -03:00
|
|
|
values in percent-encoded queries should be treated as blank strings. A true value
|
2008-09-03 19:49:01 -03:00
|
|
|
indicates that blanks should be retained as blank strings. The default false
|
|
|
|
value indicates that blank values are to be ignored and treated as if they were
|
|
|
|
not included.
|
|
|
|
|
|
|
|
The optional argument *strict_parsing* is a flag indicating what to do with
|
|
|
|
parsing errors. If false (the default), errors are silently ignored. If true,
|
|
|
|
errors raise a :exc:`ValueError` exception.
|
|
|
|
|
2011-01-14 08:52:12 -04:00
|
|
|
The optional *encoding* and *errors* parameters specify how to decode
|
|
|
|
percent-encoded sequences into Unicode characters, as accepted by the
|
|
|
|
:meth:`bytes.decode` method.
|
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
The optional argument *max_num_fields* is the maximum number of fields to
|
|
|
|
read. If set, then throws a :exc:`ValueError` if there are more than
|
|
|
|
*max_num_fields* fields read.
|
|
|
|
|
2012-09-28 10:40:44 -03:00
|
|
|
Use the :func:`urllib.parse.urlencode` function (with the ``doseq``
|
|
|
|
parameter set to ``True``) to convert such dictionaries into query
|
|
|
|
strings.
|
2008-09-03 19:49:01 -03:00
|
|
|
|
2011-02-11 07:25:47 -04:00
|
|
|
|
2011-01-14 09:31:45 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Add *encoding* and *errors* parameters.
|
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Added *max_num_fields* parameter.
|
|
|
|
|
2008-09-03 19:49:01 -03:00
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
.. function:: parse_qsl(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None)
|
2008-09-03 19:49:01 -03:00
|
|
|
|
|
|
|
Parse a query string given as a string argument (data of type
|
|
|
|
:mimetype:`application/x-www-form-urlencoded`). Data are returned as a list of
|
|
|
|
name, value pairs.
|
|
|
|
|
|
|
|
The optional argument *keep_blank_values* is a flag indicating whether blank
|
2010-08-09 16:53:52 -03:00
|
|
|
values in percent-encoded queries should be treated as blank strings. A true value
|
2008-09-03 19:49:01 -03:00
|
|
|
indicates that blanks should be retained as blank strings. The default false
|
|
|
|
value indicates that blank values are to be ignored and treated as if they were
|
|
|
|
not included.
|
|
|
|
|
|
|
|
The optional argument *strict_parsing* is a flag indicating what to do with
|
|
|
|
parsing errors. If false (the default), errors are silently ignored. If true,
|
|
|
|
errors raise a :exc:`ValueError` exception.
|
|
|
|
|
2011-01-14 08:52:12 -04:00
|
|
|
The optional *encoding* and *errors* parameters specify how to decode
|
|
|
|
percent-encoded sequences into Unicode characters, as accepted by the
|
|
|
|
:meth:`bytes.decode` method.
|
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
The optional argument *max_num_fields* is the maximum number of fields to
|
|
|
|
read. If set, then throws a :exc:`ValueError` if there are more than
|
|
|
|
*max_num_fields* fields read.
|
|
|
|
|
2008-09-03 19:49:01 -03:00
|
|
|
Use the :func:`urllib.parse.urlencode` function to convert such lists of pairs into
|
|
|
|
query strings.
|
|
|
|
|
2011-01-14 09:31:45 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Add *encoding* and *errors* parameters.
|
|
|
|
|
2018-10-30 17:30:19 -03:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Added *max_num_fields* parameter.
|
|
|
|
|
2008-09-03 19:49:01 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. function:: urlunparse(parts)
|
|
|
|
|
2008-06-23 08:23:31 -03:00
|
|
|
Construct a URL from a tuple as returned by ``urlparse()``. The *parts*
|
|
|
|
argument can be any six-item iterable. This may result in a slightly
|
|
|
|
different, but equivalent URL, if the URL that was parsed originally had
|
|
|
|
unnecessary delimiters (for example, a ``?`` with an empty query; the RFC
|
|
|
|
states that these are equivalent).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2010-05-25 12:36:46 -03:00
|
|
|
.. function:: urlsplit(urlstring, scheme='', allow_fragments=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This is similar to :func:`urlparse`, but does not split the params from the URL.
|
|
|
|
This should generally be used instead of :func:`urlparse` if the more recent URL
|
|
|
|
syntax allowing parameters to be applied to each segment of the *path* portion
|
|
|
|
of the URL (see :rfc:`2396`) is wanted. A separate function is needed to
|
2019-03-24 18:28:48 -03:00
|
|
|
separate the path segments and parameters. This function returns a 5-item
|
|
|
|
:term:`named tuple`::
|
|
|
|
|
|
|
|
(addressing scheme, network location, path, query, fragment identifier).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-03-24 18:28:48 -03:00
|
|
|
The return value is a :term:`named tuple`, its items can be accessed by index
|
|
|
|
or as named attributes:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| Attribute | Index | Value | Value if not present |
|
|
|
|
+==================+=======+=========================+======================+
|
2015-06-25 17:38:48 -03:00
|
|
|
| :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter |
|
2007-08-15 11:28:22 -03:00
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`netloc` | 1 | Network location part | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`path` | 2 | Hierarchical path | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`query` | 3 | Query component | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`fragment` | 4 | Fragment identifier | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`username` | | User name | :const:`None` |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`password` | | Password | :const:`None` |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`hostname` | | Host name (lower case) | :const:`None` |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`port` | | Port number as integer, | :const:`None` |
|
|
|
|
| | | if present | |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
|
2015-08-09 18:53:30 -03:00
|
|
|
Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
|
|
|
|
an invalid port is specified in the URL. See section
|
|
|
|
:ref:`urlparse-result-object` for more information on the result object.
|
|
|
|
|
2017-05-16 01:48:16 -03:00
|
|
|
Unmatched square brackets in the :attr:`netloc` attribute will raise a
|
|
|
|
:exc:`ValueError`.
|
|
|
|
|
2019-03-07 12:02:26 -04:00
|
|
|
Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
|
normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
|
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
|
decomposed before parsing, no error will be raised.
|
|
|
|
|
2015-08-09 18:53:30 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
Out-of-range port numbers now raise :exc:`ValueError`, instead of
|
|
|
|
returning :const:`None`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-03-07 12:02:26 -04:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Characters that affect netloc parsing under NFKC normalization will
|
|
|
|
now raise :exc:`ValueError`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. function:: urlunsplit(parts)
|
|
|
|
|
2008-06-23 08:23:31 -03:00
|
|
|
Combine the elements of a tuple as returned by :func:`urlsplit` into a
|
|
|
|
complete URL as a string. The *parts* argument can be any five-item
|
|
|
|
iterable. This may result in a slightly different, but equivalent URL, if the
|
|
|
|
URL that was parsed originally had unnecessary delimiters (for example, a ?
|
|
|
|
with an empty query; the RFC states that these are equivalent).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: urljoin(base, url, allow_fragments=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Construct a full ("absolute") URL by combining a "base URL" (*base*) with
|
|
|
|
another URL (*url*). Informally, this uses components of the base URL, in
|
2008-06-23 08:23:31 -03:00
|
|
|
particular the addressing scheme, the network location and (part of) the
|
|
|
|
path, to provide missing components in the relative URL. For example:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
>>> from urllib.parse import urljoin
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
|
|
|
|
'http://www.cwi.nl/%7Eguido/FAQ.html'
|
|
|
|
|
|
|
|
The *allow_fragments* argument has the same meaning and default as for
|
|
|
|
:func:`urlparse`.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
If *url* is an absolute URL (that is, starting with ``//`` or ``scheme://``),
|
|
|
|
the *url*'s host name and/or scheme will be present in the result. For example:
|
|
|
|
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
.. doctest::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
|
|
|
|
... '//www.python.org/%7Eguido')
|
|
|
|
'http://www.python.org/%7Eguido'
|
|
|
|
|
|
|
|
If you do not want that behavior, preprocess the *url* with :func:`urlsplit` and
|
|
|
|
:func:`urlunsplit`, removing possible *scheme* and *netloc* parts.
|
|
|
|
|
|
|
|
|
2014-08-21 20:16:17 -03:00
|
|
|
.. versionchanged:: 3.5
|
|
|
|
|
|
|
|
Behaviour updated to match the semantics defined in :rfc:`3986`.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. function:: urldefrag(url)
|
|
|
|
|
2008-06-23 08:23:31 -03:00
|
|
|
If *url* contains a fragment identifier, return a modified version of *url*
|
|
|
|
with no fragment identifier, and the fragment identifier as a separate
|
|
|
|
string. If there is no fragment identifier in *url*, return *url* unmodified
|
|
|
|
and an empty string.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-03-24 18:28:48 -03:00
|
|
|
The return value is a :term:`named tuple`, its items can be accessed by index
|
|
|
|
or as named attributes:
|
2010-11-30 11:48:08 -04:00
|
|
|
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| Attribute | Index | Value | Value if not present |
|
|
|
|
+==================+=======+=========================+======================+
|
|
|
|
| :attr:`url` | 0 | URL with no fragment | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
| :attr:`fragment` | 1 | Fragment identifier | empty string |
|
|
|
|
+------------------+-------+-------------------------+----------------------+
|
|
|
|
|
|
|
|
See section :ref:`urlparse-result-object` for more information on the result
|
|
|
|
object.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
2011-01-24 05:01:27 -04:00
|
|
|
Result is a structured object rather than a simple 2-tuple.
|
2010-11-30 11:48:08 -04:00
|
|
|
|
2019-05-27 10:43:45 -03:00
|
|
|
.. function:: unwrap(url)
|
|
|
|
|
|
|
|
Extract the url from a wrapped URL (that is, a string formatted as
|
|
|
|
``<URL:scheme://host/path>``, ``<scheme://host/path>``, ``URL:scheme://host/path``
|
|
|
|
or ``scheme://host/path``). If *url* is not a wrapped URL, it is returned
|
|
|
|
without changes.
|
|
|
|
|
2011-01-24 15:59:08 -04:00
|
|
|
.. _parsing-ascii-encoded-bytes:
|
2010-11-30 11:48:08 -04:00
|
|
|
|
|
|
|
Parsing ASCII Encoded Bytes
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
The URL parsing functions were originally designed to operate on character
|
|
|
|
strings only. In practice, it is useful to be able to manipulate properly
|
|
|
|
quoted and encoded URLs as sequences of ASCII bytes. Accordingly, the
|
|
|
|
URL parsing functions in this module all operate on :class:`bytes` and
|
|
|
|
:class:`bytearray` objects in addition to :class:`str` objects.
|
|
|
|
|
|
|
|
If :class:`str` data is passed in, the result will also contain only
|
|
|
|
:class:`str` data. If :class:`bytes` or :class:`bytearray` data is
|
|
|
|
passed in, the result will contain only :class:`bytes` data.
|
|
|
|
|
|
|
|
Attempting to mix :class:`str` data with :class:`bytes` or
|
|
|
|
:class:`bytearray` in a single function call will result in a
|
2010-11-30 13:20:31 -04:00
|
|
|
:exc:`TypeError` being raised, while attempting to pass in non-ASCII
|
2010-11-30 11:48:08 -04:00
|
|
|
byte values will trigger :exc:`UnicodeDecodeError`.
|
|
|
|
|
|
|
|
To support easier conversion of result objects between :class:`str` and
|
|
|
|
:class:`bytes`, all return values from URL parsing functions provide
|
|
|
|
either an :meth:`encode` method (when the result contains :class:`str`
|
|
|
|
data) or a :meth:`decode` method (when the result contains :class:`bytes`
|
|
|
|
data). The signatures of these methods match those of the corresponding
|
|
|
|
:class:`str` and :class:`bytes` methods (except that the default encoding
|
|
|
|
is ``'ascii'`` rather than ``'utf-8'``). Each produces a value of a
|
|
|
|
corresponding type that contains either :class:`bytes` data (for
|
|
|
|
:meth:`encode` methods) or :class:`str` data (for
|
|
|
|
:meth:`decode` methods).
|
|
|
|
|
|
|
|
Applications that need to operate on potentially improperly quoted URLs
|
|
|
|
that may contain non-ASCII data will need to do their own decoding from
|
|
|
|
bytes to characters before invoking the URL parsing methods.
|
|
|
|
|
|
|
|
The behaviour described in this section applies only to the URL parsing
|
|
|
|
functions. The URL quoting functions use their own rules when producing
|
|
|
|
or consuming byte sequences as detailed in the documentation of the
|
|
|
|
individual URL quoting functions.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
URL parsing functions now accept ASCII encoded byte sequences
|
|
|
|
|
|
|
|
|
|
|
|
.. _urlparse-result-object:
|
|
|
|
|
|
|
|
Structured Parse Results
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The result objects from the :func:`urlparse`, :func:`urlsplit` and
|
2010-12-04 15:06:18 -04:00
|
|
|
:func:`urldefrag` functions are subclasses of the :class:`tuple` type.
|
2010-11-30 11:48:08 -04:00
|
|
|
These subclasses add the attributes listed in the documentation for
|
|
|
|
those functions, the encoding and decoding support described in the
|
|
|
|
previous section, as well as an additional method:
|
|
|
|
|
|
|
|
.. method:: urllib.parse.SplitResult.geturl()
|
|
|
|
|
|
|
|
Return the re-combined version of the original URL as a string. This may
|
|
|
|
differ from the original URL in that the scheme may be normalized to lower
|
|
|
|
case and empty components may be dropped. Specifically, empty parameters,
|
|
|
|
queries, and fragment identifiers will be removed.
|
|
|
|
|
|
|
|
For :func:`urldefrag` results, only empty fragment identifiers will be removed.
|
|
|
|
For :func:`urlsplit` and :func:`urlparse` results, all noted changes will be
|
|
|
|
made to the URL returned by this method.
|
|
|
|
|
|
|
|
The result of this method remains unchanged if passed back through the original
|
|
|
|
parsing function:
|
|
|
|
|
|
|
|
>>> from urllib.parse import urlsplit
|
|
|
|
>>> url = 'HTTP://www.Python.org/doc/#'
|
|
|
|
>>> r1 = urlsplit(url)
|
|
|
|
>>> r1.geturl()
|
|
|
|
'http://www.Python.org/doc/'
|
|
|
|
>>> r2 = urlsplit(r1.geturl())
|
|
|
|
>>> r2.geturl()
|
|
|
|
'http://www.Python.org/doc/'
|
|
|
|
|
|
|
|
|
|
|
|
The following classes provide the implementations of the structured parse
|
|
|
|
results when operating on :class:`str` objects:
|
|
|
|
|
|
|
|
.. class:: DefragResult(url, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urldefrag` results containing :class:`str`
|
|
|
|
data. The :meth:`encode` method returns a :class:`DefragResultBytes`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
.. class:: ParseResult(scheme, netloc, path, params, query, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urlparse` results containing :class:`str`
|
|
|
|
data. The :meth:`encode` method returns a :class:`ParseResultBytes`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
.. class:: SplitResult(scheme, netloc, path, query, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urlsplit` results containing :class:`str`
|
|
|
|
data. The :meth:`encode` method returns a :class:`SplitResultBytes`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
|
|
|
|
The following classes provide the implementations of the parse results when
|
|
|
|
operating on :class:`bytes` or :class:`bytearray` objects:
|
|
|
|
|
|
|
|
.. class:: DefragResultBytes(url, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urldefrag` results containing :class:`bytes`
|
|
|
|
data. The :meth:`decode` method returns a :class:`DefragResult`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
.. class:: ParseResultBytes(scheme, netloc, path, params, query, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urlparse` results containing :class:`bytes`
|
|
|
|
data. The :meth:`decode` method returns a :class:`ParseResult`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
.. class:: SplitResultBytes(scheme, netloc, path, query, fragment)
|
|
|
|
|
|
|
|
Concrete class for :func:`urlsplit` results containing :class:`bytes`
|
|
|
|
data. The :meth:`decode` method returns a :class:`SplitResult`
|
|
|
|
instance.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
|
|
|
|
URL Quoting
|
|
|
|
-----------
|
|
|
|
|
|
|
|
The URL quoting functions focus on taking program data and making it safe
|
|
|
|
for use as URL components by quoting special characters and appropriately
|
|
|
|
encoding non-ASCII text. They also support reversing these operations to
|
|
|
|
recreate the original data from the contents of a URL component if that
|
|
|
|
task isn't already covered by the URL parsing functions above.
|
2009-09-16 12:58:14 -03:00
|
|
|
|
|
|
|
.. function:: quote(string, safe='/', encoding=None, errors=None)
|
2008-06-23 01:41:59 -03:00
|
|
|
|
|
|
|
Replace special characters in *string* using the ``%xx`` escape. Letters,
|
2017-02-25 05:00:28 -04:00
|
|
|
digits, and the characters ``'_.-~'`` are never quoted. By default, this
|
2009-08-31 13:43:45 -03:00
|
|
|
function is intended for quoting the path section of URL. The optional *safe*
|
2008-08-18 18:44:30 -03:00
|
|
|
parameter specifies additional ASCII characters that should not be quoted
|
|
|
|
--- its default value is ``'/'``.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
*string* may be either a :class:`str` or a :class:`bytes`.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2017-02-25 05:00:28 -04:00
|
|
|
.. versionchanged:: 3.7
|
2018-05-31 01:39:00 -03:00
|
|
|
Moved from :rfc:`2396` to :rfc:`3986` for quoting URL strings. "~" is now
|
2019-11-18 11:42:13 -04:00
|
|
|
included in the set of unreserved characters.
|
2017-02-25 05:00:28 -04:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
The optional *encoding* and *errors* parameters specify how to deal with
|
|
|
|
non-ASCII characters, as accepted by the :meth:`str.encode` method.
|
|
|
|
*encoding* defaults to ``'utf-8'``.
|
|
|
|
*errors* defaults to ``'strict'``, meaning unsupported characters raise a
|
|
|
|
:class:`UnicodeEncodeError`.
|
|
|
|
*encoding* and *errors* must not be supplied if *string* is a
|
|
|
|
:class:`bytes`, or a :class:`TypeError` is raised.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
Note that ``quote(string, safe, encoding, errors)`` is equivalent to
|
|
|
|
``quote_from_bytes(string.encode(encoding, errors), safe)``.
|
|
|
|
|
|
|
|
Example: ``quote('/El Niño/')`` yields ``'/El%20Ni%C3%B1o/'``.
|
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: quote_plus(string, safe='', encoding=None, errors=None)
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-06-23 08:23:31 -03:00
|
|
|
Like :func:`quote`, but also replace spaces by plus signs, as required for
|
2009-07-29 04:27:08 -03:00
|
|
|
quoting HTML form values when building up a query string to go into a URL.
|
|
|
|
Plus signs in the original string are escaped unless they are included in
|
|
|
|
*safe*. It also does not have *safe* default to ``'/'``.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
Example: ``quote_plus('/El Niño/')`` yields ``'%2FEl+Ni%C3%B1o%2F'``.
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
|
|
|
|
.. function:: quote_from_bytes(bytes, safe='/')
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
Like :func:`quote`, but accepts a :class:`bytes` object rather than a
|
|
|
|
:class:`str`, and does not perform string-to-bytes encoding.
|
|
|
|
|
|
|
|
Example: ``quote_from_bytes(b'a&\xef')`` yields
|
|
|
|
``'a%26%EF'``.
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
|
|
|
|
.. function:: unquote(string, encoding='utf-8', errors='replace')
|
2008-06-23 01:41:59 -03:00
|
|
|
|
|
|
|
Replace ``%xx`` escapes by their single-character equivalent.
|
2008-08-18 18:44:30 -03:00
|
|
|
The optional *encoding* and *errors* parameters specify how to decode
|
|
|
|
percent-encoded sequences into Unicode characters, as accepted by the
|
|
|
|
:meth:`bytes.decode` method.
|
|
|
|
|
|
|
|
*string* must be a :class:`str`.
|
|
|
|
|
|
|
|
*encoding* defaults to ``'utf-8'``.
|
|
|
|
*errors* defaults to ``'replace'``, meaning invalid sequences are replaced
|
|
|
|
by a placeholder character.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
Example: ``unquote('/El%20Ni%C3%B1o/')`` yields ``'/El Niño/'``.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: unquote_plus(string, encoding='utf-8', errors='replace')
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2008-06-23 08:23:31 -03:00
|
|
|
Like :func:`unquote`, but also replace plus signs by spaces, as required for
|
2008-06-23 01:41:59 -03:00
|
|
|
unquoting HTML form values.
|
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
*string* must be a :class:`str`.
|
|
|
|
|
|
|
|
Example: ``unquote_plus('/El+Ni%C3%B1o/')`` yields ``'/El Niño/'``.
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
|
2008-08-18 18:44:30 -03:00
|
|
|
.. function:: unquote_to_bytes(string)
|
|
|
|
|
|
|
|
Replace ``%xx`` escapes by their single-octet equivalent, and return a
|
|
|
|
:class:`bytes` object.
|
|
|
|
|
|
|
|
*string* may be either a :class:`str` or a :class:`bytes`.
|
|
|
|
|
|
|
|
If it is a :class:`str`, unescaped non-ASCII characters in *string*
|
|
|
|
are encoded into UTF-8 bytes.
|
|
|
|
|
2010-11-30 11:48:08 -04:00
|
|
|
Example: ``unquote_to_bytes('a%26%EF')`` yields ``b'a&\xef'``.
|
2008-08-18 18:44:30 -03:00
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2015-05-17 21:44:50 -03:00
|
|
|
.. function:: urlencode(query, doseq=False, safe='', encoding=None, \
|
|
|
|
errors=None, quote_via=quote_plus)
|
2010-07-03 14:48:22 -03:00
|
|
|
|
|
|
|
Convert a mapping object or a sequence of two-element tuples, which may
|
2015-11-24 18:33:18 -04:00
|
|
|
contain :class:`str` or :class:`bytes` objects, to a percent-encoded ASCII
|
|
|
|
text string. If the resultant string is to be used as a *data* for POST
|
|
|
|
operation with the :func:`~urllib.request.urlopen` function, then
|
|
|
|
it should be encoded to bytes, otherwise it would result in a
|
|
|
|
:exc:`TypeError`.
|
2012-03-15 22:11:16 -03:00
|
|
|
|
2010-07-03 14:48:22 -03:00
|
|
|
The resulting string is a series of ``key=value`` pairs separated by ``'&'``
|
2015-05-17 21:44:50 -03:00
|
|
|
characters, where both *key* and *value* are quoted using the *quote_via*
|
|
|
|
function. By default, :func:`quote_plus` is used to quote the values, which
|
|
|
|
means spaces are quoted as a ``'+'`` character and '/' characters are
|
|
|
|
encoded as ``%2F``, which follows the standard for GET requests
|
|
|
|
(``application/x-www-form-urlencoded``). An alternate function that can be
|
|
|
|
passed as *quote_via* is :func:`quote`, which will encode spaces as ``%20``
|
|
|
|
and not encode '/' characters. For maximum control of what is quoted, use
|
|
|
|
``quote`` and specify a value for *safe*.
|
|
|
|
|
|
|
|
When a sequence of two-element tuples is used as the *query*
|
2010-07-03 14:48:22 -03:00
|
|
|
argument, the first element of each tuple is a key and the second is a
|
|
|
|
value. The value element in itself can be a sequence and in that case, if
|
2016-10-19 10:43:42 -03:00
|
|
|
the optional parameter *doseq* is evaluates to ``True``, individual
|
2010-07-03 14:48:22 -03:00
|
|
|
``key=value`` pairs separated by ``'&'`` are generated for each element of
|
|
|
|
the value sequence for the key. The order of parameters in the encoded
|
2010-11-30 11:48:08 -04:00
|
|
|
string will match the order of parameter tuples in the sequence.
|
2010-07-03 14:48:22 -03:00
|
|
|
|
2014-12-24 22:23:18 -04:00
|
|
|
The *safe*, *encoding*, and *errors* parameters are passed down to
|
2015-05-17 21:44:50 -03:00
|
|
|
*quote_via* (the *encoding* and *errors* parameters are only passed
|
2014-12-24 22:23:18 -04:00
|
|
|
when a query element is a :class:`str`).
|
2010-11-30 11:48:08 -04:00
|
|
|
|
|
|
|
To reverse this encoding process, :func:`parse_qs` and :func:`parse_qsl` are
|
|
|
|
provided in this module to parse query strings into Python data structures.
|
2010-07-03 14:48:22 -03:00
|
|
|
|
2011-02-11 07:25:47 -04:00
|
|
|
Refer to :ref:`urllib examples <urllib-examples>` to find out how urlencode
|
|
|
|
method can be used for generating query string for a URL or data for POST.
|
|
|
|
|
2010-07-03 14:48:22 -03:00
|
|
|
.. versionchanged:: 3.2
|
2010-08-17 12:07:14 -03:00
|
|
|
Query parameter supports bytes and string objects.
|
2008-06-23 01:41:59 -03:00
|
|
|
|
2015-05-17 21:44:50 -03:00
|
|
|
.. versionadded:: 3.5
|
|
|
|
*quote_via* parameter.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2010-04-22 02:53:18 -03:00
|
|
|
:rfc:`3986` - Uniform Resource Identifiers
|
2011-06-19 17:52:49 -03:00
|
|
|
This is the current standard (STD66). Any changes to urllib.parse module
|
2010-04-22 02:53:18 -03:00
|
|
|
should conform to this. Certain deviations could be observed, which are
|
2010-09-21 11:48:28 -03:00
|
|
|
mostly for backward compatibility purposes and for certain de-facto
|
2010-04-22 02:53:18 -03:00
|
|
|
parsing requirements as commonly observed in major browsers.
|
|
|
|
|
|
|
|
:rfc:`2732` - Format for Literal IPv6 Addresses in URL's.
|
|
|
|
This specifies the parsing requirements of IPv6 URLs.
|
|
|
|
|
|
|
|
:rfc:`2396` - Uniform Resource Identifiers (URI): Generic Syntax
|
|
|
|
Document describing the generic syntactic requirements for both Uniform Resource
|
|
|
|
Names (URNs) and Uniform Resource Locators (URLs).
|
|
|
|
|
|
|
|
:rfc:`2368` - The mailto URL scheme.
|
2016-05-27 23:20:39 -03:00
|
|
|
Parsing requirements for mailto URL schemes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
:rfc:`1808` - Relative Uniform Resource Locators
|
|
|
|
This Request For Comments includes the rules for joining an absolute and a
|
|
|
|
relative URL, including a fair number of "Abnormal Examples" which govern the
|
|
|
|
treatment of border cases.
|
|
|
|
|
2010-04-22 02:53:18 -03:00
|
|
|
:rfc:`1738` - Uniform Resource Locators (URL)
|
|
|
|
This specifies the formal syntax and semantics of absolute URLs.
|