werkzeug.iri_to_uri

werkzeug.iri_to_uri(iri, charset='utf-8', errors='strict', safe_conversion=False)[source]

Converts any unicode based IRI to an acceptable ASCII URI. Werkzeug always uses utf-8 URLs internally because this is what browsers and HTTP do as well. In some places where it accepts an URL it also accepts a unicode IRI and converts it into a URI.

Examples for IRI versus URI:

>>> iri_to_uri(u'http://☃.net/')
'http://xn--n3h.net/'
>>> iri_to_uri(u'http://üser:pässword@☃.net/påth')
'http://%C3%BCser:p%C3%A4ssword@xn--n3h.net/p%C3%A5th'

There is a general problem with IRI and URI conversion with some protocols that appear in the wild that are in violation of the URI specification. In places where Werkzeug goes through a forced IRI to URI conversion it will set the safe_conversion flag which will not perform a conversion if the end result is already ASCII. This can mean that the return value is not an entirely correct URI but it will not destroy such invalid URLs in the process.

As an example consider the following two IRIs:

magnet:?xt=uri:whatever
itms-services://?action=download-manifest

The internal representation after parsing of those URLs is the same and there is no way to reconstruct the original one. If safe conversion is enabled however this function becomes a noop for both of those strings as they both can be considered URIs.

New in version 0.6.

Changed in version 0.9.6: The safe_conversion parameter was added.

Parameters:
  • iri – The IRI to convert.
  • charset – The charset for the URI.
  • safe_conversion – indicates if a safe conversion should take place. For more information see the explanation above.