The `fqdn` Python library, currently at version 1.5.1, provides RFC-compliant validation and manipulation of Fully Qualified Domain Names (FQDNs). Its primary purpose is to ensure that domain names adhere to Internet Engineering Task Force specifications like RFC 1123, making them suitable for traditional internet hostname usage. This is often a stricter subset of what modern web browsers accept. The library typically sees updates in response to specification clarifications or bug fixes, though major releases are infrequent.
pip install fqdnVerified import paths — ran on the pinned version, not inferred.
Instantiate the `FQDN` class with a domain string and use its `is_valid` property to check for RFC compliance. Other properties like `absolute` and `relative` provide different representations.
Understand that `fqdn` prioritizes strict RFC compliance. If browser-like permissiveness is needed, custom relaxation of rules or an alternative library may be required.
If single-label hostnames or other non-standard formats are expected, consult the library's source or documentation for configuration options to relax specific constraints.
Use `from fqdn import FQDN` for external string validation. Use `import socket; socket.getfqdn()` only when inquiring about the local system's hostname resolution.
For validation against actual registrable domains or live existence, combine `fqdn` with other libraries that perform DNS lookups (e.g., `dnspython`) or consult public suffix lists (e.g., `fqdn-parser`).
Install the library using pip: `pip install fqdn`
To allow underscores, you need to instantiate `FQDN` with the `allow_underscore` parameter set to `True`: `from fqdn import FQDN; domain = FQDN('my_host.example.com', allow_underscore=True); print(domain.is_valid)`To allow short hostnames (single-label or without a TLD), instantiate `FQDN` with the `allow_cached_without_dot` or `allow_non_canonical` (for broader relaxations) parameters set to `True`: `from fqdn import FQDN; domain = FQDN('localhost', allow_cached_without_dot=True); print(domain.is_valid)`Ensure the TLD of your domain name consists solely of alphabetic characters to comply with strict RFC standards. If numeric TLDs are needed for specific internal or non-standard uses, consider using the `allow_non_canonical` flag, but be aware this deviates from strict RFC compliance: `from fqdn import FQDN; domain = FQDN('example.123', allow_non_canonical=True); print(domain.is_valid)`