Thanks to visit codestin.com
Credit goes to github.com

Skip to content

aio-libs/aiohttp-asyncmdnsresolver

aiohttp-asyncmdnsresolver

https://readthedocs.org/projects/aiohttp-asyncmdnsresolver/badge/?version=latest Matrix Room — #aio-libs:matrix.org Matrix Space — #aio-libs-space:matrix.org

Introduction

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.

Installation

$ pip install aiohttp-asyncmdnsresolver

Quick start

import 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())

Resolving with both mDNS and DNS

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())

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

The documentation is located at https://aiohttp-asyncmdnsresolver.readthedocs.io.

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.

About

This module provides an aiohttp resolver that supports mDNS, which uses the zeroconf library to resolve mDNS queries.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

  •  
  •  
  •  

Packages

 
 
 

Contributors