Reference¶
The only public aiohttp_asyncmdnsresolver.api classes are AsyncMDNSResolver
and AsyncDualMDNSResolver:
>>> from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver
- class aiohttp_asyncmdnsresolver.api.AsyncMDNSResolver(*args, *, async_zeroconf=None, mdns_timeout=5.0, **kwargs)[source]¶
This class functions the same as
aiohttp.resolver.AsyncResolver, but with the added ability to resolve mDNS queries.- Parameters:
async_zeroconf (AsyncZeroconf) – If an
AsyncZeroconfinstance is passed, it will be used to resolve mDNS queries. If not, a new instance will be created.mdns_timeout (float) – The timeout for the mDNS query in seconds. If not provided the default timeout is 5 seconds. If the mdns_timeout is set to
0orNone, the query will only use the cache and will not perform a new query on the network.
Example:
import aiohttp from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver resolver = AsyncMDNSResolver() try: connector = aiohttp.TCPConnector(resolver=resolver) async with aiohttp.ClientSession(connector=connector) as session: async with session.get("http://KNKSADE41945.local.") as response: print(response.status) finally: await resolver.close()
The resolver also supports the async context manager protocol, which closes it automatically on exit –
aiohttp.TCPConnectordoes not take ownership of a resolver passed to it, so this is the recommended way to avoid leaking an internally createdAsyncZeroconfinstance. Ifasync_zeroconfwas supplied by the caller, that instance is not closed by the resolver and remains the caller’s responsibility to manage, even when usingasync withon the resolver:import aiohttp from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver async with AsyncMDNSResolver() as resolver: connector = aiohttp.TCPConnector(resolver=resolver) async with aiohttp.ClientSession(connector=connector) as session: async with session.get("http://KNKSADE41945.local.") as response: response.raise_for_status()
- async resolve(host, port=0, family=socket.AF_INET)[source]¶
Resolve host and return a list of
aiohttp.abc.ResolveResultmappings.Names in the
.localdomain (matched case-insensitively, per RFC 6762) are resolved over mDNS usingzeroconf; every other name is delegated toaiohttp.resolver.AsyncResolver. mDNS lookups consult thezeroconfcache first and only send a query on the network when the cache misses. RaisesOSErrorwhen the name cannot be resolved.family selects the address family to resolve. The supported values are
socket.AF_INET(the default, IPv4 only),socket.AF_INET6(IPv6 only) andsocket.AF_UNSPEC(both IPv4 and IPv6).
- async close()¶
Close the resolver and release its resources. When the
AsyncZeroconfinstance was created internally (noasync_zeroconfwas supplied to the constructor) it is closed as well; an externally supplied instance is left open for the caller to manage.
- class aiohttp_asyncmdnsresolver.api.AsyncDualMDNSResolver(*args, *, async_zeroconf=None, mdns_timeout=5.0, **kwargs)[source]¶
This resolver is a variant of
AsyncMDNSResolverthat resolves.localnames with both mDNS and regular DNS. It takes the same arguments asAsyncMDNSResolver, and is used in the same way.The first successful result from either resolver is returned.
If both resolvers fail, an exception is raised.
If both resolvers return results at the same time, the results are combined and duplicates are removed.
Example:
import aiohttp from aiohttp_asyncmdnsresolver.api import AsyncDualMDNSResolver resolver = AsyncDualMDNSResolver() try: 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) finally: await resolver.close()
- async resolve(host, port=0, family=socket.AF_INET)[source]¶
Behaves like
AsyncMDNSResolver.resolve(), except that.localnames are resolved over mDNS and unicast DNS concurrently following the rules above. Non-.localnames are delegated toaiohttp.resolver.AsyncResolver. RaisesOSErrorwhen neither resolver can resolve the name.
- async close()¶
Close the resolver and release its resources, with the same ownership semantics as
AsyncMDNSResolver.close().