blob: 84954379ecf5222d0d3ed0182c574f6d690a45e3 [file] [log] [blame]
Guido van Rossuma12ef941995-02-27 17:53:25 +00001\section{Built-in module \sectcode{urlparse}}
2\stmodindex{urlparse}
3\index{WWW}
4\indexii{World-Wide}{Web}
5\index{URL}
6\indexii{URL}{parsing}
7\indexii{relative}{URL}
8
9This module defines a standard interface to break URL strings up in
10components (addessing scheme, network location, path etc.), to combine
11the components back into a URL string, and to convert a ``relative
12URL'' to an absolute URL given a ``base URL''.
13
14The module has been designed to match the current Internet draft on
15Relative Uniform Resource Locators (and discovered a bug in an earlier
16draft!).
17
18It defines the following functions:
19
20\begin{funcdesc}{urlparse}{urlstring\optional{\,
21default_scheme\optional{\, allow_fragments}}}
22Parse a URL into 6 components, returning a 6-tuple: (addressing
23scheme, network location, path, parameters, query, fragment
24identifier). This corresponds to the general structure of a URL:
25\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
26Each tuple item is a string, possibly empty.
27The components are not broken up in smaller parts (e.g. the network
28location is a single string), and \% escapes are not expanded.
29The delimiters as shown above are not part of the tuple items, {\em
30except} for a leading slash in the \var{path} component, which is
31kept if present.
32
33Example:
34\code{urlparse('http://www.cwi.nl:80/\%7eguido/Python.html')}
35yields the tuple
36\code{('http', 'www.cwi.nl:80', '/\%e7guido/Python.html', '', '', '')}.
37
38If the \var{default_scheme} argument is specified, it gives the
39default addressing scheme, to be used only if the URL string does not
40specify one. The default value for this argument is the empty string.
41
42If the \var{allow_fragments} argument is zero, fragment identifiers
43are not allowed, even if the URL's addressing scheme normally does
44support them. The default value for this argument is \code{1}.
45\end{funcdesc}
46
47\begin{funcdesc}{urlunparse}{tuple}
48Construct a URL string from a tuple as returned by \code{urlparse}.
49This may result in a slightly different, but equivalent URL, if the
50URL that was parsed originally had redundant delimiters, e.g. a ? with
51an empty query (the draft states that these are equivalent).
52\end{funcdesc}
53
54\begin{funcdesc}{urljoin}{base\, url\optional{\, allow_fragments}}
55Construct a full (``absolute'') URL by combining a ``base URL''
56(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
57uses components of the base URL, in particular the addressing scheme,
58the network location and (part of) the path, to provide missing
59components in the relative URL.
60
61Example:
62\code{urljoin('http://www.cwi.nl/\%7eguido/Python.html',}
63\code{'FAQ.html')} yields the string
64\code{'http://www.cwi.nl/\%7eguido/FAQ.html'}.
65
66The \var{allow_fragments} argument has the same meaning as for
67\code{urlparse}.
68\end{funcdesc}