Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 1 | :mod:`urllib.parse` --- Parse URLs into components |
| 2 | ================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 4 | .. module:: urllib.parse |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: Parse URLs into or assemble them from components. |
| 6 | |
| 7 | |
| 8 | .. index:: |
| 9 | single: WWW |
| 10 | single: World Wide Web |
| 11 | single: URL |
| 12 | pair: URL; parsing |
| 13 | pair: relative; URL |
| 14 | |
| 15 | This module defines a standard interface to break Uniform Resource Locator (URL) |
| 16 | strings up in components (addressing scheme, network location, path etc.), to |
| 17 | combine the components back into a URL string, and to convert a "relative URL" |
| 18 | to an absolute URL given a "base URL." |
| 19 | |
| 20 | The module has been designed to match the Internet RFC on Relative Uniform |
| 21 | Resource Locators (and discovered a bug in an earlier draft!). It supports the |
| 22 | following URL schemes: ``file``, ``ftp``, ``gopher``, ``hdl``, ``http``, |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 23 | ``https``, ``imap``, ``mailto``, ``mms``, ``news``, ``nntp``, ``prospero``, |
| 24 | ``rsync``, ``rtsp``, ``rtspu``, ``sftp``, ``shttp``, ``sip``, ``sips``, |
| 25 | ``snews``, ``svn``, ``svn+ssh``, ``telnet``, ``wais``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 27 | The :mod:`urllib.parse` module defines functions that fall into two broad |
| 28 | categories: URL parsing and URL quoting. These are covered in detail in |
| 29 | the following sections. |
| 30 | |
| 31 | URL Parsing |
| 32 | ----------- |
| 33 | |
| 34 | The URL parsing functions focus on splitting a URL string into its components, |
| 35 | or on combining URL components into a URL string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
R. David Murray | f5077aa | 2010-05-25 15:36:46 +0000 | [diff] [blame] | 37 | .. function:: urlparse(urlstring, scheme='', allow_fragments=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | |
| 39 | Parse a URL into six components, returning a 6-tuple. This corresponds to the |
| 40 | general structure of a URL: ``scheme://netloc/path;parameters?query#fragment``. |
| 41 | Each tuple item is a string, possibly empty. The components are not broken up in |
| 42 | smaller parts (for example, the network location is a single string), and % |
| 43 | escapes are not expanded. The delimiters as shown above are not part of the |
| 44 | result, except for a leading slash in the *path* component, which is retained if |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 45 | present. For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 47 | >>> from urllib.parse import urlparse |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 49 | >>> o # doctest: +NORMALIZE_WHITESPACE |
| 50 | ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', |
| 51 | params='', query='', fragment='') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | >>> o.scheme |
| 53 | 'http' |
| 54 | >>> o.port |
| 55 | 80 |
| 56 | >>> o.geturl() |
| 57 | 'http://www.cwi.nl:80/%7Eguido/Python.html' |
| 58 | |
Senthil Kumaran | 7089a4e | 2010-11-07 12:57:04 +0000 | [diff] [blame] | 59 | Following the syntax specifications in :rfc:`1808`, urlparse recognizes |
| 60 | a netloc only if it is properly introduced by '//'. Otherwise the |
| 61 | input is presumed to be a relative URL and thus to start with |
| 62 | a path component. |
Senthil Kumaran | 84c7d9f | 2010-08-04 04:50:44 +0000 | [diff] [blame] | 63 | |
| 64 | >>> from urlparse import urlparse |
| 65 | >>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html') |
| 66 | ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', |
| 67 | params='', query='', fragment='') |
| 68 | >>> urlparse('www.cwi.nl:80/%7Eguido/Python.html') |
| 69 | ParseResult(scheme='', netloc='', path='www.cwi.nl:80/%7Eguido/Python.html', |
| 70 | params='', query='', fragment='') |
| 71 | >>> urlparse('help/Python.html') |
| 72 | ParseResult(scheme='', netloc='', path='help/Python.html', params='', |
| 73 | query='', fragment='') |
| 74 | |
R. David Murray | f5077aa | 2010-05-25 15:36:46 +0000 | [diff] [blame] | 75 | If the *scheme* argument is specified, it gives the default addressing |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | scheme, to be used only if the URL does not specify one. The default value for |
| 77 | this argument is the empty string. |
| 78 | |
| 79 | If the *allow_fragments* argument is false, fragment identifiers are not |
| 80 | allowed, even if the URL's addressing scheme normally does support them. The |
| 81 | default value for this argument is :const:`True`. |
| 82 | |
| 83 | The return value is actually an instance of a subclass of :class:`tuple`. This |
| 84 | class has the following additional read-only convenience attributes: |
| 85 | |
| 86 | +------------------+-------+--------------------------+----------------------+ |
| 87 | | Attribute | Index | Value | Value if not present | |
| 88 | +==================+=======+==========================+======================+ |
| 89 | | :attr:`scheme` | 0 | URL scheme specifier | empty string | |
| 90 | +------------------+-------+--------------------------+----------------------+ |
| 91 | | :attr:`netloc` | 1 | Network location part | empty string | |
| 92 | +------------------+-------+--------------------------+----------------------+ |
| 93 | | :attr:`path` | 2 | Hierarchical path | empty string | |
| 94 | +------------------+-------+--------------------------+----------------------+ |
| 95 | | :attr:`params` | 3 | Parameters for last path | empty string | |
| 96 | | | | element | | |
| 97 | +------------------+-------+--------------------------+----------------------+ |
| 98 | | :attr:`query` | 4 | Query component | empty string | |
| 99 | +------------------+-------+--------------------------+----------------------+ |
| 100 | | :attr:`fragment` | 5 | Fragment identifier | empty string | |
| 101 | +------------------+-------+--------------------------+----------------------+ |
| 102 | | :attr:`username` | | User name | :const:`None` | |
| 103 | +------------------+-------+--------------------------+----------------------+ |
| 104 | | :attr:`password` | | Password | :const:`None` | |
| 105 | +------------------+-------+--------------------------+----------------------+ |
| 106 | | :attr:`hostname` | | Host name (lower case) | :const:`None` | |
| 107 | +------------------+-------+--------------------------+----------------------+ |
| 108 | | :attr:`port` | | Port number as integer, | :const:`None` | |
| 109 | | | | if present | | |
| 110 | +------------------+-------+--------------------------+----------------------+ |
| 111 | |
| 112 | See section :ref:`urlparse-result-object` for more information on the result |
| 113 | object. |
| 114 | |
Senthil Kumaran | 7a1e09f | 2010-04-22 12:19:46 +0000 | [diff] [blame] | 115 | .. versionchanged:: 3.2 |
| 116 | Added IPv6 URL parsing capabilities. |
| 117 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 119 | .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False) |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 120 | |
| 121 | Parse a query string given as a string argument (data of type |
| 122 | :mimetype:`application/x-www-form-urlencoded`). Data are returned as a |
| 123 | dictionary. The dictionary keys are the unique query variable names and the |
| 124 | values are lists of values for each name. |
| 125 | |
| 126 | The optional argument *keep_blank_values* is a flag indicating whether blank |
Senthil Kumaran | f0769e8 | 2010-08-09 19:53:52 +0000 | [diff] [blame] | 127 | values in percent-encoded queries should be treated as blank strings. A true value |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 128 | indicates that blanks should be retained as blank strings. The default false |
| 129 | value indicates that blank values are to be ignored and treated as if they were |
| 130 | not included. |
| 131 | |
| 132 | The optional argument *strict_parsing* is a flag indicating what to do with |
| 133 | parsing errors. If false (the default), errors are silently ignored. If true, |
| 134 | errors raise a :exc:`ValueError` exception. |
| 135 | |
Georg Brandl | 7fe2c4a | 2008-12-05 07:32:56 +0000 | [diff] [blame] | 136 | Use the :func:`urllib.parse.urlencode` function to convert such |
| 137 | dictionaries into query strings. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 138 | |
| 139 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 140 | .. function:: parse_qsl(qs, keep_blank_values=False, strict_parsing=False) |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 141 | |
| 142 | Parse a query string given as a string argument (data of type |
| 143 | :mimetype:`application/x-www-form-urlencoded`). Data are returned as a list of |
| 144 | name, value pairs. |
| 145 | |
| 146 | The optional argument *keep_blank_values* is a flag indicating whether blank |
Senthil Kumaran | f0769e8 | 2010-08-09 19:53:52 +0000 | [diff] [blame] | 147 | values in percent-encoded queries should be treated as blank strings. A true value |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 148 | indicates that blanks should be retained as blank strings. The default false |
| 149 | value indicates that blank values are to be ignored and treated as if they were |
| 150 | not included. |
| 151 | |
| 152 | The optional argument *strict_parsing* is a flag indicating what to do with |
| 153 | parsing errors. If false (the default), errors are silently ignored. If true, |
| 154 | errors raise a :exc:`ValueError` exception. |
| 155 | |
| 156 | Use the :func:`urllib.parse.urlencode` function to convert such lists of pairs into |
| 157 | query strings. |
| 158 | |
| 159 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | .. function:: urlunparse(parts) |
| 161 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 162 | Construct a URL from a tuple as returned by ``urlparse()``. The *parts* |
| 163 | argument can be any six-item iterable. This may result in a slightly |
| 164 | different, but equivalent URL, if the URL that was parsed originally had |
| 165 | unnecessary delimiters (for example, a ``?`` with an empty query; the RFC |
| 166 | states that these are equivalent). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
| 168 | |
R. David Murray | f5077aa | 2010-05-25 15:36:46 +0000 | [diff] [blame] | 169 | .. function:: urlsplit(urlstring, scheme='', allow_fragments=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | This is similar to :func:`urlparse`, but does not split the params from the URL. |
| 172 | This should generally be used instead of :func:`urlparse` if the more recent URL |
| 173 | syntax allowing parameters to be applied to each segment of the *path* portion |
| 174 | of the URL (see :rfc:`2396`) is wanted. A separate function is needed to |
| 175 | separate the path segments and parameters. This function returns a 5-tuple: |
| 176 | (addressing scheme, network location, path, query, fragment identifier). |
| 177 | |
| 178 | The return value is actually an instance of a subclass of :class:`tuple`. This |
| 179 | class has the following additional read-only convenience attributes: |
| 180 | |
| 181 | +------------------+-------+-------------------------+----------------------+ |
| 182 | | Attribute | Index | Value | Value if not present | |
| 183 | +==================+=======+=========================+======================+ |
| 184 | | :attr:`scheme` | 0 | URL scheme specifier | empty string | |
| 185 | +------------------+-------+-------------------------+----------------------+ |
| 186 | | :attr:`netloc` | 1 | Network location part | empty string | |
| 187 | +------------------+-------+-------------------------+----------------------+ |
| 188 | | :attr:`path` | 2 | Hierarchical path | empty string | |
| 189 | +------------------+-------+-------------------------+----------------------+ |
| 190 | | :attr:`query` | 3 | Query component | empty string | |
| 191 | +------------------+-------+-------------------------+----------------------+ |
| 192 | | :attr:`fragment` | 4 | Fragment identifier | empty string | |
| 193 | +------------------+-------+-------------------------+----------------------+ |
| 194 | | :attr:`username` | | User name | :const:`None` | |
| 195 | +------------------+-------+-------------------------+----------------------+ |
| 196 | | :attr:`password` | | Password | :const:`None` | |
| 197 | +------------------+-------+-------------------------+----------------------+ |
| 198 | | :attr:`hostname` | | Host name (lower case) | :const:`None` | |
| 199 | +------------------+-------+-------------------------+----------------------+ |
| 200 | | :attr:`port` | | Port number as integer, | :const:`None` | |
| 201 | | | | if present | | |
| 202 | +------------------+-------+-------------------------+----------------------+ |
| 203 | |
| 204 | See section :ref:`urlparse-result-object` for more information on the result |
| 205 | object. |
| 206 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
| 208 | .. function:: urlunsplit(parts) |
| 209 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 210 | Combine the elements of a tuple as returned by :func:`urlsplit` into a |
| 211 | complete URL as a string. The *parts* argument can be any five-item |
| 212 | iterable. This may result in a slightly different, but equivalent URL, if the |
| 213 | URL that was parsed originally had unnecessary delimiters (for example, a ? |
| 214 | with an empty query; the RFC states that these are equivalent). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 217 | .. function:: urljoin(base, url, allow_fragments=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
| 219 | Construct a full ("absolute") URL by combining a "base URL" (*base*) with |
| 220 | another URL (*url*). Informally, this uses components of the base URL, in |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 221 | particular the addressing scheme, the network location and (part of) the |
| 222 | path, to provide missing components in the relative URL. For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 224 | >>> from urllib.parse import urljoin |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html') |
| 226 | 'http://www.cwi.nl/%7Eguido/FAQ.html' |
| 227 | |
| 228 | The *allow_fragments* argument has the same meaning and default as for |
| 229 | :func:`urlparse`. |
| 230 | |
| 231 | .. note:: |
| 232 | |
| 233 | If *url* is an absolute URL (that is, starting with ``//`` or ``scheme://``), |
| 234 | the *url*'s host name and/or scheme will be present in the result. For example: |
| 235 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 236 | .. doctest:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
| 238 | >>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', |
| 239 | ... '//www.python.org/%7Eguido') |
| 240 | 'http://www.python.org/%7Eguido' |
| 241 | |
| 242 | If you do not want that behavior, preprocess the *url* with :func:`urlsplit` and |
| 243 | :func:`urlunsplit`, removing possible *scheme* and *netloc* parts. |
| 244 | |
| 245 | |
| 246 | .. function:: urldefrag(url) |
| 247 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 248 | If *url* contains a fragment identifier, return a modified version of *url* |
| 249 | with no fragment identifier, and the fragment identifier as a separate |
| 250 | string. If there is no fragment identifier in *url*, return *url* unmodified |
| 251 | and an empty string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 253 | The return value is actually an instance of a subclass of :class:`tuple`. This |
| 254 | class has the following additional read-only convenience attributes: |
| 255 | |
| 256 | +------------------+-------+-------------------------+----------------------+ |
| 257 | | Attribute | Index | Value | Value if not present | |
| 258 | +==================+=======+=========================+======================+ |
| 259 | | :attr:`url` | 0 | URL with no fragment | empty string | |
| 260 | +------------------+-------+-------------------------+----------------------+ |
| 261 | | :attr:`fragment` | 1 | Fragment identifier | empty string | |
| 262 | +------------------+-------+-------------------------+----------------------+ |
| 263 | |
| 264 | See section :ref:`urlparse-result-object` for more information on the result |
| 265 | object. |
| 266 | |
| 267 | .. versionchanged:: 3.2 |
| 268 | Result is a structured object rather than a simple 2-tuple |
| 269 | |
| 270 | |
| 271 | Parsing ASCII Encoded Bytes |
| 272 | --------------------------- |
| 273 | |
| 274 | The URL parsing functions were originally designed to operate on character |
| 275 | strings only. In practice, it is useful to be able to manipulate properly |
| 276 | quoted and encoded URLs as sequences of ASCII bytes. Accordingly, the |
| 277 | URL parsing functions in this module all operate on :class:`bytes` and |
| 278 | :class:`bytearray` objects in addition to :class:`str` objects. |
| 279 | |
| 280 | If :class:`str` data is passed in, the result will also contain only |
| 281 | :class:`str` data. If :class:`bytes` or :class:`bytearray` data is |
| 282 | passed in, the result will contain only :class:`bytes` data. |
| 283 | |
| 284 | Attempting to mix :class:`str` data with :class:`bytes` or |
| 285 | :class:`bytearray` in a single function call will result in a |
Éric Araujo | ff2a4ba | 2010-11-30 17:20:31 +0000 | [diff] [blame] | 286 | :exc:`TypeError` being raised, while attempting to pass in non-ASCII |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 287 | byte values will trigger :exc:`UnicodeDecodeError`. |
| 288 | |
| 289 | To support easier conversion of result objects between :class:`str` and |
| 290 | :class:`bytes`, all return values from URL parsing functions provide |
| 291 | either an :meth:`encode` method (when the result contains :class:`str` |
| 292 | data) or a :meth:`decode` method (when the result contains :class:`bytes` |
| 293 | data). The signatures of these methods match those of the corresponding |
| 294 | :class:`str` and :class:`bytes` methods (except that the default encoding |
| 295 | is ``'ascii'`` rather than ``'utf-8'``). Each produces a value of a |
| 296 | corresponding type that contains either :class:`bytes` data (for |
| 297 | :meth:`encode` methods) or :class:`str` data (for |
| 298 | :meth:`decode` methods). |
| 299 | |
| 300 | Applications that need to operate on potentially improperly quoted URLs |
| 301 | that may contain non-ASCII data will need to do their own decoding from |
| 302 | bytes to characters before invoking the URL parsing methods. |
| 303 | |
| 304 | The behaviour described in this section applies only to the URL parsing |
| 305 | functions. The URL quoting functions use their own rules when producing |
| 306 | or consuming byte sequences as detailed in the documentation of the |
| 307 | individual URL quoting functions. |
| 308 | |
| 309 | .. versionchanged:: 3.2 |
| 310 | URL parsing functions now accept ASCII encoded byte sequences |
| 311 | |
| 312 | |
| 313 | .. _urlparse-result-object: |
| 314 | |
| 315 | Structured Parse Results |
| 316 | ------------------------ |
| 317 | |
| 318 | The result objects from the :func:`urlparse`, :func:`urlsplit` and |
Georg Brandl | 4640237 | 2010-12-04 19:06:18 +0000 | [diff] [blame] | 319 | :func:`urldefrag` functions are subclasses of the :class:`tuple` type. |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 320 | These subclasses add the attributes listed in the documentation for |
| 321 | those functions, the encoding and decoding support described in the |
| 322 | previous section, as well as an additional method: |
| 323 | |
| 324 | .. method:: urllib.parse.SplitResult.geturl() |
| 325 | |
| 326 | Return the re-combined version of the original URL as a string. This may |
| 327 | differ from the original URL in that the scheme may be normalized to lower |
| 328 | case and empty components may be dropped. Specifically, empty parameters, |
| 329 | queries, and fragment identifiers will be removed. |
| 330 | |
| 331 | For :func:`urldefrag` results, only empty fragment identifiers will be removed. |
| 332 | For :func:`urlsplit` and :func:`urlparse` results, all noted changes will be |
| 333 | made to the URL returned by this method. |
| 334 | |
| 335 | The result of this method remains unchanged if passed back through the original |
| 336 | parsing function: |
| 337 | |
| 338 | >>> from urllib.parse import urlsplit |
| 339 | >>> url = 'HTTP://www.Python.org/doc/#' |
| 340 | >>> r1 = urlsplit(url) |
| 341 | >>> r1.geturl() |
| 342 | 'http://www.Python.org/doc/' |
| 343 | >>> r2 = urlsplit(r1.geturl()) |
| 344 | >>> r2.geturl() |
| 345 | 'http://www.Python.org/doc/' |
| 346 | |
| 347 | |
| 348 | The following classes provide the implementations of the structured parse |
| 349 | results when operating on :class:`str` objects: |
| 350 | |
| 351 | .. class:: DefragResult(url, fragment) |
| 352 | |
| 353 | Concrete class for :func:`urldefrag` results containing :class:`str` |
| 354 | data. The :meth:`encode` method returns a :class:`DefragResultBytes` |
| 355 | instance. |
| 356 | |
| 357 | .. versionadded:: 3.2 |
| 358 | |
| 359 | .. class:: ParseResult(scheme, netloc, path, params, query, fragment) |
| 360 | |
| 361 | Concrete class for :func:`urlparse` results containing :class:`str` |
| 362 | data. The :meth:`encode` method returns a :class:`ParseResultBytes` |
| 363 | instance. |
| 364 | |
| 365 | .. class:: SplitResult(scheme, netloc, path, query, fragment) |
| 366 | |
| 367 | Concrete class for :func:`urlsplit` results containing :class:`str` |
| 368 | data. The :meth:`encode` method returns a :class:`SplitResultBytes` |
| 369 | instance. |
| 370 | |
| 371 | |
| 372 | The following classes provide the implementations of the parse results when |
| 373 | operating on :class:`bytes` or :class:`bytearray` objects: |
| 374 | |
| 375 | .. class:: DefragResultBytes(url, fragment) |
| 376 | |
| 377 | Concrete class for :func:`urldefrag` results containing :class:`bytes` |
| 378 | data. The :meth:`decode` method returns a :class:`DefragResult` |
| 379 | instance. |
| 380 | |
| 381 | .. versionadded:: 3.2 |
| 382 | |
| 383 | .. class:: ParseResultBytes(scheme, netloc, path, params, query, fragment) |
| 384 | |
| 385 | Concrete class for :func:`urlparse` results containing :class:`bytes` |
| 386 | data. The :meth:`decode` method returns a :class:`ParseResult` |
| 387 | instance. |
| 388 | |
| 389 | .. versionadded:: 3.2 |
| 390 | |
| 391 | .. class:: SplitResultBytes(scheme, netloc, path, query, fragment) |
| 392 | |
| 393 | Concrete class for :func:`urlsplit` results containing :class:`bytes` |
| 394 | data. The :meth:`decode` method returns a :class:`SplitResult` |
| 395 | instance. |
| 396 | |
| 397 | .. versionadded:: 3.2 |
| 398 | |
| 399 | |
| 400 | URL Quoting |
| 401 | ----------- |
| 402 | |
| 403 | The URL quoting functions focus on taking program data and making it safe |
| 404 | for use as URL components by quoting special characters and appropriately |
| 405 | encoding non-ASCII text. They also support reversing these operations to |
| 406 | recreate the original data from the contents of a URL component if that |
| 407 | task isn't already covered by the URL parsing functions above. |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 408 | |
| 409 | .. function:: quote(string, safe='/', encoding=None, errors=None) |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 410 | |
| 411 | Replace special characters in *string* using the ``%xx`` escape. Letters, |
Senthil Kumaran | 8aa8bbe | 2009-08-31 16:43:45 +0000 | [diff] [blame] | 412 | digits, and the characters ``'_.-'`` are never quoted. By default, this |
| 413 | function is intended for quoting the path section of URL. The optional *safe* |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 414 | parameter specifies additional ASCII characters that should not be quoted |
| 415 | --- its default value is ``'/'``. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 417 | *string* may be either a :class:`str` or a :class:`bytes`. |
| 418 | |
| 419 | The optional *encoding* and *errors* parameters specify how to deal with |
| 420 | non-ASCII characters, as accepted by the :meth:`str.encode` method. |
| 421 | *encoding* defaults to ``'utf-8'``. |
| 422 | *errors* defaults to ``'strict'``, meaning unsupported characters raise a |
| 423 | :class:`UnicodeEncodeError`. |
| 424 | *encoding* and *errors* must not be supplied if *string* is a |
| 425 | :class:`bytes`, or a :class:`TypeError` is raised. |
| 426 | |
| 427 | Note that ``quote(string, safe, encoding, errors)`` is equivalent to |
| 428 | ``quote_from_bytes(string.encode(encoding, errors), safe)``. |
| 429 | |
| 430 | Example: ``quote('/El Niño/')`` yields ``'/El%20Ni%C3%B1o/'``. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 431 | |
| 432 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 433 | .. function:: quote_plus(string, safe='', encoding=None, errors=None) |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 434 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 435 | Like :func:`quote`, but also replace spaces by plus signs, as required for |
Georg Brandl | 81c09db | 2009-07-29 07:27:08 +0000 | [diff] [blame] | 436 | quoting HTML form values when building up a query string to go into a URL. |
| 437 | Plus signs in the original string are escaped unless they are included in |
| 438 | *safe*. It also does not have *safe* default to ``'/'``. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 440 | Example: ``quote_plus('/El Niño/')`` yields ``'%2FEl+Ni%C3%B1o%2F'``. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 441 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 442 | |
| 443 | .. function:: quote_from_bytes(bytes, safe='/') |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 444 | |
| 445 | Like :func:`quote`, but accepts a :class:`bytes` object rather than a |
| 446 | :class:`str`, and does not perform string-to-bytes encoding. |
| 447 | |
| 448 | Example: ``quote_from_bytes(b'a&\xef')`` yields |
| 449 | ``'a%26%EF'``. |
| 450 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 451 | |
| 452 | .. function:: unquote(string, encoding='utf-8', errors='replace') |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 453 | |
| 454 | Replace ``%xx`` escapes by their single-character equivalent. |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 455 | The optional *encoding* and *errors* parameters specify how to decode |
| 456 | percent-encoded sequences into Unicode characters, as accepted by the |
| 457 | :meth:`bytes.decode` method. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 459 | *string* must be a :class:`str`. |
| 460 | |
| 461 | *encoding* defaults to ``'utf-8'``. |
| 462 | *errors* defaults to ``'replace'``, meaning invalid sequences are replaced |
| 463 | by a placeholder character. |
| 464 | |
| 465 | Example: ``unquote('/El%20Ni%C3%B1o/')`` yields ``'/El Niño/'``. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 466 | |
| 467 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 468 | .. function:: unquote_plus(string, encoding='utf-8', errors='replace') |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 469 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 470 | Like :func:`unquote`, but also replace plus signs by spaces, as required for |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 471 | unquoting HTML form values. |
| 472 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 473 | *string* must be a :class:`str`. |
| 474 | |
| 475 | Example: ``unquote_plus('/El+Ni%C3%B1o/')`` yields ``'/El Niño/'``. |
| 476 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 478 | .. function:: unquote_to_bytes(string) |
| 479 | |
| 480 | Replace ``%xx`` escapes by their single-octet equivalent, and return a |
| 481 | :class:`bytes` object. |
| 482 | |
| 483 | *string* may be either a :class:`str` or a :class:`bytes`. |
| 484 | |
| 485 | If it is a :class:`str`, unescaped non-ASCII characters in *string* |
| 486 | are encoded into UTF-8 bytes. |
| 487 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 488 | Example: ``unquote_to_bytes('a%26%EF')`` yields ``b'a&\xef'``. |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 489 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 490 | |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 491 | .. function:: urlencode(query, doseq=False, safe='', encoding=None, errors=None) |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 492 | |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 493 | Convert a mapping object or a sequence of two-element tuples, which may |
Senthil Kumaran | f0769e8 | 2010-08-09 19:53:52 +0000 | [diff] [blame] | 494 | either be a :class:`str` or a :class:`bytes`, to a "percent-encoded" string, |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 495 | suitable to pass to :func:`urlopen` above as the optional *data* argument. |
| 496 | This is useful to pass a dictionary of form fields to a ``POST`` request. |
| 497 | The resulting string is a series of ``key=value`` pairs separated by ``'&'`` |
| 498 | characters, where both *key* and *value* are quoted using :func:`quote_plus` |
| 499 | above. When a sequence of two-element tuples is used as the *query* |
| 500 | argument, the first element of each tuple is a key and the second is a |
| 501 | value. The value element in itself can be a sequence and in that case, if |
| 502 | the optional parameter *doseq* is evaluates to *True*, individual |
| 503 | ``key=value`` pairs separated by ``'&'`` are generated for each element of |
| 504 | the value sequence for the key. The order of parameters in the encoded |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 505 | string will match the order of parameter tuples in the sequence. |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 506 | |
| 507 | When *query* parameter is a :class:`str`, the *safe*, *encoding* and *error* |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 508 | parameters are passed down to :func:`quote_plus` for encoding. |
| 509 | |
| 510 | To reverse this encoding process, :func:`parse_qs` and :func:`parse_qsl` are |
| 511 | provided in this module to parse query strings into Python data structures. |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 512 | |
| 513 | .. versionchanged:: 3.2 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 514 | Query parameter supports bytes and string objects. |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 515 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 516 | |
| 517 | .. seealso:: |
| 518 | |
Senthil Kumaran | 6257bdd | 2010-04-22 05:53:18 +0000 | [diff] [blame] | 519 | :rfc:`3986` - Uniform Resource Identifiers |
| 520 | This is the current standard (STD66). Any changes to urlparse module |
| 521 | should conform to this. Certain deviations could be observed, which are |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 522 | mostly for backward compatibility purposes and for certain de-facto |
Senthil Kumaran | 6257bdd | 2010-04-22 05:53:18 +0000 | [diff] [blame] | 523 | parsing requirements as commonly observed in major browsers. |
| 524 | |
| 525 | :rfc:`2732` - Format for Literal IPv6 Addresses in URL's. |
| 526 | This specifies the parsing requirements of IPv6 URLs. |
| 527 | |
| 528 | :rfc:`2396` - Uniform Resource Identifiers (URI): Generic Syntax |
| 529 | Document describing the generic syntactic requirements for both Uniform Resource |
| 530 | Names (URNs) and Uniform Resource Locators (URLs). |
| 531 | |
| 532 | :rfc:`2368` - The mailto URL scheme. |
| 533 | Parsing requirements for mailto url schemes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 534 | |
| 535 | :rfc:`1808` - Relative Uniform Resource Locators |
| 536 | This Request For Comments includes the rules for joining an absolute and a |
| 537 | relative URL, including a fair number of "Abnormal Examples" which govern the |
| 538 | treatment of border cases. |
| 539 | |
Senthil Kumaran | 6257bdd | 2010-04-22 05:53:18 +0000 | [diff] [blame] | 540 | :rfc:`1738` - Uniform Resource Locators (URL) |
| 541 | This specifies the formal syntax and semantics of absolute URLs. |