blob: 4819d3ce3552f0235880df101877def0062bdaba [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}
Fred Drake8ee679f2001-07-14 02:50:55 +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 Drake55452192001-11-16 03:22:15 +000066\begin{funcdesc}{urlsplit}{urlstring\optional{,
67 default_scheme\optional{, allow_fragments}}}
68This is similar to \function{urlparse()}, but does not split the
69params from the URL. This should generally be used instead of
70\function{urlparse()} if the more recent URL syntax allowing
71parameters to be applied to each segment of the \var{path} portion of
72the URL (see \rfc{2396}). A separate function is needed to separate
73the path segments and parameters. This function returns a 5-tuple:
74(addressing scheme, network location, path, query, fragment
75identifier).
76\versionadded{2.2}
77\end{funcdesc}
78
79\begin{funcdesc}{urlunsplit}{tuple}
80Combine the elements of a tuple as returned by \function{urlsplit()}
81into a complete URL as a string.
82\versionadded{2.2}
83\end{funcdesc}
84
Fred Drakecce10901998-03-17 06:33:25 +000085\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000086Construct a full (``absolute'') URL by combining a ``base URL''
87(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
88uses components of the base URL, in particular the addressing scheme,
89the network location and (part of) the path, to provide missing
90components in the relative URL.
91
92Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000093
Fred Drake19479911998-02-13 06:58:54 +000094\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000095urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Fred Drake19479911998-02-13 06:58:54 +000096\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000097
Guido van Rossum96628a91995-04-10 11:34:00 +000098yields the string
99
Fred Drake19479911998-02-13 06:58:54 +0000100\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +0000101'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +0000102\end{verbatim}
Fred Drake0308ff82000-08-25 17:29:35 +0000103
Guido van Rossuma12ef941995-02-27 17:53:25 +0000104The \var{allow_fragments} argument has the same meaning as for
Fred Draked1cc9c21998-01-21 04:55:02 +0000105\code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000106\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +0000107
Fred Drake98ef20d2002-10-16 20:07:54 +0000108\begin{funcdesc}{urldefrag}{url}
109If \var{url} contains a fragment identifier, returns a modified
110version of \var{url} with no fragment identifier, and the fragment
111identifier as a separate string. If there is no fragment identifier
112in \var{url}, returns \var{url} unmodified and an empty string.
113\end{funcdesc}
114
Fred Drake45ca3332000-08-24 04:58:25 +0000115
116\begin{seealso}
117 \seerfc{1738}{Uniform Resource Locators (URL)}{
118 This specifies the formal syntax and semantics of absolute
119 URLs.}
120 \seerfc{1808}{Relative Uniform Resource Locators}{
121 This Request For Comments includes the rules for joining an
122 absolute and a relative URL, including a fair normal of
123 ``Abnormal Examples'' which govern the treatment of border
124 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +0000125 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
126 Document describing the generic syntactic requirements for
127 both Uniform Resource Names (URNs) and Uniform Resource
128 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000129\end{seealso}