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.
pip install idnaVerified import paths — ran on the pinned version, not inferred.
Encode a Unicode domain to ASCII-compatible encoding (A-label) and decode back; handle mixed-case input with uts46=True.
Pin to idna<3 for Python 2. For Python 3, use idna>=3.0.
import idna.codec; domain.encode('idna2008')domain = domain.lstrip('.')Remove the transitional=True/False argument from all encode() calls.
idna.encode(domain, uts46=True) for user-supplied domains; idna.encode(domain) only for already-normalized lowercase labels.
Upgrade to idna>=3.7 immediately if processing untrusted domain names.
Catch IDNAError and fall back to encodings.idna only when emoji/symbol domain support is explicitly required.
Ensure all variables are assigned a value before they are referenced or used in operations (e.g., 'ace_label = idna.encode(domain_name)' instead of just 'print(ace_label)').
Ensure all variables are defined before they are used. Carefully check variable names for typos, especially when copying text or dealing with visually similar Unicode characters. It is recommended to use standard ASCII characters for variable names to avoid such confusion.
Ensure that the string passed to the IDNA encoding function represents a valid hostname, not an entire URL, and that each domain label (segment between dots) is no longer than 63 bytes. For URLs, parse out the hostname component before applying IDNA encoding.
To ensure the `idna` codec is loaded, explicitly import `encodings.idna` (or `import idna` if using the external library) at the application's startup, or perform a no-op encoding/decoding operation like `b''.decode('idna')` or `u''.encode('idna')` early in your code.Validate the input domain name string against IDNA 2008 and UTS #46 rules, ensuring it adheres to character sets, contextual requirements, and structural rules. Use specific exception handling for subclasses like `idna.InvalidCodepoint` to identify and address the exact violation.
No dependency data recorded yet.