IDNA — Internationalized Domain Names in Applications

3.11 · active · verified Fri Mar 27

idna implements the Internationalized Domain Names in Applications (IDNA 2008, RFC 5891) protocol and Unicode IDNA Compatibility Processing (UTS #46) for Python. It is the modern replacement for the built-in encodings.idna module, which only supports the obsolete IDNA 2003 standard. Current version is 3.11 (Python 3.8+); releases are irregular but active, with security patches and Unicode data updates driving most releases.

Warnings

Install

Imports

Quickstart

Encode a Unicode domain to ASCII-compatible encoding (A-label) and decode back; handle mixed-case input with uts46=True.

import idna

# Encode a Unicode domain to ACE/A-label bytes
acе_label = idna.encode('ドメイン.テスト')
print(ace_label)          # b'xn--eckwd4c7c.xn--zckzah'

# Decode ACE back to Unicode
unicode_domain = idna.decode('xn--eckwd4c7c.xn--zckzah')
print(unicode_domain)     # ドメイン.テスト

# Capital letters are rejected by IDNA 2008 strict mode;
# pass uts46=True to enable UTS #46 case-folding pre-processing.
ace_uts46 = idna.encode('Königsgäßchen', uts46=True)
print(ace_uts46)          # b'xn--knigsgchen-b4a3dun'

# Per-label helpers
print(idna.alabel('例え'))   # b'xn--r8jz45g'
print(idna.ulabel(b'xn--r8jz45g'))  # 例え

# Codec interface (import idna.codec to register it first)
import idna.codec
encoded = '例え.jp'.encode('idna2008')   # codec name is 'idna2008', NOT 'idna'
print(encoded)            # b'xn--r8jz45g.jp'

# Error handling
try:
    idna.encode('Königsgäßchen')   # strict mode rejects uppercase
except idna.core.InvalidCodepoint as e:
    print(f'Encoding error: {e}')

view raw JSON →