| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,77 @@ | |||
| 1 | + #!/usr/bin/env python3 | ||
| 2 | + """Example of announcing 250 services (in this case, a fake HTTP server).""" | ||
| 3 | + | ||
| 4 | + import argparse | ||
| 5 | + import asyncio | ||
| 6 | + import logging | ||
| 7 | + import socket | ||
| 8 | + import time | ||
| 9 | + from typing import List | ||
| 10 | + | ||
| 11 | + from zeroconf import IPVersion | ||
| 12 | + from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + async def register_services(infos: List[AsyncServiceInfo]) -> None: | ||
| 16 | + tasks = [aiozc.async_register_service(info) for info in infos] | ||
| 17 | + background_tasks = await asyncio.gather(*tasks) | ||
| 18 | + await asyncio.gather(*background_tasks) | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + async def unregister_services(infos: List[AsyncServiceInfo]) -> None: | ||
| 22 | + tasks = [aiozc.async_unregister_service(info) for info in infos] | ||
| 23 | + background_tasks = await asyncio.gather(*tasks) | ||
| 24 | + await asyncio.gather(*background_tasks) | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + async def close_aiozc(aiozc: AsyncZeroconf) -> None: | ||
| 28 | + await aiozc.async_close() | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + if __name__ == '__main__': | ||
| 32 | + logging.basicConfig(level=logging.DEBUG) | ||
| 33 | + | ||
| 34 | + parser = argparse.ArgumentParser() | ||
| 35 | + parser.add_argument('--debug', action='store_true') | ||
| 36 | + version_group = parser.add_mutually_exclusive_group() | ||
| 37 | + version_group.add_argument('--v6', action='store_true') | ||
| 38 | + version_group.add_argument('--v6-only', action='store_true') | ||
| 39 | + args = parser.parse_args() | ||
| 40 | + | ||
| 41 | + if args.debug: | ||
| 42 | + logging.getLogger('zeroconf').setLevel(logging.DEBUG) | ||
| 43 | + if args.v6: | ||
| 44 | + ip_version = IPVersion.All | ||
| 45 | + elif args.v6_only: | ||
| 46 | + ip_version = IPVersion.V6Only | ||
| 47 | + else: | ||
| 48 | + ip_version = IPVersion.V4Only | ||
| 49 | + | ||
| 50 | + infos = [] | ||
| 51 | + for i in range(250): | ||
| 52 | + infos.append( | ||
| 53 | + AsyncServiceInfo( | ||
| 54 | + "_http._tcp.local.", | ||
| 55 | + f"Paul's Test Web Site {i}._http._tcp.local.", | ||
| 56 | + addresses=[socket.inet_aton("127.0.0.1")], | ||
| 57 | + port=80, | ||
| 58 | + properties={'path': '/~paulsm/'}, | ||
| 59 | + server=f"zcdemohost-{i}.local.", | ||
| 60 | + ) | ||
| 61 | + ) | ||
| 62 | + | ||
| 63 | + print("Registration of 250 services, press Ctrl-C to exit...") | ||
| 64 | + aiozc = AsyncZeroconf(ip_version=ip_version) | ||
| 65 | + loop = asyncio.get_event_loop() | ||
| 66 | + loop.run_until_complete(register_services(infos)) | ||
| 67 | + print("Registration complete.") | ||
| 68 | + try: | ||
| 69 | + while True: | ||
| 70 | + time.sleep(0.1) | ||
| 71 | + except KeyboardInterrupt: | ||
| 72 | + pass | ||
| 73 | + finally: | ||
| 74 | + print("Unregistering...") | ||
| 75 | + loop.run_until_complete(unregister_services(infos)) | ||
| 76 | + print("Unregistration complete.") | ||
| 77 | + loop.run_until_complete(close_aiozc(aiozc)) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments