-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathregistration.py
More file actions
executable file
·55 lines (45 loc) · 1.47 KB
/
registration.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.47 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
"""Example of announcing a service (in this case, a fake HTTP server)"""
from __future__ import annotations
import argparse
import logging
import socket
from time import sleep
from zeroconf import IPVersion, ServiceInfo, Zeroconf
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
version_group = parser.add_mutually_exclusive_group()
version_group.add_argument("--v6", action="store_true")
version_group.add_argument("--v6-only", action="store_true")
args = parser.parse_args()
if args.debug:
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
if args.v6:
ip_version = IPVersion.All
elif args.v6_only:
ip_version = IPVersion.V6Only
else:
ip_version = IPVersion.V4Only
desc = {"path": "/~paulsm/"}
info = ServiceInfo(
"_http._tcp.local.",
"Paul's Test Web Site._http._tcp.local.",
addresses=[socket.inet_aton("127.0.0.1")],
port=80,
properties=desc,
server="ash-2.local.",
)
zeroconf = Zeroconf(ip_version=ip_version)
print("Registration of a service, press Ctrl-C to exit...")
zeroconf.register_service(info)
try:
while True:
sleep(0.1)
except KeyboardInterrupt:
pass
finally:
print("Unregistering...")
zeroconf.unregister_service(info)
zeroconf.close()