Skip to content

Commit edd86c8

Browse files
committed
chore: improve example
1 parent 89b2953 commit edd86c8

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

examples/resolve_address.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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))

0 commit comments

Comments
 (0)