dnspython

raw JSON →
2.8.0 verified Tue May 12 auth: no python install: verified quickstart: verified

dnspython is a comprehensive DNS toolkit for Python, currently at version 2.8.0, supporting Python 3.10 and above. It is actively maintained and follows a regular release cadence.

pip install dnspython
error ModuleNotFoundError: No module named 'dns'
cause The `dnspython` package, which provides the `dns` module, is not installed in the Python environment, or the environment where it's installed is not the one being used.
fix
Install the dnspython package using pip: pip install dnspython
error ModuleNotFoundError: No module named 'dnspython'
cause Developers often try to import `dnspython` using `import dnspython`, but the primary module to import from the `dnspython` package is `dns` (e.g., `import dns.resolver`).
fix
Change your import statement from import dnspython to import dns (or more specifically, import dns.resolver, import dns.message, etc., depending on what you need).
error dns.resolver.NXDOMAIN: None of DNS query names exist
cause This error occurs when the queried domain name does not exist according to the configured DNS nameservers. This can be due to a typo in the domain, the domain truly not existing, or issues with the nameservers being used.
fix
Verify the domain name for typos. If the name is correct, try configuring the resolver to use different or public nameservers (e.g., Google's 8.8.8.8) using resolver = dns.resolver.Resolver(); resolver.nameservers = ['8.8.8.8'] or explicitly checking different nameservers.
error dns.resolver.NoAnswer
cause This exception is raised when the queried domain name exists, but there is no DNS record of the specific type requested (e.g., asking for an MX record where only A records exist).
fix
Catch the dns.resolver.NoAnswer exception in a try-except block to handle cases where no record of the specified type is found. Alternatively, when using resolver.query(), set raise_on_no_answer=False if you want to inspect the response even if no answer section is present.
error dns.resolver.LifetimeTimeout: The resolution lifetime expired
cause The DNS query or the overall resolution process took longer than the specified `lifetime` or `timeout` setting, meaning no response was received from any nameserver within the allotted time. This often indicates network issues, slow DNS servers, or restrictive firewall rules.
fix
Increase the lifetime and/or timeout parameters for your resolver. For example: resolver = dns.resolver.Resolver(); resolver.timeout = 5; resolver.lifetime = 10. Also, ensure that your network configuration allows DNS queries to the target nameservers.
breaking In dnspython 2.0.0, the 'dns.query' module was refactored, leading to changes in function signatures and behavior. Review the changelog for detailed migration steps.
fix Update your code to align with the new function signatures and behaviors as per the dnspython 2.0.0 release notes.
gotcha Avoid naming your script 'dns.py' to prevent import conflicts with the dnspython library.
fix Rename your script to a different name to prevent import conflicts.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.21s 20.3M
3.10 slim (glibc) - - 0.14s 21M
3.11 alpine (musl) - - 0.30s 22.7M
3.11 slim (glibc) - - 0.24s 23M
3.12 alpine (musl) - - 0.23s 14.5M
3.12 slim (glibc) - - 0.24s 15M
3.13 alpine (musl) - - 0.21s 14.2M
3.13 slim (glibc) - - 0.21s 15M
3.9 alpine (musl) - - 0.14s 19.7M
3.9 slim (glibc) - - 0.13s 20M

This script queries the A records for 'example.com' and prints each IP address.

import dns.resolver

# Query for A records of 'example.com'
result = dns.resolver.resolve('example.com', 'A')
for ipval in result:
    print('IP', ipval.to_text())