-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathresolve_address.py
More file actions
executable file
·40 lines (29 loc) · 1.04 KB
/
resolve_address.py
File metadata and controls
executable file
·40 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
"""Example of resolving a name to an IP address."""
from __future__ import annotations
import asyncio
import logging
import sys
from zeroconf import AddressResolver, IPVersion
from zeroconf.asyncio import AsyncZeroconf
async def resolve_name(name: str) -> None:
aiozc = AsyncZeroconf()
await aiozc.zeroconf.async_wait_for_start()
resolver = AddressResolver(name)
if await resolver.async_request(aiozc.zeroconf, 3000):
print(f"{name} IP addresses:", resolver.ip_addresses_by_version(IPVersion.All))
else:
print(f"Name {name} not resolved")
await aiozc.async_close()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
argv = sys.argv.copy()
if "--debug" in argv:
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
argv.remove("--debug")
if len(argv) < 2 or not argv[1]:
raise ValueError("Usage: resolve_address.py [--debug] <name>")
name = argv[1]
if not name.endswith("."):
name += "."
asyncio.run(resolve_name(name))