This commit was manufactured by cvs2svn to create tag 'r241c1'.
[python/dscho.git] / Doc / lib / liburlparse.tex
blob7e00ca5692e0d40094d7e6d2195ca57dce900499
1 \section{\module{urlparse} ---
2 Parse URLs into components}
3 \declaremodule{standard}{urlparse}
5 \modulesynopsis{Parse URLs into components.}
7 \index{WWW}
8 \index{World Wide Web}
9 \index{URL}
10 \indexii{URL}{parsing}
11 \indexii{relative}{URL}
14 This module defines a standard interface to break Uniform Resource
15 Locator (URL) strings up in components (addressing scheme, network
16 location, path etc.), to combine the components back into a URL
17 string, and to convert a ``relative URL'' to an absolute URL given a
18 ``base URL.''
20 The module has been designed to match the Internet RFC on Relative
21 Uniform Resource Locators (and discovered a bug in an earlier
22 draft!).
24 It defines the following functions:
26 \begin{funcdesc}{urlparse}{urlstring\optional{, default_scheme\optional{, allow_fragments}}}
27 Parse a URL into 6 components, returning a 6-tuple: (addressing
28 scheme, network location, path, parameters, query, fragment
29 identifier). This corresponds to the general structure of a URL:
30 \code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
31 Each tuple item is a string, possibly empty.
32 The components are not broken up in smaller parts (e.g. the network
33 location is a single string), and \% escapes are not expanded.
34 The delimiters as shown above are not part of the tuple items,
35 except for a leading slash in the \var{path} component, which is
36 retained if present.
38 Example:
40 \begin{verbatim}
41 urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
42 \end{verbatim}
44 yields the tuple
46 \begin{verbatim}
47 ('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
48 \end{verbatim}
50 If the \var{default_scheme} argument is specified, it gives the
51 default addressing scheme, to be used only if the URL string does not
52 specify one. The default value for this argument is the empty string.
54 If the \var{allow_fragments} argument is zero, fragment identifiers
55 are not allowed, even if the URL's addressing scheme normally does
56 support them. The default value for this argument is \code{1}.
57 \end{funcdesc}
59 \begin{funcdesc}{urlunparse}{tuple}
60 Construct a URL string from a tuple as returned by \code{urlparse()}.
61 This may result in a slightly different, but equivalent URL, if the
62 URL that was parsed originally had redundant delimiters, e.g. a ? with
63 an empty query (the draft states that these are equivalent).
64 \end{funcdesc}
66 \begin{funcdesc}{urlsplit}{urlstring\optional{,
67 default_scheme\optional{, allow_fragments}}}
68 This is similar to \function{urlparse()}, but does not split the
69 params from the URL. This should generally be used instead of
70 \function{urlparse()} if the more recent URL syntax allowing
71 parameters to be applied to each segment of the \var{path} portion of
72 the URL (see \rfc{2396}). A separate function is needed to separate
73 the path segments and parameters. This function returns a 5-tuple:
74 (addressing scheme, network location, path, query, fragment
75 identifier).
76 \versionadded{2.2}
77 \end{funcdesc}
79 \begin{funcdesc}{urlunsplit}{tuple}
80 Combine the elements of a tuple as returned by \function{urlsplit()}
81 into a complete URL as a string.
82 \versionadded{2.2}
83 \end{funcdesc}
85 \begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
86 Construct a full (``absolute'') URL by combining a ``base URL''
87 (\var{base}) with a ``relative URL'' (\var{url}). Informally, this
88 uses components of the base URL, in particular the addressing scheme,
89 the network location and (part of) the path, to provide missing
90 components in the relative URL.
92 Example:
94 \begin{verbatim}
95 urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
96 \end{verbatim}
98 yields the string
100 \begin{verbatim}
101 'http://www.cwi.nl/%7Eguido/FAQ.html'
102 \end{verbatim}
104 The \var{allow_fragments} argument has the same meaning as for
105 \code{urlparse()}.
106 \end{funcdesc}
108 \begin{funcdesc}{urldefrag}{url}
109 If \var{url} contains a fragment identifier, returns a modified
110 version of \var{url} with no fragment identifier, and the fragment
111 identifier as a separate string. If there is no fragment identifier
112 in \var{url}, returns \var{url} unmodified and an empty string.
113 \end{funcdesc}
116 \begin{seealso}
117 \seerfc{1738}{Uniform Resource Locators (URL)}{
118 This specifies the formal syntax and semantics of absolute
119 URLs.}
120 \seerfc{1808}{Relative Uniform Resource Locators}{
121 This Request For Comments includes the rules for joining an
122 absolute and a relative URL, including a fair number of
123 ``Abnormal Examples'' which govern the treatment of border
124 cases.}
125 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
126 Document describing the generic syntactic requirements for
127 both Uniform Resource Names (URNs) and Uniform Resource
128 Locators (URLs).}
129 \end{seealso}