本文整理汇总了Python中zeroconf.Zeroconf方法的典型用法代码示例。如果您正苦于以下问题:Python zeroconf.Zeroconf方法的具体用法?Python zeroconf.Zeroconf怎么用?Python zeroconf.Zeroconf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zeroconf
的用法示例。
在下文中一共展示了zeroconf.Zeroconf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mDNSinit
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def mDNSinit(type, name):
deviceType = '_' + type
desc = {'deviceName': name}
# desc = {}
info = ServiceInfo(deviceType + "._tcp.local.",
name + "." + deviceType +"._tcp.local.",
socket.inet_aton(deviceIP), wsPort, 0, 0,
desc, name + ".local.")
zeroconf = Zeroconf()
zeroconf.register_service(info)
# if isDebugAll() is True:
BLINKER_LOG_ALL('deviceIP: ', deviceIP)
BLINKER_LOG_ALL('mdns name: ', name)
BLINKER_LOG('mDNS responder init!')
示例2: __init__
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def __init__(self, deviceId):
"""
Create and start a researcher for devices on network.
Arguments:
- deviceId : List of deviceIds (strings) to search.
"""
self._zeroconf = Zeroconf()
self._browser = []
self._services = {}
self._lock = threading.RLock()
self._cond = threading.Condition(self._lock)
for did in deviceId:
self._browser.append(ServiceBrowser(self._zeroconf, '_arsdk-' +
str(did) + '._udp.local.',
self))
示例3: run
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def run(self, ttl=None):
if self.host:
self.zeroconf = zeroconf.Zeroconf(interfaces=[self.host])
else:
self.zeroconf = zeroconf.Zeroconf()
zeroconf.ServiceBrowser(self.zeroconf, self.domain, MDNSHandler(self))
if ttl:
GObject.timeout_add(ttl * 1000, self.shutdown)
self.__running = True
self.__mainloop = GObject.MainLoop()
context = self.__mainloop.get_context()
try:
while self.__running:
if context.pending():
context.iteration(True)
else:
time.sleep(0.01)
except KeyboardInterrupt:
pass
self.zeroconf.close()
logger.info('MDNSListener.run()')
示例4: start
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def start(self) -> None:
"""The ZeroConf service changed requests are handled in a separate thread so we don't block the UI.
We can also re-schedule the requests when they fail to get detailed service info.
Any new or re-reschedule requests will be appended to the request queue and the thread will process them.
"""
self._service_changed_request_queue = Queue()
self._service_changed_request_event = Event()
try:
self._zero_conf = Zeroconf()
# CURA-6855 catch WinErrors
except OSError:
Logger.logException("e", "Failed to create zeroconf instance.")
return
self._service_changed_request_thread = Thread(target = self._handleOnServiceChangedRequests, daemon = True, name = "ZeroConfServiceChangedThread")
self._service_changed_request_thread.start()
self._zero_conf_browser = ServiceBrowser(self._zero_conf, self.ZERO_CONF_NAME, [self._queueService])
# Cleanup ZeroConf resources.
示例5: add_service
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def add_service(self, zeroconf, type, name):
"""
Method required to be a Zeroconf listener. Called by Zeroconf when a "_device._tcp" service
is registered on the network. Don't call directly.
sample values:
type = '_device._tcp.local.'
name = 'DEVICE000._device._tcp.local.'
"""
try:
info = zeroconf.get_service_info(type, name)
if info:
#ip = socket.inet_ntoa(info.address)
ip = socket.inet_ntoa(info.addresses[0])
self.add( ip, name )
except Exception as error:
logging.error("Exception trying to add zeroconf service '"+name+"' of type '"+type+"': "+str(error))
示例6: add_service
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def add_service(self, zeroconf_obj, service_type, name):
"""Callback called by ServiceBrowser when a new mDNS service is discovered.
Sometimes there is a delay in zeroconf between the add_service callback
being triggered and the service actually being returned in a call to
zeroconf_obj.get_service_info(). Because of this there are a few retries.
Args:
zeroconf_obj: The Zeroconf class instance.
service_type: The string name of the service, such
as '_privet._tcp.local.'.
name: The name of the service on mDNS.
"""
self.logger.info('Service added: "%s"', name)
self.lock.acquire()
info = zeroconf_obj.get_service_info(service_type, name, timeout=10000)
retries = 5
while info is None and retries > 0:
self.logger.error('zeroconf_obj.get_service_info returned None, forces '
'retry.')
time.sleep(0.1)
retries -= 1
info = zeroconf_obj.get_service_info(service_type, name, timeout=10000)
if info is not None:
self._added_service_infos.append(copy.deepcopy(info))
self.lock.release()
示例7: wait_for_service_add
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def wait_for_service_add(t_seconds, target_service, listener):
"""Wait for a service to be added.
Args:
t_seconds: Time to listen for mDNS records, in seconds.
Floating point ok.
service: string, The service to wait for
listener: _Listener object, the listener to wait on
Returns:
If Add event observed, return the Zeroconf information class; otherwise,
return None
"""
t_end = time.time() + t_seconds
while time.time() < t_end:
services = listener.services()
for service in services:
if target_service in service.properties['ty']:
return service
time.sleep(1)
return None
示例8: __init__
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def __init__(self, logger, wifi_interfaces=[]):
"""Initialization requires a logger.
Args:
logger: initialized logger object.
if_addr: string, interface address for Zeroconf, None means
all interfaces.
"""
self.logger = logger
self.l = _Listener(logger)
if not wifi_interfaces:
self.z = Zeroconf()
else:
self.z = Zeroconf(wifi_interfaces)
self.sb = ServiceBrowser(zc=self.z, type_='_privet._tcp.local.',
listener=self.l)
示例9: start
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def start():
global localHTTP, zeroconf, info, httpthread
ip = get_local_address()
logging.info("Local IP is " + ip)
desc = {'version': '0.1'}
info = ServiceInfo("_http._tcp.local.",
"Alexa Device._http._tcp.local.",
socket.inet_aton(ip), alexa_params.LOCAL_PORT, 0, 0,
desc, alexa_params.LOCAL_HOST + ".")
zeroconf = Zeroconf()
zeroconf.registerService(info)
logging.info("Local mDNS is started, domain is " + alexa_params.LOCAL_HOST)
localHTTP = HTTPServer(("", alexa_params.LOCAL_PORT), alexa_http_config.AlexaConfig)
httpthread = threading.Thread(target=localHTTP.serve_forever)
httpthread.start()
logging.info("Local HTTP is " + alexa_params.BASE_URL)
alexa_control.start()
示例10: main
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def main():
zeroconf = Zeroconf()
listener = MyListener()
browser = ServiceBrowser(zeroconf, "_ewelink._tcp.local.",listener= listener)
while True:
if listener.all_sub_num>0:
dict=listener.all_info_dict.copy()
for x in dict.keys():
info=dict[x]
info=zeroconf.get_service_info(info.type,x)
if info!= None:
data=info.properties
cur_str=x[8:18]+" "+parseAddress(info.address)+" "+str(info.port)+" " +str(data)
print(cur_str)
if len(listener.all_del_sub)>0:
for x in listener.all_del_sub:
cur_str=x[8:18]+"\nDEL"
print(cur_str)
time.sleep(0.5)
示例11: announce_zeroconf
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def announce_zeroconf(self):
desc = {'name': 'SigmaTCP',
'vendor': 'HiFiBerry',
'version': hifiberrydsp.__version__}
hostname = socket.gethostname()
try:
ip = socket.gethostbyname(hostname)
except Exception:
logging.error("can't get IP for hostname %s, "
"not initialising Zeroconf",
hostname)
return
self.zeroconf_info = ServiceInfo(ZEROCONF_TYPE,
"{}.{}".format(
hostname, ZEROCONF_TYPE),
socket.inet_aton(ip),
DEFAULT_PORT, 0, 0, desc)
self.zeroconf = Zeroconf()
self.zeroconf.register_service(self.zeroconf_info)
示例12: __init__
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def __init__(self, config):
self.config = config
# Use IPv6
self.serveraddress = ('::', self.config.port)
self.ServerClass = HTTPServerV6
self.ServerClass.allow_reuse_address = False
self.ip_addr = AirDropUtil.get_ip_for_interface(self.config.interface, ipv6=True)
if self.ip_addr is None:
if self.config.interface == 'awdl0':
raise RuntimeError('Interface {} does not have an IPv6 address. '
'Make sure that `owl` is running.'.format(self.config.interface))
else:
raise RuntimeError('Interface {} does not have an IPv6 address'.format(self.config.interface))
self.Handler = AirDropServerHandler
self.Handler.config = self.config
self.zeroconf = Zeroconf(interfaces=[str(self.ip_addr)],
ip_version=IPVersion.V6Only,
apple_p2p=platform.system() == 'Darwin')
self.http_server = self._init_server()
self.service_info = self._init_service()
示例13: __init__
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def __init__(self, config):
self.ip_addr = AirDropUtil.get_ip_for_interface(config.interface, ipv6=True)
if self.ip_addr is None:
if config.interface == 'awdl0':
raise RuntimeError('Interface {} does not have an IPv6 address. '
'Make sure that `owl` is running.'.format(config.interface))
else:
raise RuntimeError('Interface {} does not have an IPv6 address'.format(config.interface))
self.zeroconf = Zeroconf(interfaces=[str(self.ip_addr)],
ip_version=IPVersion.V6Only,
apple_p2p=platform.system() == 'Darwin')
self.callback_add = None
self.callback_remove = None
self.browser = None
示例14: start_discovery
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def start_discovery(callback=None):
"""
Start discovering chromecasts on the network.
This method will start discovering chromecasts on a separate thread. When
a chromecast is discovered, the callback will be called with the
discovered chromecast's zeroconf name. This is the dictionary key to find
the chromecast metadata in listener.services.
This method returns the CastListener object and the zeroconf ServiceBrowser
object. The CastListener object will contain information for the discovered
chromecasts. To stop discovery, call the stop_discovery method with the
ServiceBrowser object.
"""
listener = CastListener(callback)
return listener, \
ServiceBrowser(Zeroconf(), "_googlecast._tcp.local.", listener)
示例15: _RegisterService
# 需要导入模块: import zeroconf [as 别名]
# 或者: from zeroconf import Zeroconf [as 别名]
def _RegisterService(self, name, ip, port):
# name: fully qualified service name
self.service_name = '%s.%s' % (name, service_type)
self.name = name
self.port = port
if ip == "0.0.0.0":
print("MDNS brodcasted on all interfaces")
interfaces = zeroconf.InterfaceChoice.All
ip = self.gethostaddr()
else:
interfaces = [ip]
self.server = zeroconf.Zeroconf(interfaces=interfaces)
print("MDNS brodcasted service address :" + ip)
self.ip_32b = socket.inet_aton(ip)
self.server.register_service(
zeroconf.ServiceInfo(service_type,
self.service_name,
self.ip_32b,
self.port,
properties=self.serviceproperties))
self.retrytimer = None