blob: 36ca949ba63a3b579179a4f29e82fef50f0b4e4e [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{urlparse}}
Guido van Rossuma12ef941995-02-27 17:53:25 +00002\stmodindex{urlparse}
3\index{WWW}
Guido van Rossum470be141995-03-17 16:07:09 +00004\index{World-Wide Web}
Guido van Rossuma12ef941995-02-27 17:53:25 +00005\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.
Guido van Rossum470be141995-03-17 16:07:09 +000031The delimiters as shown above are not part of the tuple items,
32except for a leading slash in the \var{path} component, which is
33retained if present.
Guido van Rossuma12ef941995-02-27 17:53:25 +000034
35Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000036
37\begin{verbatim}
38urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
39\end{verbatim}
40
Guido van Rossuma12ef941995-02-27 17:53:25 +000041yields the tuple
Guido van Rossum96628a91995-04-10 11:34:00 +000042
43\begin{verbatim}
44('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
45\end{verbatim}
Guido van Rossuma12ef941995-02-27 17:53:25 +000046
47If the \var{default_scheme} argument is specified, it gives the
48default addressing scheme, to be used only if the URL string does not
49specify one. The default value for this argument is the empty string.
50
51If the \var{allow_fragments} argument is zero, fragment identifiers
52are not allowed, even if the URL's addressing scheme normally does
53support them. The default value for this argument is \code{1}.
54\end{funcdesc}
55
56\begin{funcdesc}{urlunparse}{tuple}
57Construct a URL string from a tuple as returned by \code{urlparse}.
58This may result in a slightly different, but equivalent URL, if the
59URL that was parsed originally had redundant delimiters, e.g. a ? with
60an empty query (the draft states that these are equivalent).
61\end{funcdesc}
62
63\begin{funcdesc}{urljoin}{base\, url\optional{\, allow_fragments}}
64Construct a full (``absolute'') URL by combining a ``base URL''
65(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
66uses components of the base URL, in particular the addressing scheme,
67the network location and (part of) the path, to provide missing
68components in the relative URL.
69
70Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000071
72\begin{verbatim}
73urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
74\end{verbatim}
75
76yields the string
77
78\begin{verbatim}
79'http://www.cwi.nl/%7Eguido/FAQ.html'
80\end{verbatim}
Guido van Rossuma12ef941995-02-27 17:53:25 +000081
82The \var{allow_fragments} argument has the same meaning as for
83\code{urlparse}.
84\end{funcdesc}