blob: ed649d10044561e51ae84fba7bdbf07881094224 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{urlparse} ---
Fred Drake0308ff82000-08-25 17:29:35 +00002 Parse URLs into components}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{urlparse}
4
Fred Drake72d157e1998-08-06 21:23:17 +00005\modulesynopsis{Parse URLs into components.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossuma12ef941995-02-27 17:53:25 +00007\index{WWW}
Guido van Rossum470be141995-03-17 16:07:09 +00008\index{World-Wide Web}
Guido van Rossuma12ef941995-02-27 17:53:25 +00009\index{URL}
10\indexii{URL}{parsing}
11\indexii{relative}{URL}
12
Guido van Rossum86751151995-02-28 17:14:32 +000013
Fred Drake0308ff82000-08-25 17:29:35 +000014This module defines a standard interface to break Uniform Resource
15Locator (URL) strings up in components (addressing scheme, network
16location, path etc.), to combine the components back into a URL
17string, and to convert a ``relative URL'' to an absolute URL given a
18``base URL.''
Guido van Rossuma12ef941995-02-27 17:53:25 +000019
Fred Draked1cc9c21998-01-21 04:55:02 +000020The module has been designed to match the Internet RFC on Relative
21Uniform Resource Locators (and discovered a bug in an earlier
Fred Drake45ca3332000-08-24 04:58:25 +000022draft!).
Guido van Rossuma12ef941995-02-27 17:53:25 +000023
24It defines the following functions:
25
Fred Drake6884e3b1997-12-29 19:09:37 +000026\begin{funcdesc}{urlparse}{urlstring\optional{, default_scheme\optional{, allow_fragments}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000027Parse a URL into 6 components, returning a 6-tuple: (addressing
28scheme, network location, path, parameters, query, fragment
29identifier). This corresponds to the general structure of a URL:
30\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
31Each tuple item is a string, possibly empty.
32The components are not broken up in smaller parts (e.g. the network
33location is a single string), and \% escapes are not expanded.
Guido van Rossum470be141995-03-17 16:07:09 +000034The delimiters as shown above are not part of the tuple items,
35except for a leading slash in the \var{path} component, which is
36retained if present.
Guido van Rossuma12ef941995-02-27 17:53:25 +000037
38Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000039
Fred Drake19479911998-02-13 06:58:54 +000040\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000041urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
Fred Drake19479911998-02-13 06:58:54 +000042\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000043
Guido van Rossuma12ef941995-02-27 17:53:25 +000044yields the tuple
Guido van Rossum96628a91995-04-10 11:34:00 +000045
Fred Drake19479911998-02-13 06:58:54 +000046\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000047('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
Fred Drake19479911998-02-13 06:58:54 +000048\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000049
Guido van Rossuma12ef941995-02-27 17:53:25 +000050If the \var{default_scheme} argument is specified, it gives the
51default addressing scheme, to be used only if the URL string does not
52specify one. The default value for this argument is the empty string.
53
54If the \var{allow_fragments} argument is zero, fragment identifiers
55are not allowed, even if the URL's addressing scheme normally does
56support them. The default value for this argument is \code{1}.
57\end{funcdesc}
58
59\begin{funcdesc}{urlunparse}{tuple}
Fred Draked1cc9c21998-01-21 04:55:02 +000060Construct a URL string from a tuple as returned by \code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000061This may result in a slightly different, but equivalent URL, if the
62URL that was parsed originally had redundant delimiters, e.g. a ? with
63an empty query (the draft states that these are equivalent).
64\end{funcdesc}
65
Fred Drakecce10901998-03-17 06:33:25 +000066\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000067Construct a full (``absolute'') URL by combining a ``base URL''
68(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
69uses components of the base URL, in particular the addressing scheme,
70the network location and (part of) the path, to provide missing
71components in the relative URL.
72
73Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000074
Fred Drake19479911998-02-13 06:58:54 +000075\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000076urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Fred Drake19479911998-02-13 06:58:54 +000077\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000078
Guido van Rossum96628a91995-04-10 11:34:00 +000079yields the string
80
Fred Drake19479911998-02-13 06:58:54 +000081\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000082'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +000083\end{verbatim}
Fred Drake0308ff82000-08-25 17:29:35 +000084
Guido van Rossuma12ef941995-02-27 17:53:25 +000085The \var{allow_fragments} argument has the same meaning as for
Fred Draked1cc9c21998-01-21 04:55:02 +000086\code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000087\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +000088
89
90\begin{seealso}
91 \seerfc{1738}{Uniform Resource Locators (URL)}{
92 This specifies the formal syntax and semantics of absolute
93 URLs.}
94 \seerfc{1808}{Relative Uniform Resource Locators}{
95 This Request For Comments includes the rules for joining an
96 absolute and a relative URL, including a fair normal of
97 ``Abnormal Examples'' which govern the treatment of border
98 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +000099 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
100 Document describing the generic syntactic requirements for
101 both Uniform Resource Names (URNs) and Uniform Resource
102 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000103\end{seealso}