blob: f18efe9cf6eb455a713b144d74ac299668f099a3 [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},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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}.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028
29\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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043retained if present. For example:
Guido van Rossum96628a91995-04-10 11:34:00 +000044
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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', '', '', '')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090\begin{funcdesc}{urlunparse}{parts}
91Construct a URL from a tuple as returned by \code{urlparse()}.
92The \var{parts} argument 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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130\versionchanged[Added attributes to return value]{2.5}
Fred Drake55452192001-11-16 03:22:15 +0000131\end{funcdesc}
132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136The \var{parts} argument be any five-item iterable.
137This 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''
145(\var{base}) with a ``relative URL'' (\var{url}). Informally, this
146uses components of the base URL, in particular the addressing scheme,
147the network location and (part of) the path, to provide missing
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156The \var{allow_fragments} argument has the same meaning and default as
157for \function{urlparse()}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000158\end{funcdesc}
Fred Drake45ca3332000-08-24 04:58:25 +0000159
Fred Drake98ef20d2002-10-16 20:07:54 +0000160\begin{funcdesc}{urldefrag}{url}
161If \var{url} contains a fragment identifier, returns a modified
162version of \var{url} with no fragment identifier, and the fragment
163identifier as a separate string. If there is no fragment identifier
164in \var{url}, returns \var{url} unmodified and an empty string.
165\end{funcdesc}
166
Fred Drake45ca3332000-08-24 04:58:25 +0000167
168\begin{seealso}
169 \seerfc{1738}{Uniform Resource Locators (URL)}{
170 This specifies the formal syntax and semantics of absolute
171 URLs.}
172 \seerfc{1808}{Relative Uniform Resource Locators}{
173 This Request For Comments includes the rules for joining an
Fred Drake5f2c1d22002-10-17 19:23:43 +0000174 absolute and a relative URL, including a fair number of
Fred Drake45ca3332000-08-24 04:58:25 +0000175 ``Abnormal Examples'' which govern the treatment of border
176 cases.}
Fred Drake0308ff82000-08-25 17:29:35 +0000177 \seerfc{2396}{Uniform Resource Identifiers (URI): Generic Syntax}{
178 Document describing the generic syntactic requirements for
179 both Uniform Resource Names (URNs) and Uniform Resource
180 Locators (URLs).}
Fred Drake45ca3332000-08-24 04:58:25 +0000181\end{seealso}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182
183
184\subsection{Results of \function{urlparse()} and \function{urlsplit()}
185 \label{urlparse-result-object}}
186
187The result objects from the \function{urlparse()} and
188\function{urlsplit()} functions are subclasses of the \pytype{tuple}
189type. These subclasses add the attributes described in those
190functions, as well as provide an additional method:
191
192\begin{methoddesc}[ParseResult]{geturl}{}
193 Return the re-combined version of the original URL as a string.
194 This may differ from the original URL in that the scheme will always
195 be normalized to lower case and empty components may be dropped.
196 Specifically, empty parameters, queries, and fragment identifiers
197 will be removed.
198
199 The result of this method is a fixpoint if passed back through the
200 original parsing function:
201
202\begin{verbatim}
203>>> import urlparse
204>>> url = 'HTTP://www.Python.org/doc/#'
205
206>>> r1 = urlparse.urlsplit(url)
207>>> r1.geturl()
208'http://www.Python.org/doc/'
209
210>>> r2 = urlparse.urlsplit(r1.geturl())
211>>> r2.geturl()
212'http://www.Python.org/doc/'
213\end{verbatim}
214
215\versionadded{2.5}
216\end{methoddesc}
217
218The following classes provide the implementations of the parse results::
219
220\begin{classdesc*}{BaseResult}
221 Base class for the concrete result classes. This provides most of
222 the attribute definitions. It does not provide a \method{geturl()}
223 method. It is derived from \class{tuple}, but does not override the
224 \method{__init__()} or \method{__new__()} methods.
225\end{classdesc*}
226
227
228\begin{classdesc}{ParseResult}{scheme, netloc, path, params, query, fragment}
229 Concrete class for \function{urlparse()} results. The
230 \method{__new__()} method is overridden to support checking that the
231 right number of arguments are passed.
232\end{classdesc}
233
234
235\begin{classdesc}{SplitResult}{scheme, netloc, path, query, fragment}
236 Concrete class for \function{urlsplit()} results. The
237 \method{__new__()} method is overridden to support checking that the
238 right number of arguments are passed.
239\end{classdesc}