aiohttp-asyncmdnsresolver: mDNS Resolver for aiohttp

0.1.1 · active · verified Thu Apr 16

aiohttp-asyncmdnsresolver is an asynchronous resolver for aiohttp that extends its capabilities to include mDNS (multicast DNS) resolution. It leverages the zeroconf library to resolve '.local' domain names. The library is actively maintained by the aio-libs organization, with its current version being 0.1.1, and typically sees releases aligned with updates to aiohttp or zeroconf, or as new features and fixes are introduced.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `aiohttp-asyncmdnsresolver` with `aiohttp.ClientSession` to enable both mDNS (`.local` hostnames) and standard DNS resolution. It uses `AsyncDualMDNSResolver` which attempts mDNS first and falls back to regular DNS, providing comprehensive hostname resolution. Remember to replace `mydevice.local.` with an actual mDNS-resolvable host on your network for testing mDNS.

import asyncio
import aiohttp
from aiohttp_asyncmdnsresolver.api import AsyncDualMDNSResolver

async def main():
    # Use AsyncDualMDNSResolver for both mDNS (.local) and standard DNS resolution
    resolver = AsyncDualMDNSResolver()
    # Create a TCPConnector with the custom resolver
    connector = aiohttp.TCPConnector(resolver=resolver, ssl=False)

    async with aiohttp.ClientSession(connector=connector) as session:
        # Example: Resolving a standard domain
        try:
            async with session.get('http://example.com') as response:
                print(f"Example.com Status: {response.status}")
        except Exception as e:
            print(f"Could not connect to example.com: {e}")

        # Example: Resolving an mDNS (.local) domain (replace with a real mDNS host if available)
        try:
            async with session.get('http://mydevice.local.') as response:
                print(f"Mydevice.local Status: {response.status}")
        except Exception as e:
            print(f"Could not connect to mydevice.local: {e}")

    await resolver.close()

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

view raw JSON →