blob: 730d4688f532eab8a07bb621e9880a97a3ea90e0 [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},
Fred Drake23fd3d42006-04-01 06:11:07 +000026\code{sftp}, \code{shttp}, \code{sip}, \code{sips}, \code{snews}, \code{svn},
Georg Brandl1de37002006-01-20 21:17:01 +000027\code{svn+ssh}, \code{telnet}, \code{wais}.
Fred Drake23fd3d42006-04-01 06:11:07 +000028\versionadded[Support for the \code{sftp} and \code{sips} schemes]{2.5}
Guido van Rossuma12ef941995-02-27 17:53:25 +000029
Georg Brandl1de37002006-01-20 21:17:01 +000030The \module{urlparse} module defines the following functions:
Guido van Rossuma12ef941995-02-27 17:53:25 +000031
Fred Drake6884e3b1997-12-29 19:09:37 +000032\begin{funcdesc}{urlparse}{urlstring\optional{, default_scheme\optional{, allow_fragments}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000033Parse a URL into 6 components, returning a 6-tuple: (addressing
34scheme, network location, path, parameters, query, fragment
35identifier). This corresponds to the general structure of a URL:
36\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
37Each tuple item is a string, possibly empty.
38The components are not broken up in smaller parts (e.g. the network
39location is a single string), and \% escapes are not expanded.
Guido van Rossum470be141995-03-17 16:07:09 +000040The delimiters as shown above are not part of the tuple items,
41except for a leading slash in the \var{path} component, which is
42retained if present.
Guido van Rossuma12ef941995-02-27 17:53:25 +000043
44Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000045
Fred Drake19479911998-02-13 06:58:54 +000046\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000047urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
Fred Drake19479911998-02-13 06:58:54 +000048\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000049
Guido van Rossuma12ef941995-02-27 17:53:25 +000050yields the tuple
Guido van Rossum96628a91995-04-10 11:34:00 +000051
Fred Drake19479911998-02-13 06:58:54 +000052\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +000053('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
Fred Drake19479911998-02-13 06:58:54 +000054\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000055
Guido van Rossuma12ef941995-02-27 17:53:25 +000056If the \var{default_scheme} argument is specified, it gives the
57default addressing scheme, to be used only if the URL string does not
58specify one. The default value for this argument is the empty string.
59
60If the \var{allow_fragments} argument is zero, fragment identifiers
61are not allowed, even if the URL's addressing scheme normally does
62support them. The default value for this argument is \code{1}.
63\end{funcdesc}
64
65\begin{funcdesc}{urlunparse}{tuple}
Fred Draked1cc9c21998-01-21 04:55:02 +000066Construct a URL string from a tuple as returned by \code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000067This may result in a slightly different, but equivalent URL, if the
68URL that was parsed originally had redundant delimiters, e.g. a ? with
69an empty query (the draft states that these are equivalent).
70\end{funcdesc}
71
Fred Drake55452192001-11-16 03:22:15 +000072\begin{funcdesc}{urlsplit}{urlstring\optional{,
73 default_scheme\optional{, allow_fragments}}}
74This is similar to \function{urlparse()}, but does not split the
75params from the URL. This should generally be used instead of
76\function{urlparse()} if the more recent URL syntax allowing
77parameters to be applied to each segment of the \var{path} portion of
Walter Dörwaldff9ca5e2005-08-31 11:03:12 +000078the URL (see \rfc{2396}) is wanted. A separate function is needed to
79separate the path segments and parameters. This function returns a
805-tuple: (addressing scheme, network location, path, query, fragment
Fred Drake55452192001-11-16 03:22:15 +000081identifier).
82\versionadded{2.2}
83\end{funcdesc}
84
85\begin{funcdesc}{urlunsplit}{tuple}
86Combine the elements of a tuple as returned by \function{urlsplit()}
87into a complete URL as a string.
88\versionadded{2.2}
89\end{funcdesc}
90
Fred Drakecce10901998-03-17 06:33:25 +000091\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000092Construct a full (``absolute'') URL by combining a ``base URL''
93(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
94uses components of the base URL, in particular the addressing scheme,
95the network location and (part of) the path, to provide missing
96components in the relative URL.
97
98Example:
Guido van Rossum96628a91995-04-10 11:34:00 +000099
Fred Drake19479911998-02-13 06:58:54 +0000100\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +0000101urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Fred Drake19479911998-02-13 06:58:54 +0000102\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +0000103
Guido van Rossum96628a91995-04-10 11:34:00 +0000104yields the string
105
Fred Drake19479911998-02-13 06:58:54 +0000106\begin{verbatim}
Guido van Rossum96628a91995-04-10 11:34:00 +0000107'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +0000108\end{verbatim}
Fred Drake0308ff82000-08-25 17:29:35 +0000109
Guido van Rossuma12ef941995-02-27 17:53:25 +0000110The \var{allow_fragments} argument has the same meaning as for
Fred Draked1cc9c21998-01-21 04:55:02 +0000111\code{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000112\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +0000113
Fred Drake98ef20d2002-10-16 20:07:54 +0000114\begin{funcdesc}{urldefrag}{url}
115If \var{url} contains a fragment identifier, returns a modified
116version of \var{url} with no fragment identifier, and the fragment
117identifier as a separate string. If there is no fragment identifier
118in \var{url}, returns \var{url} unmodified and an empty string.
119\end{funcdesc}
120
Fred Drake45ca3332000-08-24 04:58:25 +0000121
122\begin{seealso}
123 \seerfc{1738}{Uniform Resource Locators (URL)}{
124 This specifies the formal syntax and semantics of absolute
125 URLs.}
126 \seerfc{1808}{Relative Uniform Resource Locators}{
127 This Request For Comments includes the rules for joining an
Fred Drake5f2c1d22002-10-17 19:23:43 +0000128 absolute and a relative URL, including a fair number of
Fred Drake45ca3332000-08-24 04:58:25 +0000129 ``Abnormal Examples'' which govern the treatment of border
130 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +0000131 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
132 Document describing the generic syntactic requirements for
133 both Uniform Resource Names (URNs) and Uniform Resource
134 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000135\end{seealso}