File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ """Example of resolving a name to an IPv4 address."""
4+
5+ import asyncio
6+ import logging
7+ import sys
8+
9+ from zeroconf import AddressResolver , IPVersion
10+ from zeroconf .asyncio import AsyncZeroconf
11+
12+
13+ async def resolve_name (name : str ) -> None :
14+ aiozc = AsyncZeroconf (interfaces = ["127.0.0.1" ])
15+ await aiozc .zeroconf .async_wait_for_start ()
16+ resolver = AddressResolver (name )
17+ if await resolver .async_request (aiozc .zeroconf , 3000 ):
18+ print (f"{ name } IP addresses:" , resolver .ip_addresses_by_version (IPVersion .All ))
19+ else :
20+ print (f"Name { name } not resolved" )
21+ await aiozc .async_close ()
22+
23+
24+ if __name__ == "__main__" :
25+ logging .basicConfig (level = logging .DEBUG )
26+ argv = sys .argv .copy ()
27+ if "--debug" in argv :
28+ logging .getLogger ("zeroconf" ).setLevel (logging .DEBUG )
29+ argv .remove ("--debug" )
30+
31+ if len (argv ) < 2 or not argv [1 ]:
32+ raise ValueError ("Usage: resolve_address.py [--debug] <name>" )
33+
34+ name = argv [1 ]
35+ if not name .endswith ("." ):
36+ name += "."
37+
38+ asyncio .run (resolve_name (name ))
You can’t perform that action at this time.
0 commit comments