本文整理汇总了Python中zeroconf.Zeroconf.send方法的典型用法代码示例。如果您正苦于以下问题:Python Zeroconf.send方法的具体用法?Python Zeroconf.send怎么用?Python Zeroconf.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zeroconf.Zeroconf
的用法示例。
在下文中一共展示了Zeroconf.send方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_integration
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import send [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()
示例2: test_lots_of_names
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import send [as 别名]
def test_lots_of_names(self):
# instantiate a zeroconf instance
zc = Zeroconf(interfaces=['127.0.0.1'])
# create a bunch of servers
type_ = "_my-service._tcp.local."
name = 'a wonderful service'
server_count = 300
self.generate_many_hosts(zc, type_, name, server_count)
# verify that name changing works
self.verify_name_change(zc, type_, name, server_count)
# we are going to monkey patch the zeroconf send to check packet sizes
old_send = zc.send
longest_packet_len = 0
longest_packet = None # type: Optional[r.DNSOutgoing]
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
"""Sends an outgoing packet."""
packet = out.packet()
nonlocal longest_packet_len, longest_packet
if longest_packet_len < len(packet):
longest_packet_len = len(packet)
longest_packet = out
old_send(out, addr=addr, port=port)
# monkey patch the zeroconf send
setattr(zc, "send", send)
# dummy service callback
def on_service_state_change(zeroconf, service_type, state_change, name):
pass
# start a browser
browser = ServiceBrowser(zc, type_, [on_service_state_change])
# wait until the browse request packet has maxed out in size
sleep_count = 0
while sleep_count < 100 and longest_packet_len < r._MAX_MSG_ABSOLUTE - 100:
sleep_count += 1
time.sleep(0.1)
browser.cancel()
time.sleep(0.5)
import zeroconf
zeroconf.log.debug('sleep_count %d, sized %d', sleep_count, longest_packet_len)
# now the browser has sent at least one request, verify the size
assert longest_packet_len <= r._MAX_MSG_ABSOLUTE
assert longest_packet_len >= r._MAX_MSG_ABSOLUTE - 100
# mock zeroconf's logger warning() and debug()
from unittest.mock import patch
patch_warn = patch('zeroconf.log.warning')
patch_debug = patch('zeroconf.log.debug')
mocked_log_warn = patch_warn.start()
mocked_log_debug = patch_debug.start()
# now that we have a long packet in our possession, let's verify the
# exception handling.
out = longest_packet
assert out is not None
out.data.append(b'\0' * 1000)
# mock the zeroconf logger and check for the correct logging backoff
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
# try to send an oversized packet
zc.send(out)
assert mocked_log_warn.call_count == call_counts[0] + 1
assert mocked_log_debug.call_count == call_counts[0]
zc.send(out)
assert mocked_log_warn.call_count == call_counts[0] + 1
assert mocked_log_debug.call_count == call_counts[0] + 1
# force a receive of an oversized packet
packet = out.packet()
s = zc._respond_sockets[0]
# mock the zeroconf logger and check for the correct logging backoff
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
# force receive on oversized packet
s.sendto(packet, 0, (r._MDNS_ADDR, r._MDNS_PORT))
s.sendto(packet, 0, (r._MDNS_ADDR, r._MDNS_PORT))
time.sleep(2.0)
zeroconf.log.debug(
'warn %d debug %d was %s', mocked_log_warn.call_count, mocked_log_debug.call_count, call_counts
)
assert mocked_log_debug.call_count > call_counts[0]
# close our zeroconf which will close the sockets
zc.close()
# pop the big chunk off the end of the data and send on a closed socket
out.data.pop()
#.........这里部分代码省略.........
示例3: test_ttl
# 需要导入模块: from zeroconf import Zeroconf [as 别名]
# 或者: from zeroconf.Zeroconf import send [as 别名]
def test_ttl(self):
# instantiate a zeroconf instance
zc = Zeroconf(interfaces=['127.0.0.1'])
# service definition
type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name,
socket.inet_aton("10.0.1.2"), 80, 0, 0,
desc, "ash-2.local.")
# we are going to monkey patch the zeroconf send to check packet sizes
old_send = zc.send
# needs to be a list so that we can modify it in our phony send
nbr_answers = [0, None]
nbr_additionals = [0, None]
nbr_authorities = [0, None]
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
"""Sends an outgoing packet."""
for answer, time_ in out.answers:
nbr_answers[0] += 1
assert answer.ttl == expected_ttl
for answer in out.additionals:
nbr_additionals[0] += 1
assert answer.ttl == expected_ttl
for answer in out.authorities:
nbr_authorities[0] += 1
assert answer.ttl == expected_ttl
old_send(out, addr=addr, port=port)
# monkey patch the zeroconf send
zc.send = send
# register service with default TTL
expected_ttl = r._DNS_TTL
zc.register_service(info)
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 3
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
# query
query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA)
query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN))
zc.handle_query(query, r._MDNS_ADDR, r._MDNS_PORT)
assert nbr_answers[0] == 4 and nbr_additionals[0] == 1 and nbr_authorities[0] == 0
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
# unregister
expected_ttl = 0
zc.unregister_service(info)
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 0
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
# register service with custom TTL
expected_ttl = r._DNS_TTL * 2
assert expected_ttl != r._DNS_TTL
zc.register_service(info, ttl=expected_ttl)
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 3
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
# query
query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA)
query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN))
query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN))
zc.handle_query(query, r._MDNS_ADDR, r._MDNS_PORT)
assert nbr_answers[0] == 4 and nbr_additionals[0] == 1 and nbr_authorities[0] == 0
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
# unregister
expected_ttl = 0
zc.unregister_service(info)
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 0
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0