本文整理汇总了Python中zeroconf.Zeroconf.notify_all方法的典型用法代码示例。如果您正苦于以下问题:Python Zeroconf.notify_all方法的具体用法?Python Zeroconf.notify_all怎么用?Python Zeroconf.notify_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zeroconf.Zeroconf
的用法示例。
在下文中一共展示了Zeroconf.notify_all方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_backoff
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import notify_all [as 别名]
def test_backoff():
got_query = Event()
type_ = "_http._tcp.local."
zeroconf_browser = Zeroconf(interfaces=['127.0.0.1'])
# we are going to monkey patch the zeroconf send to check query transmission
old_send = zeroconf_browser.send
time_offset = 0.0
start_time = time.time() * 1000
initial_query_interval = r._BROWSER_TIME / 1000
def current_time_millis():
"""Current system time in milliseconds"""
return start_time + time_offset * 1000
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
"""Sends an outgoing packet."""
got_query.set()
old_send(out, addr=addr, port=port)
# monkey patch the zeroconf send
setattr(zeroconf_browser, "send", send)
# monkey patch the zeroconf current_time_millis
r.current_time_millis = current_time_millis
# monkey patch the backoff limit to prevent test running forever
r._BROWSER_BACKOFF_LIMIT = 10 # seconds
# dummy service callback
def on_service_state_change(zeroconf, service_type, state_change, name):
pass
browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change])
try:
# Test that queries are sent at increasing intervals
sleep_count = 0
next_query_interval = 0.0
expected_query_time = 0.0
while True:
zeroconf_browser.notify_all()
sleep_count += 1
got_query.wait(0.1)
if time_offset == expected_query_time:
assert got_query.is_set()
got_query.clear()
if next_query_interval == r._BROWSER_BACKOFF_LIMIT:
# Only need to test up to the point where we've seen a query
# after the backoff limit has been hit
break
elif next_query_interval == 0:
next_query_interval = initial_query_interval
expected_query_time = initial_query_interval
else:
next_query_interval = min(2 * next_query_interval, r._BROWSER_BACKOFF_LIMIT)
expected_query_time += next_query_interval
else:
assert not got_query.is_set()
time_offset += initial_query_interval
finally:
browser.cancel()
zeroconf_browser.close()
示例2: test_integration
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import notify_all [as 别名]
def test_integration():
service_added = Event()
service_removed = Event()
unexpected_ttl = Event()
got_query = Event()
type_ = "_http._tcp.local."
registration_name = "xxxyyy.%s" % type_
def on_service_state_change(zeroconf, service_type, state_change, name):
if name == registration_name:
if state_change is ServiceStateChange.Added:
service_added.set()
elif state_change is ServiceStateChange.Removed:
service_removed.set()
zeroconf_browser = Zeroconf(interfaces=['127.0.0.1'])
# we are going to monkey patch the zeroconf send to check packet sizes
old_send = zeroconf_browser.send
time_offset = 0.0
def current_time_millis():
"""Current system time in milliseconds"""
return time.time() * 1000 + time_offset * 1000
expected_ttl = r._DNS_HOST_TTL
nbr_answers = 0
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
"""Sends an outgoing packet."""
pout = r.DNSIncoming(out.packet())
nonlocal nbr_answers
for answer in pout.answers:
nbr_answers += 1
if not answer.ttl > expected_ttl / 2:
unexpected_ttl.set()
got_query.set()
old_send(out, addr=addr, port=port)
# monkey patch the zeroconf send
setattr(zeroconf_browser, "send", send)
# monkey patch the zeroconf current_time_millis
r.current_time_millis = current_time_millis
# monkey patch the backoff limit to ensure we always get one query every 1/4 of the DNS TTL
r._BROWSER_BACKOFF_LIMIT = int(expected_ttl / 4)
service_added = Event()
service_removed = Event()
browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change])
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.")
zeroconf_registrar.register_service(info)
try:
service_added.wait(1)
assert service_added.is_set()
# Test that we receive queries containing answers only if the remaining TTL
# is greater than half the original TTL
sleep_count = 0
test_iterations = 50
while nbr_answers < test_iterations:
# Increase simulated time shift by 1/4 of the TTL in seconds
time_offset += expected_ttl / 4
zeroconf_browser.notify_all()
sleep_count += 1
got_query.wait(0.1)
got_query.clear()
# Prevent the test running indefinitely in an error condition
assert sleep_count < test_iterations * 4
assert not unexpected_ttl.is_set()
# Don't remove service, allow close() to cleanup
finally:
zeroconf_registrar.close()
service_removed.wait(1)
assert service_removed.is_set()
browser.cancel()
zeroconf_browser.close()
示例3: test_integration
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import notify_all [as 别名]
def test_integration():
service_added = Event()
service_removed = Event()
unexpected_ttl = Event()
got_query = Event()
type_ = "_http._tcp.local."
registration_name = "xxxyyy.%s" % type_
def on_service_state_change(zeroconf, service_type, state_change, name):
if name == registration_name:
if state_change is ServiceStateChange.Added:
service_added.set()
elif state_change is ServiceStateChange.Removed:
service_removed.set()
zeroconf_browser = Zeroconf(interfaces=['127.0.0.1'])
# we are going to monkey patch the zeroconf send to check packet sizes
old_send = zeroconf_browser.send
time_offset = 0
def current_time_millis():
"""Current system time in milliseconds"""
return time.time() * 1000 + time_offset * 1000
expected_ttl = r._DNS_TTL
# needs to be a list so that we can modify it in our phony send
nbr_queries = [0, None]
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
"""Sends an outgoing packet."""
pout = r.DNSIncoming(out.packet())
for answer in pout.answers:
nbr_queries[0] += 1
if not answer.ttl > expected_ttl / 2:
unexpected_ttl.set()
got_query.set()
old_send(out, addr=addr, port=port)
# monkey patch the zeroconf send
zeroconf_browser.send = send
# monkey patch the zeroconf current_time_millis
r.current_time_millis = current_time_millis
service_added = Event()
service_removed = Event()
browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change])
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name,
socket.inet_aton("10.0.1.2"), 80, 0, 0,
desc, "ash-2.local.")
zeroconf_registrar.register_service(info)
try:
service_added.wait(1)
assert service_added.is_set()
sleep_count = 0
while nbr_queries[0] < 50:
time_offset += expected_ttl / 4
zeroconf_browser.notify_all()
sleep_count += 1
got_query.wait(1)
got_query.clear()
assert not unexpected_ttl.is_set()
# Don't remove service, allow close() to cleanup
finally:
zeroconf_registrar.close()
browser.cancel()
zeroconf_browser.close()