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