Skip to content

Commit ef9334f

Browse files
authored
Add AsyncServiceBrowser example (#487)
1 parent 275765a commit ef9334f

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

examples/async_browser.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
3+
""" Example of browsing for a service.
4+
5+
The default is HTTP and HAP; use --find to search for all available services in the network
6+
"""
7+
8+
import argparse
9+
import asyncio
10+
import logging
11+
from typing import cast
12+
13+
from zeroconf import IPVersion, ServiceStateChange
14+
from zeroconf.asyncio import AsyncServiceBrowser, AsyncZeroconf
15+
16+
17+
def async_on_service_state_change(
18+
zeroconf: AsyncZeroconf, service_type: str, name: str, state_change: ServiceStateChange
19+
) -> None:
20+
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
21+
if state_change is not ServiceStateChange.Added:
22+
return
23+
asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name))
24+
25+
26+
async def async_display_service_info(zeroconf: AsyncZeroconf, service_type: str, name: str) -> None:
27+
info = await zeroconf.async_get_service_info(service_type, name)
28+
print("Info from zeroconf.get_service_info: %r" % (info))
29+
if info:
30+
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]
31+
print(" Name: %s" % name)
32+
print(" Addresses: %s" % ", ".join(addresses))
33+
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
34+
print(" Server: %s" % (info.server,))
35+
if info.properties:
36+
print(" Properties are:")
37+
for key, value in info.properties.items():
38+
print(" %s: %s" % (key, value))
39+
else:
40+
print(" No properties")
41+
else:
42+
print(" No info")
43+
print('\n')
44+
45+
46+
if __name__ == '__main__':
47+
logging.basicConfig(level=logging.DEBUG)
48+
49+
parser = argparse.ArgumentParser()
50+
parser.add_argument('--debug', action='store_true')
51+
version_group = parser.add_mutually_exclusive_group()
52+
version_group.add_argument('--v6', action='store_true')
53+
version_group.add_argument('--v6-only', action='store_true')
54+
args = parser.parse_args()
55+
56+
if args.debug:
57+
logging.getLogger('zeroconf').setLevel(logging.DEBUG)
58+
if args.v6:
59+
ip_version = IPVersion.All
60+
elif args.v6_only:
61+
ip_version = IPVersion.V6Only
62+
else:
63+
ip_version = IPVersion.V4Only
64+
65+
aiozc = AsyncZeroconf(ip_version=ip_version)
66+
67+
services = ["_http._tcp.local.", "_hap._tcp.local."]
68+
print("\nBrowsing %s service(s), press Ctrl-C to exit...\n" % services)
69+
aiobrowser = AsyncServiceBrowser(aiozc, services, handlers=[async_on_service_state_change])
70+
71+
loop = asyncio.get_event_loop()
72+
try:
73+
loop.run_forever()
74+
except KeyboardInterrupt:
75+
pass
76+
finally:
77+
loop.run_until_complete(aiobrowser.async_cancel())
78+
loop.run_until_complete(aiozc.async_close())

0 commit comments

Comments
 (0)