summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorThomas Goirand <zigo@debian.org>2020-04-07 16:55:09 +0200
committerThomas Goirand <zigo@debian.org>2020-04-07 16:55:09 +0200
commit80e63ddae613834c6f4e4198365a8fd3ca3c24a4 (patch)
tree49adc8bb127f0fc0e23024c2184a442b0c15fcde /examples
parentbe941ab1c6f24ac535438e84c41eb1b77ec2c01a (diff)
New upstream version 0.25.0
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/browser.py23
-rwxr-xr-xexamples/registration.py23
-rwxr-xr-xexamples/self_test.py8
3 files changed, 43 insertions, 11 deletions
diff --git a/examples/browser.py b/examples/browser.py
index 8b4fd5e..bf3ebfb 100755
--- a/examples/browser.py
+++ b/examples/browser.py
@@ -2,13 +2,13 @@
""" Example of browsing for a service (in this case, HTTP) """
+import argparse
import logging
import socket
-import sys
from time import sleep
from typing import cast
-from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf
+from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf
def on_service_state_change(
@@ -36,11 +36,24 @@ def on_service_state_change(
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
- if len(sys.argv) > 1:
- assert sys.argv[1:] == ['--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
- zeroconf = Zeroconf()
+ zeroconf = Zeroconf(ip_version=ip_version)
print("\nBrowsing services, press Ctrl-C to exit...\n")
browser = ServiceBrowser(zeroconf, "_http._tcp.local.", handlers=[on_service_state_change])
diff --git a/examples/registration.py b/examples/registration.py
index bda55b8..65c2219 100755
--- a/examples/registration.py
+++ b/examples/registration.py
@@ -2,18 +2,31 @@
""" Example of announcing a service (in this case, a fake HTTP server) """
+import argparse
import logging
import socket
-import sys
from time import sleep
-from zeroconf import ServiceInfo, Zeroconf
+from zeroconf import IPVersion, ServiceInfo, Zeroconf
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
- if len(sys.argv) > 1:
- assert sys.argv[1:] == ['--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/'}
@@ -26,7 +39,7 @@ if __name__ == '__main__':
server="ash-2.local.",
)
- zeroconf = Zeroconf()
+ zeroconf = Zeroconf(ip_version=ip_version)
print("Registration of a service, press Ctrl-C to exit...")
zeroconf.register_service(info)
try:
diff --git a/examples/self_test.py b/examples/self_test.py
index 62e3257..35007db 100755
--- a/examples/self_test.py
+++ b/examples/self_test.py
@@ -18,10 +18,15 @@ if __name__ == '__main__':
r = Zeroconf()
print("1. Testing registration of a service...")
desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'}
+ addresses = [socket.inet_aton("127.0.0.1")]
+ expected = {'127.0.0.1'}
+ if socket.has_ipv6:
+ addresses.append(socket.inet_pton(socket.AF_INET6, '::1'))
+ expected.add('::1')
info = ServiceInfo(
"_http._tcp.local.",
"My Service Name._http._tcp.local.",
- addresses=[socket.inet_aton("127.0.0.1")],
+ addresses=addresses,
port=1234,
properties=desc,
)
@@ -34,6 +39,7 @@ if __name__ == '__main__':
print("3. Testing query of own service...")
queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
assert queried_info
+ assert set(queried_info.parsed_addresses()) == expected
print(" Getting self: %s" % (queried_info,))
print(" Query done.")
print("4. Testing unregister of service information...")