Internationalized Domain Names in Applications (IDNA)
Actively maintained. Last release 2 months ago, commits in 13 of the last 26 weeks. A safe choice.
pip install idnaInternationalized Domain Names in Applications (IDNA)
Support for Internationalized Domain Names in Applications
(IDNA) and Unicode IDNA
Compatibility Processing. It
supersedes the standard library's encodings.idna, which only
implements the 2003 specification, offering broader script coverage and
limiting domains with known security vulnerabilities.
Usage
Package may be installed from PyPI via
the typical methods (e.g. python3 -m pip install idna)
For typical usage, the encode and decode functions will take a
domain name argument and perform a conversion to ASCII-compatible encoding
(known as A-labels), or to Unicode strings (known as U-labels)
respectively.
>>> import idna
>>> idna.encode('ドメイン.テスト')
b'xn--eckwd4c7c.xn--zckzah'
>>> print(idna.decode('xn--eckwd4c7c.xn--zckzah'))
ドメイン.テスト
Conversions can be applied at a per-label basis using the ulabel or
alabel functions for specialized use cases.
Compatibility Mapping (UTS #46)
This library provides support for Unicode IDNA Compatibility Processing which normalizes input from different potential ways a user may input a domain prior to performing the IDNA conversion operations. This functionality, known as a mapping, is considered by the specification to be a local user-interface issue distinct from IDNA conversion functionality.
For example, "Königsgäßchen" is not a permissible label as capital letters are not allowed. UTS #46 will convert this into lower case prior to applying the IDNA conversion.
>>> import idna
>>> idna.encode('Königsgäßchen')
...
idna.core.InvalidCodepoint: Codepoint U+004B at position 1 of 'Königsgäßchen' not allowed
>>> idna.encode('Königsgäßchen', uts46=True)
b'xn--knigsgchen-b4a3dun'
>>> idna.decode('xn--knigsgchen-b4a3dun')
'königsgäßchen'
When performing a decode operation for display purposes, decode()
accepts a display=True argument that leaves any xn-- label that
fails to decode unchanged. This is useful for user interface display
where a domain is in use, the A-label form can be presented when it
is not a valid IDN.
Exceptions
All errors raised during conversion derive from the base class. The more specific exceptions are: