blob: 9a2b4f530771e8b021507516a6f20437b81d3622 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{urlparse}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{urlparse}
3
Fred Drake72d157e1998-08-06 21:23:17 +00004\modulesynopsis{Parse URLs into components.}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
Guido van Rossuma12ef941995-02-27 17:53:25 +00006\index{WWW}
Guido van Rossum470be141995-03-17 16:07:09 +00007\index{World-Wide Web}
Guido van Rossuma12ef941995-02-27 17:53:25 +00008\index{URL}
9\indexii{URL}{parsing}
10\indexii{relative}{URL}
11
Guido van Rossum86751151995-02-28 17:14:32 +000012
Guido van Rossuma12ef941995-02-27 17:53:25 +000013This module defines a standard interface to break URL strings up in
14components (addessing scheme, network location, path etc.), to combine
15the components back into a URL string, and to convert a ``relative
Fred Drake72d157e1998-08-06 21:23:17 +000016URL'' to an absolute URL given a ``base URL.''
Guido van Rossuma12ef941995-02-27 17:53:25 +000017
Fred Draked1cc9c21998-01-21 04:55:02 +000018The module has been designed to match the Internet RFC on Relative
19Uniform Resource Locators (and discovered a bug in an earlier
Fred Drakec5891241998-02-09 19:16:20 +000020draft!). Refer to \rfc{1808} for details on relative
21URLs and \rfc{1738} for information on basic URL syntax.
Guido van Rossuma12ef941995-02-27 17:53:25 +000022
23It defines the following functions:
24
Fred Drake6884e3b1997-12-29 19:09:37 +000025\begin{funcdesc}{urlparse}{urlstring\optional{, default_scheme\optional{, allow_fragments}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000026Parse a URL into 6 components, returning a 6-tuple: (addressing
27scheme, network location, path, parameters, query, fragment
28identifier). This corresponds to the general structure of a URL:
29\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
30Each tuple item is a string, possibly empty.
31The components are not broken up in smaller parts (e.g. the network
32location is a single string), and \% escapes are not expanded.
Guido van Rossum470be141995-03-17 16:07:09 +000033The delimiters as shown above are not part of the tuple items,
34except for a leading slash in the \var{path} component, which is
35retained if present.
Guido van Rossuma12ef941995-02-27 17:53:25 +000036
37Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000038
Fred Drake19479911998-02-13 06:58:54 +000039\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000040urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
Fred Drake19479911998-02-13 06:58:54 +000041\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000042%
Guido van Rossuma12ef941995-02-27 17:53:25 +000043yields the tuple
Guido van Rossum96628a91995-04-10 11:34:00 +000044
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000046('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
Fred Drake19479911998-02-13 06:58:54 +000047\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000048%
Guido van Rossuma12ef941995-02-27 17:53:25 +000049If the \var{default_scheme} argument is specified, it gives the
50default addressing scheme, to be used only if the URL string does not
51specify one. The default value for this argument is the empty string.
52
53If the \var{allow_fragments} argument is zero, fragment identifiers
54are not allowed, even if the URL's addressing scheme normally does
55support them. The default value for this argument is \code{1}.
56\end{funcdesc}
57
58\begin{funcdesc}{urlunparse}{tuple}
Fred Draked1cc9c21998-01-21 04:55:02 +000059Construct a URL string from a tuple as returned by \code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000060This may result in a slightly different, but equivalent URL, if the
61URL that was parsed originally had redundant delimiters, e.g. a ? with
62an empty query (the draft states that these are equivalent).
63\end{funcdesc}
64
Fred Drakecce10901998-03-17 06:33:25 +000065\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000066Construct a full (``absolute'') URL by combining a ``base URL''
67(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
68uses components of the base URL, in particular the addressing scheme,
69the network location and (part of) the path, to provide missing
70components in the relative URL.
71
72Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000073
Fred Drake19479911998-02-13 06:58:54 +000074\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000075urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Fred Drake19479911998-02-13 06:58:54 +000076\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000077%
Guido van Rossum96628a91995-04-10 11:34:00 +000078yields the string
79
Fred Drake19479911998-02-13 06:58:54 +000080\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000081'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +000082\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000083%
Guido van Rossuma12ef941995-02-27 17:53:25 +000084The \var{allow_fragments} argument has the same meaning as for
Fred Draked1cc9c21998-01-21 04:55:02 +000085\code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000086\end{funcdesc}