aiodns

4.0.0 · active · verified Sun Apr 05

aiodns is a Python library that provides a simple way to perform asynchronous DNS resolutions using the `asyncio` framework and `pycares` as its backend. The current version is 4.0.0, and it generally follows an active release cadence with several updates throughout the year to improve performance, add features, and maintain compatibility with newer Python and `pycares` versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform an asynchronous 'A' record DNS lookup using the `DNSResolver` class and its recommended `query_dns` method. It includes basic error handling and shows the crucial step of closing the resolver to release resources.

import asyncio
from aiodns import DNSResolver

async def resolve_a_record(hostname):
    resolver = DNSResolver()
    try:
        # query_dns returns native pycares 5.x DNSResult types
        result = await resolver.query_dns(hostname, 'A')
        for record in result.answer:
            print(f"Hostname: {hostname}, Type: A, Address: {record.data.addr}")
    except Exception as e:
        print(f"DNS resolution failed for {hostname}: {e}")
    finally:
        # It's important to close the resolver when no longer needed.
        # For long-lived resolvers, manual closing is often done at application shutdown.
        resolver.close()

async def main():
    await resolve_a_record('google.com')
    await resolve_a_record('nonexistent.example.com') # Example of a failed lookup

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →