talira

Internationalized domain names (IDNA 2008 and UTS #46) for Python

open sourceBSD-3-Clausefree

idna converts internationalized domain names between their Unicode form (U-labels) and the ASCII-compatible encoding used on the wire (A-labels, the xn-- form), following IDNA 2008 (RFC 5891) and Unicode's IDNA Compatibility Processing (UTS #46). It supersedes Python's standard library encodings.idna, which only implements the 2003 specification, with broader script coverage and limits on domains with known security problems. The encode and decode functions convert whole domains, and alabel and ulabel single labels; UTS #46 mapping normalises input such as capital letters first. Errors carry machine-readable codes, and an idna command converts domains from the shell. It supports Python 3.10 and later, free-threaded Python included.

Problems it solves

  • convert an internationalized domain name to punycode in Python
  • Python's built-in idna codec rejects or mangles newer Unicode domain names
  • validate that a user-entered domain name with non-Latin characters is a valid IDN

Features

  • Encode and decode internationalized domain names between Unicode and xn-- ASCII form (IDNA 2008)
  • UTS #46 compatibility mapping that normalises user input before conversion
  • Per-label conversion with alabel and ulabel
  • Exceptions with machine-readable codes, codepoints and positions
  • idna command-line tool for converting domains
  • Thread-safe on free-threaded Python

Limitations

  • Emoji domains aren't supported: the IDNA standard prohibits encoding symbols such as emoji1
  • On an older Python, a character new to Unicode may be rejected as unknown, since some data comes from the running Python's unicodedata module1