blob: 76622d5341ad9ed5d6f2dc2e5239a95b540d29e0 [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 Drakead5177c2006-04-01 22:14:43 +000028
Fred Drake23fd3d42006-04-01 06:11:07 +000029\versionadded[Support for the \code{sftp} and \code{sips} schemes]{2.5}
Guido van Rossuma12ef941995-02-27 17:53:25 +000030
Georg Brandl1de37002006-01-20 21:17:01 +000031The \module{urlparse} module defines the following functions:
Guido van Rossuma12ef941995-02-27 17:53:25 +000032
Fred Drakead5177c2006-04-01 22:14:43 +000033\begin{funcdesc}{urlparse}{urlstring\optional{,
34 default_scheme\optional{, allow_fragments}}}
35Parse a URL into six components, returning a 6-tuple. This
36corresponds to the general structure of a URL:
Guido van Rossuma12ef941995-02-27 17:53:25 +000037\code{\var{scheme}://\var{netloc}/\var{path};\var{parameters}?\var{query}\#\var{fragment}}.
38Each tuple item is a string, possibly empty.
Fred Drakead5177c2006-04-01 22:14:43 +000039The components are not broken up in smaller parts (for example, the network
Guido van Rossuma12ef941995-02-27 17:53:25 +000040location is a single string), and \% escapes are not expanded.
Fred Drakead5177c2006-04-01 22:14:43 +000041The delimiters as shown above are not part of the result,
Guido van Rossum470be141995-03-17 16:07:09 +000042except for a leading slash in the \var{path} component, which is
Fred Drakead5177c2006-04-01 22:14:43 +000043retained if present. For example:
Guido van Rossum96628a91995-04-10 11:34:00 +000044
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Fred Drakead5177c2006-04-01 22:14:43 +000046>>> from urlparse import urlparse
47>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
48>>> o
Guido van Rossum96628a91995-04-10 11:34:00 +000049('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '')
Fred Drakead5177c2006-04-01 22:14:43 +000050>>> o.scheme
51'http'
52>>> o.port
5380
54>>> o.geturl()
55'http://www.cwi.nl:80/%7Eguido/Python.html'
Fred Drake19479911998-02-13 06:58:54 +000056\end{verbatim}
Fred Drake45ca3332000-08-24 04:58:25 +000057
Guido van Rossuma12ef941995-02-27 17:53:25 +000058If the \var{default_scheme} argument is specified, it gives the
Fred Drakead5177c2006-04-01 22:14:43 +000059default addressing scheme, to be used only if the URL does not
Guido van Rossuma12ef941995-02-27 17:53:25 +000060specify one. The default value for this argument is the empty string.
61
Fred Drakead5177c2006-04-01 22:14:43 +000062If the \var{allow_fragments} argument is false, fragment identifiers
Guido van Rossuma12ef941995-02-27 17:53:25 +000063are not allowed, even if the URL's addressing scheme normally does
Fred Drakead5177c2006-04-01 22:14:43 +000064support them. The default value for this argument is \constant{True}.
65
66The return value is actually an instance of a subclass of
67\pytype{tuple}. This class has the following additional read-only
68convenience attributes:
69
70\begin{tableiv}{l|c|l|c}{member}{Attribute}{Index}{Value}{Value if not present}
71 \lineiv{scheme} {0} {URL scheme specifier} {empty string}
72 \lineiv{netloc} {1} {Network location part} {empty string}
73 \lineiv{path} {2} {Hierarchical path} {empty string}
74 \lineiv{params} {3} {Parameters for last path element} {empty string}
75 \lineiv{query} {4} {Query component} {empty string}
76 \lineiv{fragment}{5} {Fragment identifier} {empty string}
77 \lineiv{username}{ } {User name} {\constant{None}}
78 \lineiv{password}{ } {Password} {\constant{None}}
79 \lineiv{hostname}{ } {Host name (lower case)} {\constant{None}}
80 \lineiv{port} { } {Port number as integer, if present} {\constant{None}}
81\end{tableiv}
82
83See section~\ref{urlparse-result-object}, ``Results of
84\function{urlparse()} and \function{urlsplit()},'' for more
85information on the result object.
86
87\versionchanged[Added attributes to return value]{2.5}
Guido van Rossuma12ef941995-02-27 17:53:25 +000088\end{funcdesc}
89
Fred Drakead5177c2006-04-01 22:14:43 +000090\begin{funcdesc}{urlunparse}{parts}
91Construct a URL from a tuple as returned by \code{urlparse()}.
Andrew M. Kuchling96e60652006-12-20 19:58:18 +000092The \var{parts} argument can be any six-item iterable.
Guido van Rossuma12ef941995-02-27 17:53:25 +000093This may result in a slightly different, but equivalent URL, if the
Fred Drakead5177c2006-04-01 22:14:43 +000094URL that was parsed originally had unnecessary delimiters (for example,
95a ? with an empty query; the RFC states that these are equivalent).
Guido van Rossuma12ef941995-02-27 17:53:25 +000096\end{funcdesc}
97
Fred Drake55452192001-11-16 03:22:15 +000098\begin{funcdesc}{urlsplit}{urlstring\optional{,
99 default_scheme\optional{, allow_fragments}}}
100This is similar to \function{urlparse()}, but does not split the
101params from the URL. This should generally be used instead of
102\function{urlparse()} if the more recent URL syntax allowing
103parameters to be applied to each segment of the \var{path} portion of
Walter Dörwaldff9ca5e2005-08-31 11:03:12 +0000104the URL (see \rfc{2396}) is wanted. A separate function is needed to
105separate the path segments and parameters. This function returns a
1065-tuple: (addressing scheme, network location, path, query, fragment
Fred Drake55452192001-11-16 03:22:15 +0000107identifier).
Fred Drakead5177c2006-04-01 22:14:43 +0000108
109The return value is actually an instance of a subclass of
110\pytype{tuple}. This class has the following additional read-only
111convenience attributes:
112
113\begin{tableiv}{l|c|l|c}{member}{Attribute}{Index}{Value}{Value if not present}
114 \lineiv{scheme} {0} {URL scheme specifier} {empty string}
115 \lineiv{netloc} {1} {Network location part} {empty string}
116 \lineiv{path} {2} {Hierarchical path} {empty string}
117 \lineiv{query} {3} {Query component} {empty string}
118 \lineiv{fragment} {4} {Fragment identifier} {empty string}
119 \lineiv{username} { } {User name} {\constant{None}}
120 \lineiv{password} { } {Password} {\constant{None}}
121 \lineiv{hostname} { } {Host name (lower case)} {\constant{None}}
122 \lineiv{port} { } {Port number as integer, if present} {\constant{None}}
123\end{tableiv}
124
125See section~\ref{urlparse-result-object}, ``Results of
126\function{urlparse()} and \function{urlsplit()},'' for more
127information on the result object.
128
Fred Drake55452192001-11-16 03:22:15 +0000129\versionadded{2.2}
Fred Drakead5177c2006-04-01 22:14:43 +0000130\versionchanged[Added attributes to return value]{2.5}
Fred Drake55452192001-11-16 03:22:15 +0000131\end{funcdesc}
132
Fred Drakead5177c2006-04-01 22:14:43 +0000133\begin{funcdesc}{urlunsplit}{parts}
Fred Drake55452192001-11-16 03:22:15 +0000134Combine the elements of a tuple as returned by \function{urlsplit()}
135into a complete URL as a string.
Andrew M. Kuchling96e60652006-12-20 19:58:18 +0000136The \var{parts} argument can be any five-item iterable.
Fred Drakead5177c2006-04-01 22:14:43 +0000137This may result in a slightly different, but equivalent URL, if the
138URL that was parsed originally had unnecessary delimiters (for example,
139a ? with an empty query; the RFC states that these are equivalent).
Fred Drake55452192001-11-16 03:22:15 +0000140\versionadded{2.2}
141\end{funcdesc}
142
Fred Drakecce10901998-03-17 06:33:25 +0000143\begin{funcdesc}{urljoin}{base, url\optional{, allow_fragments}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000144Construct a full (``absolute'') URL by combining a ``base URL''
Georg Brandlb85509d2006-10-12 11:14:44 +0000145(\var{base}) with another URL (\var{url}). Informally, this
Guido van Rossuma12ef941995-02-27 17:53:25 +0000146uses components of the base URL, in particular the addressing scheme,
147the network location and (part of) the path, to provide missing
Fred Drakead5177c2006-04-01 22:14:43 +0000148components in the relative URL. For example:
Guido van Rossum96628a91995-04-10 11:34:00 +0000149
Fred Drake19479911998-02-13 06:58:54 +0000150\begin{verbatim}
Fred Drakead5177c2006-04-01 22:14:43 +0000151>>> from urlparse import urljoin
152>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
Guido van Rossum96628a91995-04-10 11:34:00 +0000153'http://www.cwi.nl/%7Eguido/FAQ.html'
Fred Drake19479911998-02-13 06:58:54 +0000154\end{verbatim}
Fred Drake0308ff82000-08-25 17:29:35 +0000155
Fred Drakead5177c2006-04-01 22:14:43 +0000156The \var{allow_fragments} argument has the same meaning and default as
157for \function{urlparse()}.
Georg Brandlb85509d2006-10-12 11:14:44 +0000158
159\note{If \var{url} is an absolute URL (that is, starting with \code{//}
160 or \code{scheme://}, the \var{url}'s host name and/or scheme
161 will be present in the result. For example:}
162
163\begin{verbatim}
164>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
165... '//www.python.org/%7Eguido')
166'http://www.python.org/%7Eguido'
167\end{verbatim}
168
169If you do not want that behavior, preprocess
170the \var{url} with \function{urlsplit()} and \function{urlunsplit()},
Georg Brandldfc29662007-03-08 17:49:17 +0000171removing possible \emph{scheme} and \emph{netloc} parts.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000172\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +0000173
Fred Drake98ef20d2002-10-16 20:07:54 +0000174\begin{funcdesc}{urldefrag}{url}
175If \var{url} contains a fragment identifier, returns a modified
176version of \var{url} with no fragment identifier, and the fragment
177identifier as a separate string. If there is no fragment identifier
178in \var{url}, returns \var{url} unmodified and an empty string.
179\end{funcdesc}
180
Fred Drake45ca3332000-08-24 04:58:25 +0000181
182\begin{seealso}
183 \seerfc{1738}{Uniform Resource Locators (URL)}{
184 This specifies the formal syntax and semantics of absolute
185 URLs.}
186 \seerfc{1808}{Relative Uniform Resource Locators}{
187 This Request For Comments includes the rules for joining an
Fred Drake5f2c1d22002-10-17 19:23:43 +0000188 absolute and a relative URL, including a fair number of
Fred Drake45ca3332000-08-24 04:58:25 +0000189 ``Abnormal Examples'' which govern the treatment of border
190 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +0000191 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
192 Document describing the generic syntactic requirements for
193 both Uniform Resource Names (URNs) and Uniform Resource
194 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000195\end{seealso}
Fred Drakead5177c2006-04-01 22:14:43 +0000196
197
198\subsection{Results of \function{urlparse()} and \function{urlsplit()}
199 \label{urlparse-result-object}}
200
201The result objects from the \function{urlparse()} and
202\function{urlsplit()} functions are subclasses of the \pytype{tuple}
203type. These subclasses add the attributes described in those
204functions, as well as provide an additional method:
205
206\begin{methoddesc}[ParseResult]{geturl}{}
207 Return the re-combined version of the original URL as a string.
208 This may differ from the original URL in that the scheme will always
209 be normalized to lower case and empty components may be dropped.
210 Specifically, empty parameters, queries, and fragment identifiers
211 will be removed.
212
213 The result of this method is a fixpoint if passed back through the
214 original parsing function:
215
216\begin{verbatim}
217>>> import urlparse
218>>> url = 'HTTP://www.Python.org/doc/#'
219
220>>> r1 = urlparse.urlsplit(url)
221>>> r1.geturl()
222'http://www.Python.org/doc/'
223
224>>> r2 = urlparse.urlsplit(r1.geturl())
225>>> r2.geturl()
226'http://www.Python.org/doc/'
227\end{verbatim}
228
229\versionadded{2.5}
230\end{methoddesc}
231
232The following classes provide the implementations of the parse results::
233
234\begin{classdesc*}{BaseResult}
235 Base class for the concrete result classes. This provides most of
236 the attribute definitions. It does not provide a \method{geturl()}
237 method. It is derived from \class{tuple}, but does not override the
238 \method{__init__()} or \method{__new__()} methods.
239\end{classdesc*}
240
241
242\begin{classdesc}{ParseResult}{scheme, netloc, path, params, query, fragment}
243 Concrete class for \function{urlparse()} results. The
244 \method{__new__()} method is overridden to support checking that the
245 right number of arguments are passed.
246\end{classdesc}
247
248
249\begin{classdesc}{SplitResult}{scheme, netloc, path, query, fragment}
250 Concrete class for \function{urlsplit()} results. The
251 \method{__new__()} method is overridden to support checking that the
252 right number of arguments are passed.
253\end{classdesc}