<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>11.14 urlparse -- Parse URLs into components</title> <META NAME="description" CONTENT="11.14 urlparse -- Parse URLs into components"> <META NAME="keywords" CONTENT="lib"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="lib.css"> <link rel="first" href="lib.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="module-SocketServer.html"> <LINK REL="previous" href="module-telnetlib.html"> <LINK REL="up" href="internet.html"> <LINK REL="next" href="module-SocketServer.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="telnet-example.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="internet.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-SocketServer.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="telnet-example.html">11.13.2 Telnet Example</A> <b class="navlabel">Up:</b> <a class="sectref" href="internet.html">11. Internet Protocols and</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-SocketServer.html">11.15 SocketServer </A> <br><hr> </DIV> <!--End of Navigation Panel--> <H1><A NAME="SECTION00131400000000000000000"> 11.14 <tt class="module">urlparse</tt> -- Parse URLs into components</A> </H1> <P> <P> <a name="l2h-2623"> </a> <P> This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a ``relative URL'' to an absolute URL given a ``base URL.'' <P> The module has been designed to match the Internet RFC on Relative Uniform Resource Locators (and discovered a bug in an earlier draft!). <P> It defines the following functions: <P> <dl><dt><b><a name="l2h-2618"><tt class="function">urlparse</tt></a></b>(<var>urlstring</var><big>[</big><var>, default_scheme</var><big>[</big><var>, allow_fragments</var><big>]</big><big>]</big>) <dd> Parse a URL into 6 components, returning a 6-tuple: (addressing scheme, network location, path, parameters, query, fragment identifier). This corresponds to the general structure of a URL: <code><var>scheme</var>://<var>netloc</var>/<var>path</var>;<var>parameters</var>?<var>query</var>#<var>fragment</var></code>. Each tuple item is a string, possibly empty. The components are not broken up in smaller parts (e.g. the network location is a single string), and % escapes are not expanded. The delimiters as shown above are not part of the tuple items, except for a leading slash in the <var>path</var> component, which is retained if present. <P> Example: <P> <dl><dd><pre class="verbatim"> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') </pre></dl> <P> yields the tuple <P> <dl><dd><pre class="verbatim"> ('http', 'www.cwi.nl:80', '/%7Eguido/Python.html', '', '', '') </pre></dl> <P> If the <var>default_scheme</var> argument is specified, it gives the default addressing scheme, to be used only if the URL string does not specify one. The default value for this argument is the empty string. <P> If the <var>allow_fragments</var> argument is zero, fragment identifiers are not allowed, even if the URL's addressing scheme normally does support them. The default value for this argument is <code>1</code>. </dl> <P> <dl><dt><b><a name="l2h-2619"><tt class="function">urlunparse</tt></a></b>(<var>tuple</var>) <dd> Construct a URL string from a tuple as returned by <code>urlparse()</code>. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had redundant delimiters, e.g. a ? with an empty query (the draft states that these are equivalent). </dl> <P> <dl><dt><b><a name="l2h-2620"><tt class="function">urlsplit</tt></a></b>(<var>urlstring</var><big>[</big><var>, default_scheme</var><big>[</big><var>, allow_fragments</var><big>]</big><big>]</big>) <dd> This is similar to <tt class="function">urlparse()</tt>, but does not split the params from the URL. This should generally be used instead of <tt class="function">urlparse()</tt> if the more recent URL syntax allowing parameters to be applied to each segment of the <var>path</var> portion of the URL (see <a class="rfc" name="rfcref-63475" href="http://www.faqs.org/rfcs/rfc2396.html">RFC 2396</a>). A separate function is needed to separate the path segments and parameters. This function returns a 5-tuple: (addressing scheme, network location, path, query, fragment identifier). <span class="versionnote">New in version 2.2.</span> </dl> <P> <dl><dt><b><a name="l2h-2621"><tt class="function">urlunsplit</tt></a></b>(<var>tuple</var>) <dd> Combine the elements of a tuple as returned by <tt class="function">urlsplit()</tt> into a complete URL as a string. <span class="versionnote">New in version 2.2.</span> </dl> <P> <dl><dt><b><a name="l2h-2622"><tt class="function">urljoin</tt></a></b>(<var>base, url</var><big>[</big><var>, allow_fragments</var><big>]</big>) <dd> Construct a full (``absolute'') URL by combining a ``base URL'' (<var>base</var>) with a ``relative URL'' (<var>url</var>). Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL. <P> Example: <P> <dl><dd><pre class="verbatim"> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html') </pre></dl> <P> yields the string <P> <dl><dd><pre class="verbatim"> 'http://www.cwi.nl/%7Eguido/FAQ.html' </pre></dl> <P> The <var>allow_fragments</var> argument has the same meaning as for <code>urlparse()</code>. </dl> <P> <div class="seealso"> <p class="heading"><b>See Also:</b></p> <dl compact class="seerfc"> <dt><a href="http://www.faqs.org/rfcs/rfc1738.html" title="Uniform Resource Locators (URL)" >RFC 1738, <em>Uniform Resource Locators (URL)</em></a> <dd> This specifies the formal syntax and semantics of absolute URLs. </dl> <dl compact class="seerfc"> <dt><a href="http://www.faqs.org/rfcs/rfc1808.html" title="Relative Uniform Resource Locators" >RFC 1808, <em>Relative Uniform Resource Locators</em></a> <dd> This Request For Comments includes the rules for joining an absolute and a relative URL, including a fair normal of ``Abnormal Examples'' which govern the treatment of border cases. </dl> <dl compact class="seerfc"> <dt><a href="http://www.faqs.org/rfcs/rfc2396.html" title="Uniform Resource Identifiers (URI): Generic Syntax" >RFC 2396, <em>Uniform Resource Identifiers (URI): Generic Syntax</em></a> <dd> Document describing the generic syntactic requirements for both Uniform Resource Names (URNs) and Uniform Resource Locators (URLs). </dl> </div> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="telnet-example.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="internet.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-SocketServer.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="telnet-example.html">11.13.2 Telnet Example</A> <b class="navlabel">Up:</b> <a class="sectref" href="internet.html">11. Internet Protocols and</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-SocketServer.html">11.15 SocketServer </A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>