blob: a060cd6eda267eb61868ede40dda01ce30a17379 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{urlparse} ---
Fred Drake0308ff82000-08-25 17:29:35 +00002 Parse URLs into components}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{urlparse}
4
Fred Drake72d157e1998-08-06 21:23:17 +00005\modulesynopsis{Parse URLs into components.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossuma12ef941995-02-27 17:53:25 +00007\index{WWW}
Fred Drake8ee679f2001-07-14 02:50:55 +00008\index{World Wide Web}
Guido van Rossuma12ef941995-02-27 17:53:25 +00009\index{URL}
10\indexii{URL}{parsing}
11\indexii{relative}{URL}
12
Guido van Rossum86751151995-02-28 17:14:32 +000013
Fred Drake0308ff82000-08-25 17:29:35 +000014This module defines a standard interface to break Uniform Resource
15Locator (URL) strings up in components (addressing scheme, network
16location, path etc.), to combine the components back into a URL
17string, and to convert a ``relative URL'' to an absolute URL given a
18``base URL.''
Guido van Rossuma12ef941995-02-27 17:53:25 +000019
Fred Draked1cc9c21998-01-21 04:55:02 +000020The module has been designed to match the Internet RFC on Relative
21Uniform Resource Locators (and discovered a bug in an earlier
Georg Brandl1de37002006-01-20 21:17:01 +000022draft!). It supports the following URL schemes:
23\code{file}, \code{ftp}, \code{gopher}, \code{hdl}, \code{http},
24\code{https}, \code{imap}, \code{mailto}, \code{mms}, \code{news},
25\code{nntp}, \code{prospero}, \code{rsync}, \code{rtsp}, \code{rtspu},
26\code{sftp}, \code{shttp}, \code{sip}, \code{snews}, \code{svn},
27\code{svn+ssh}, \code{telnet}, \code{wais}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000028
Georg Brandl1de37002006-01-20 21:17:01 +000029The \module{urlparse} module defines the following functions:
Guido van Rossuma12ef941995-02-27 17:53:25 +000030
Fred Drake6884e3b1997-12-29 19:09:37 +000031\begin{funcdesc}{urlparse}{urlstring\optional{, default_scheme\optional{, allow_fragments}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000032Parse a URL into 6 components, returning a 6-tuple: (addressing
33scheme, network location, path, parameters, query, fragment
34identifier). This corresponds to the general structure of a URL:
35\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
36Each tuple item is a string, possibly empty.
37The components are not broken up in smaller parts (e.g. the network
38location is a single string), and \% escapes are not expanded.
Guido van Rossum470be141995-03-17 16:07:09 +000039The delimiters as shown above are not part of the tuple items,
40except for a leading slash in the \var{path} component, which is
41retained if present.
Guido van Rossuma12ef941995-02-27 17:53:25 +000042
43Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000044
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000046urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
Fred Drake19479911998-02-13 06:58:54 +000047\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000048
Guido van Rossuma12ef941995-02-27 17:53:25 +000049yields the tuple
Guido van Rossum96628a91995-04-10 11:34:00 +000050
Fred Drake19479911998-02-13 06:58:54 +000051\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000052('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
Fred Drake19479911998-02-13 06:58:54 +000053\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000054
Guido van Rossuma12ef941995-02-27 17:53:25 +000055If the \var{default_scheme} argument is specified, it gives the
56default addressing scheme, to be used only if the URL string does not
57specify one. The default value for this argument is the empty string.
58
59If the \var{allow_fragments} argument is zero, fragment identifiers
60are not allowed, even if the URL's addressing scheme normally does
61support them. The default value for this argument is \code{1}.
62\end{funcdesc}
63
64\begin{funcdesc}{urlunparse}{tuple}
Fred Draked1cc9c21998-01-21 04:55:02 +000065Construct a URL string from a tuple as returned by \code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000066This may result in a slightly different, but equivalent URL, if the
67URL that was parsed originally had redundant delimiters, e.g. a ? with
68an empty query (the draft states that these are equivalent).
69\end{funcdesc}
70
Fred Drake55452192001-11-16 03:22:15 +000071\begin{funcdesc}{urlsplit}{urlstring\optional{,
72 default_scheme\optional{, allow_fragments}}}
73This is similar to \function{urlparse()}, but does not split the
74params from the URL. This should generally be used instead of
75\function{urlparse()} if the more recent URL syntax allowing
76parameters to be applied to each segment of the \var{path} portion of
Walter Dörwaldff9ca5e2005-08-31 11:03:12 +000077the URL (see \rfc{2396}) is wanted. A separate function is needed to
78separate the path segments and parameters. This function returns a
795-tuple: (addressing scheme, network location, path, query, fragment
Fred Drake55452192001-11-16 03:22:15 +000080identifier).
81\versionadded{2.2}
82\end{funcdesc}
83
84\begin{funcdesc}{urlunsplit}{tuple}
85Combine the elements of a tuple as returned by \function{urlsplit()}
86into a complete URL as a string.
87\versionadded{2.2}
88\end{funcdesc}
89
Fred Drakecce10901998-03-17 06:33:25 +000090\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000091Construct a full (``absolute'') URL by combining a ``base URL''
92(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
93uses components of the base URL, in particular the addressing scheme,
94the network location and (part of) the path, to provide missing
95components in the relative URL.
96
97Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000098
Fred Drake19479911998-02-13 06:58:54 +000099\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +0000100urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Fred Drake19479911998-02-13 06:58:54 +0000101\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +0000102
Guido van Rossum96628a91995-04-10 11:34:00 +0000103yields the string
104
Fred Drake19479911998-02-13 06:58:54 +0000105\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +0000106'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +0000107\end{verbatim}
Fred Drake0308ff82000-08-25 17:29:35 +0000108
Guido van Rossuma12ef941995-02-27 17:53:25 +0000109The \var{allow_fragments} argument has the same meaning as for
Fred Draked1cc9c21998-01-21 04:55:02 +0000110\code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000111\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +0000112
Fred Drake98ef20d2002-10-16 20:07:54 +0000113\begin{funcdesc}{urldefrag}{url}
114If \var{url} contains a fragment identifier, returns a modified
115version of \var{url} with no fragment identifier, and the fragment
116identifier as a separate string. If there is no fragment identifier
117in \var{url}, returns \var{url} unmodified and an empty string.
118\end{funcdesc}
119
Fred Drake45ca3332000-08-24 04:58:25 +0000120
121\begin{seealso}
122 \seerfc{1738}{Uniform Resource Locators (URL)}{
123 This specifies the formal syntax and semantics of absolute
124 URLs.}
125 \seerfc{1808}{Relative Uniform Resource Locators}{
126 This Request For Comments includes the rules for joining an
Fred Drake5f2c1d22002-10-17 19:23:43 +0000127 absolute and a relative URL, including a fair number of
Fred Drake45ca3332000-08-24 04:58:25 +0000128 ``Abnormal Examples'' which govern the treatment of border
129 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +0000130 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
131 Document describing the generic syntactic requirements for
132 both Uniform Resource Names (URNs) and Uniform Resource
133 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000134\end{seealso}