blob: 60c83cc341fde845743238bd93f6ded187616f83 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{urlparse} ---
2 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
Guido van Rossuma12ef941995-02-27 17:53:25 +000014This module defines a standard interface to break URL strings up in
15components (addessing scheme, network location, path etc.), to combine
16the components back into a URL string, and to convert a ``relative
Fred Drake72d157e1998-08-06 21:23:17 +000017URL'' to an absolute URL given a ``base URL.''
Guido van Rossuma12ef941995-02-27 17:53:25 +000018
Fred Draked1cc9c21998-01-21 04:55:02 +000019The module has been designed to match the Internet RFC on Relative
20Uniform Resource Locators (and discovered a bug in an earlier
Fred Drakec5891241998-02-09 19:16:20 +000021draft!). Refer to \rfc{1808} for details on relative
22URLs and \rfc{1738} for information on basic URL syntax.
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}
Guido van Rossume47da0a1997-07-17 16:34:52 +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}
Guido van Rossume47da0a1997-07-17 16:34:52 +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}
Guido van Rossume47da0a1997-07-17 16:34:52 +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}
Guido van Rossume47da0a1997-07-17 16:34:52 +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}