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