aiohttp-asyncmdnsresolver

Introduction

This module provides an aiohttp resolver that supports mDNS, using the zeroconf library to resolve .local host names. Any host name that is not in the .local domain is delegated to aiohttp’s standard AsyncResolver, so either resolver is a drop-in replacement for it.

Two resolver classes are provided:

AsyncMDNSResolver

Resolves .local names with mDNS only, and every other name with regular DNS.

AsyncDualMDNSResolver

Resolves .local names with both mDNS and regular DNS concurrently and returns the first successful result. Use this when .local names may also be served by a unicast DNS server.

Installation

$ pip install aiohttp-asyncmdnsresolver

Usage

Pass a resolver to an aiohttp.TCPConnector. aiohttp does not take ownership of a resolver supplied this way, so use the resolver as an async context manager to close it automatically when you are done with it:

import asyncio

import aiohttp
from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver


async def main() -> None:
    async with AsyncMDNSResolver() as resolver:
        connector = aiohttp.TCPConnector(resolver=resolver)
        async with aiohttp.ClientSession(connector=connector) as session:
            async with session.get("http://example.com") as response:
                print(response.status)
            async with session.get("http://xxx.local.") as response:
                print(response.status)


asyncio.run(main())

Swap AsyncMDNSResolver for AsyncDualMDNSResolver to resolve .local names with both mDNS and unicast DNS at once.

Security note

mDNS resolution of .local names is not authenticated; any host on the same local link can answer for any .local name, so the resolved address should not be treated as a trust boundary. When connecting to sensitive .local services, use HTTPS with certificate verification, and consider certificate pinning or mTLS. Passing mdns_timeout=0 or mdns_timeout=None restricts mDNS resolution to the existing zeroconf cache and avoids sending new queries on the network, though cached entries may themselves originate from on-link responses.

API documentation

Open Reference for reading full list of available methods.

Source code

The project is hosted on GitHub

Please file an issue on the bug tracker if you have found a bug or have some suggestion in order to improve the library.

Authors and License

It’s Apache 2 licensed and freely available.

Contents:

Indices and tables