This module provides an aiohttp resolver that supports mDNS, which uses the zeroconf library
to resolve mDNS queries.
For full documentation please read https://aiohttp-asyncmdnsresolver.readthedocs.io.
$ pip install aiohttp-asyncmdnsresolverimport asyncio
import aiohttp
from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver
async def main():
# aiohttp does not own a resolver passed to TCPConnector, so use the
# resolver as an async context manager to close it on exit.
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())AsyncDualMDNSResolver is a variant that resolves .local names over mDNS
and regular DNS concurrently, returning the first successful result. Use it
when a .local name may be served by a unicast DNS server as well as mDNS.
import asyncio
import aiohttp
from aiohttp_asyncmdnsresolver.api import AsyncDualMDNSResolver
async def main():
# aiohttp does not own a resolver passed to TCPConnector, so use the
# resolver as an async context manager to close it on exit.
async with AsyncDualMDNSResolver() as resolver:
connector = aiohttp.TCPConnector(resolver=resolver)
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get('http://printer.local.') as response:
print(response.status)
asyncio.run(main())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.
The documentation is located at https://aiohttp-asyncmdnsresolver.readthedocs.io.
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.
It's Apache 2 licensed and freely available.