我有一些我想用zeroconf看到的应用程序.
有一种简单的可编写脚本的方法吗?
我的网络管理员是否需要执行此操作才能启用此功能?
Python或sh是首选.特定于操作系统的建议欢迎使用Linux和OS X.
或者你可以使用bash:
dns-sd -R[ ...]
这在OS X上默认有效.对于其他*nix,请参阅avahi-publish手册页(您可能需要通过首选软件包管理器安装).
pybonjour似乎没有积极维护.我正在使用python-zeroconf.
pip install zeroconf
以下是我用来向iOS设备宣布Twisted-Autobahn WebSocket的脚本的摘录:
from zeroconf import ServiceInfo, Zeroconf class WebSocketManager(service.Service, object): ws_service_name = 'Verasonics WebSocket' wsPort = None wsInfo = None def __init__(self, factory, portCallback): factory.protocol = BroadcastServerProtocol self.factory = factory self.portCallback = portCallback self.zeroconf = Zeroconf() def privilegedStartService(self): self.wsPort = reactor.listenTCP(0, self.factory) port = self.wsPort.getHost().port fqdn = socket.gethostname() ip_addr = socket.gethostbyname(fqdn) hostname = fqdn.split('.')[0] wsDesc = {'service': 'Verasonics Frame', 'version': '1.0.0'} self.wsInfo = ServiceInfo('_verasonics-ws._tcp.local.', hostname + ' ' + self.ws_service_name + '._verasonics-ws._tcp.local.', socket.inet_aton(ip_addr), port, 0, 0, wsDesc, hostname + '.local.') self.zeroconf.register_service(self.wsInfo) self.portCallback(port) return super(WebSocketManager, self).privilegedStartService() def stopService(self): self.zeroconf.unregister_service(self.wsInfo) self.wsPort.stopListening() return super(WebSocketManager , self).stopService()
我推荐pybonjour.