Registry / http-networking / py3dns

py3dns

JSON →
library4.0.2pypypi✓ verified 89d ago

Py3DNS is a Python 3 library for making DNS queries and parsing responses. It provides a flexible API for various record types (A, AAAA, MX, NS, SOA, etc.) and is a port of the older `pydns` library, specifically adapted for Python 3. The current version is 4.0.2, with updates primarily focusing on Python compatibility, bug fixes, and feature enhancements like `asyncio` support.

pip install py3dns
INSTALL
IMPORT
SIG · PY3DNS
P
py3dns
http-networkingpythonv4.0.2
Install
1.5s avg
Import
23ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v4.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.025s · 18MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 1.5s · import 0.022s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DNS
✓ import DNS
✗ from py3dns import DNS
The primary API is exposed directly under the 'DNS' module namespace after 'import DNS'.

This quickstart demonstrates how to perform basic A and MX record DNS queries using Py3DNS. It includes name server discovery and a fallback to public DNS if discovery fails, making the example robust for various environments.

import DNS import os # Configure name servers, typically from system settings # For testing, you can explicitly set them or rely on system discovery # Example using Google's DNS for demonstration if system discovery fails or for specific testing # DNS.defaults['server'] = ['8.8.8.8', '8.8.4.4'] # Discover name servers from /etc/resolv.conf or common system locations DNS.DiscoverNameServers() # Ensure name servers were found or set if not DNS.defaults.get('server'): print("Warning: No DNS name servers discovered. Queries might fail.") print("Attempting to use public DNS servers as fallback.") DNS.defaults['server'] = ['8.8.8.8', '8.8.4.4'] # Fallback to Google DNS # Make an A record query for google.com try: req = DNS.Request(name='google.com', qtype='A') response = req.req() print(f"DNS Query for google.com (A records):\n") if response.answers: for answer in response.answers: print(f" Name: {answer.name}, Type: {answer.qtype}, Data: {answer.data}, TTL: {answer.ttl}") else: print(" No A records found for google.com.") # Example: MX record query req_mx = DNS.Request(name='example.com', qtype='MX') response_mx = req_mx.req() print(f"\nDNS Query for example.com (MX records):\n") if response_mx.answers: for answer in response_mx.answers: print(f" Name: {answer.name}, Type: {answer.qtype}, Priority: {answer.data[0]}, Exchange: {answer.data[1]}") else: print(" No MX records found for example.com.") except DNS.DNSError as e: print(f"DNS Error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
breakingDirect migration from Python 2's 'pydns' to 'py3dns' is not straightforward, as 'py3dns' is a separate project with Python 3 specific adjustments. While the API is largely compatible, code will require Python 3 syntax and 'py3dns' installation.
fix
Ensure `py3dns` is installed (`pip install py3dns`). Review your existing `pydns` code and adjust for Python 3 syntax and module differences. Test thoroughly.
affects: All versions of py3dns (when migrating from pydns)
gotchaDNS queries will fail with a 'No name servers specified' error if name servers are not explicitly configured or automatically discovered before making requests.
fix
Call `DNS.DiscoverNameServers()` or `DNS.ParseResolvConf()` before making any `DNS.Request` calls to ensure name servers are properly set up. Alternatively, set `DNS.defaults['server'] = ['your_dns_server_ip']`.
affects: All versions
gotchaSynchronous DNS requests (e.g., `DNS.Request.req()`) are blocking and can lead to performance bottlenecks or frozen applications in concurrent or I/O-bound environments.
fix
For concurrent or non-blocking operations, consider using Py3DNS's `asyncio` integration (e.g., `DNS.async_req`) introduced in version 4.0.0, or manage concurrency using Python's `threading` or `multiprocessing` modules.
affects: All versions prior to 4.0.0, and synchronous usage in 4.x+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'DNS'
This error typically occurs if `py3dns` is not installed, or if an attempt was made to install the Python 2-only `pydns` package instead.
fix
Verify that `py3dns` is installed in your active environment by running `pip install py3dns`. Ensure your Python interpreter matches the environment where `py3dns` is installed.
DNSError: No name servers specified
The library could not find or was not explicitly provided with any DNS name server addresses to send queries to, often because `DNS.DiscoverNameServers()` was not called or failed.
fix
Before making any `DNS.Request`, ensure name servers are set up by calling `DNS.DiscoverNameServers()`, `DNS.ParseResolvConf()`, or by manually setting `DNS.defaults['server'] = ['IP_ADDRESS_1', 'IP_ADDRESS_2']`.
socket.timeout: timed out
A DNS query took longer than the default or configured timeout period, often due to network issues, an unresponsive DNS server, or a very slow connection.
fix
Increase the timeout value by setting `DNS.defaults['timeout'] = X` (where `X` is the timeout in seconds) before making the request. Also, check your network connectivity and the availability of your configured DNS servers.
Upgrade
Version history
4.0.2latest on PyPI · released Jun 6, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
py3dns — pip install py3dns · libregistry