def publish(name, desc, path, port): global server, localip if not server: ip = getip() if ip.startswith('127.'): # if we have no internet connection, this can happen. return localip = socket.inet_aton(ip) server = Zeroconf.Zeroconf(ip) hostname = socket.gethostname().split('.')[0] host = hostname + ".local" name = "%s-%s" % (hostname, name) # advertise to browsers svc = Zeroconf.ServiceInfo('_http._tcp.local.', name + '._http._tcp.local.', server = host, port = port, properties = {'description': desc, 'path': "/" + path}, address = localip, weight = 0, priority = 0) server.registerService(svc) # advertise to Mercurial clients svc = Zeroconf.ServiceInfo('_hg._tcp.local.', name + '._hg._tcp.local.', server = host, port = port, properties = {'description': desc, 'path': "/" + path}, address = localip, weight = 0, priority = 0) server.registerService(svc)
def announce(self, infohash, peerid): discovery_logger.info("announcing: %s", infohash) # old #service_name = "_BitTorrent-%s._tcp.local." % infohash #service_type = service_name service_name = "%s._%s" % (peerid, infohash) service_type = "_bittorrent._tcp.local." browser = Zeroconf.ServiceBrowser(self.server, service_type, self) service = Zeroconf.ServiceInfo( service_type, "%s.%s" % (service_name, service_type), address=None, # to be filled in later port=self.port, weight=0, priority=0, properties={}) service.browser = browser service.registered = False self.services.append(service) df = get_deferred_host_ip() df.addCallback(self._announce2, service) return service
def testSameName(self): name = "paired.local." generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet())
def testMatchQuestion(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet()) self.assertEqual(len(generated.questions), 1) self.assertEqual(len(generated.questions), len(parsed.questions)) self.assertEqual(question, parsed.questions[0])
def init_autodiscovery(self): """ Inicializa el descubrimiento automático de los servidores TagFS. """ self._zeroconf = Zeroconf.Zeroconf(self._daemon.hostname) zeroconf_service_name = '{n}.{t}'.format(n=self._pyro_uri, t=ZEROCONF_SERVICE_TYPE) self._zeroconf_service = Zeroconf.ServiceInfo(ZEROCONF_SERVICE_TYPE, zeroconf_service_name, socket.inet_aton(self._daemon.hostname), self._daemon.port, 0, 0, {}) self._zeroconf.registerService(self._zeroconf_service)
def _init_autodiscovery(self): """ Inicializa el descubrimiento automático de los servidores. """ self._servers = {} self._servers_mutex = threading.Lock() self.addService = self._server_added self.removeService = self._server_removed self._zeroconf = Zeroconf.Zeroconf(self._address) self._zeroconf_browser = Zeroconf.ServiceBrowser( self._zeroconf, ZEROCONF_SERVICE_TYPE, self)
def testNumbersQuestions(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) for i in range(0, 10): generated.addQuestion(question) bytes = generated.packet() numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) self.assertEqual(numQuestions, 10) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) self.assertEqual(numAddtionals, 0)
def getzcpaths(): ip = getip() if ip.startswith('127.'): return server = Zeroconf.Zeroconf(ip) l = listener() Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l) time.sleep(1) server.close() for value in l.found.values(): name = value.name[:value.name.index('.')] url = "http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port, value.properties.get("path", "/")) yield "zc-" + name, url
def __init__(self): hostname = socket.gethostname() if hostname == FRONTEND_NAME: print "Master node start" r = Zeroconf() browser = ServiceBrowser(r, type, listener); else: print "Slave node " + hostname + " start" r = Zeroconf() ip = socket.gethostbyname(socket.gethostname()) desc = {'desc': 'backend_node'} info = ServiceInfo(SERVICE_TYPE, hostname + "." + SERVICE_TYPE, socket.inet_aton(ip), APPLICATION_PORT, 0, 0, desc, hostname) print "mDNS service..." r.registerService(info) print "started OK"
def testNumbers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) self.assertEqual(numQuestions, 0) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) self.assertEqual(numAddtionals, 0)
def run(self): self.localhostname = split(socket.gethostname(),'.')[0]+'.local.' try: self.localip = socket.gethostbyname(self.localhostname) self.zeroconf = Zeroconf.Zeroconf(self.localip) except: return self.active = True toRegister = self.services() for i in toRegister: service = Zeroconf.ServiceInfo( i["type"]+".local.", i["name"]+"."+i["type"]+".local.", socket.inet_aton(self.localip), i["port"], 0, 0, i["properties"], self.localhostname) self.zeroconf.registerService(service)
class MyListener(object): def __init__(self): self.r = Zeroconf() pass def removeService(self, zeroconf, type, name): print "Service", name, "removed" def addService(self, zeroconf, type, name): print "Service", name, "added" print "Type is", type info = self.r.getServiceInfo(type, name) print "Address is", str(socket.inet_ntoa(info.getAddress())) print "Port is", info.getPort() print "Weight is", info.getWeight() print "Priority is", info.getPriority() print "Server is", info.getServer() print "Text is", info.getText() print "Properties are", info.getProperties()
class StorageServerDiscovery(object): def __init__(self): self.r = Zeroconf() pass def removeService(self, zeroconf, type, name): for member in storageBackendNodes: if member.hostname == name: storageBackendNodes.remove(member) return print "\tService", name, "removed" def addService(self, zeroconf, type, name): for member in storageBackendNodes: if member.hostname == name: return # already exist info = self.r.getServiceInfo(type, name) node = Node(name, socket.inet_ntoa(info.getAddress()), info.getPort()) storageBackendNodes.append(node) print "\tNode", name, "added"
def main(): usage = """Fusedaap :""" + fuse.Fuse.fusage server = DaapFS() server.fuse_args.setmod('foreground') server.parse(errex=1) server.multithreaded = True hostMan = HostManager() hdh = HostDirHandler(server.dirSup.requestDirLease("/hosts")) hostMan.addHandler(hdh) adh = ArtistDirHandler(server.dirSup.requestDirLease("/artists")) hostMan.addHandler(adh) r = Zeroconf.Zeroconf() r.addServiceListener(daapZConfType, hostMan) try: server.main() # main loop except: print 'Exiting . . .' r.close() return logger.info("closing zeroconf in main") print "Disconnecting from services . . ." r.close() # close zeroconf first so no new servers are connected to hostMan.closeAllConnections()
def testExceedinglyLongNamePart(self): name = "%s.local." % ("a" * 1000) generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) self.assertRaises(r.NamePartTooLongException, generated.packet)
def __init__(self): self.r = Zeroconf() pass
def _get_server(): global server if not server: server = Zeroconf.Zeroconf() return server
def testLongName(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet())
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ __author__ = "Paul Scott-Murphy" __email__ = "paul at scott dash murphy dot com" __version__ = "0.12" from Zeroconf import * import socket desc = {'path':'/~paulsm/'} info = ServiceInfo("_http._tcp.local.", "Paul's Test Web Site._http._tcp.local.", socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") r = Zeroconf() print "Registration of a service..." r.registerService(info) print "Waiting..."
def testParseOwnPacketFlags(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) parsed = r.DNSIncoming(generated.packet())
def addService(self, zeroconf, type, name): """Listener method called when new zeroconf service is detected.""" if self.__closed: return #do NOT add service if closed self.allHosts.append(name) ServiceResolver(zeroconf, Zeroconf.ServiceInfo(type, name), self, 3000)
def testParseOwnPacketSimpleUnicast(self): generated = r.DNSOutgoing(0, 0) parsed = r.DNSIncoming(generated.packet())
def testResponseHeaderBits(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() flags = ord(bytes[2]) << 8 | ord(bytes[3]) self.assertEqual(flags, 0x8000)
def testQueryHeaderBits(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() flags = ord(bytes[2]) << 8 | ord(bytes[3]) self.assertEqual(flags, 0x0)
def testTransactionID(self): """ID must be zero in a DNS-SD packet""" generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() id = ord(bytes[0]) << 8 | ord(bytes[1]) self.assertEqual(id, 0)
class MyListener(object): def __init__(self): self.r = Zeroconf() pass def removeService(self, zeroconf, type, name): print "Service", name, "removed" def addService(self, zeroconf, type, name): print "Service", name, "added" print "Type is", type info = self.r.getServiceInfo(type, name) print "Address is", str(socket.inet_ntoa(info.getAddress())) print "Port is", info.getPort() print "Weight is", info.getWeight() print "Priority is", info.getPriority() print "Server is", info.getServer() print "Text is", info.getText() print "Properties are", info.getProperties() if __name__ == '__main__': print "Multicast DNS Service Discovery for Python Browser test" r = Zeroconf() print "1. Testing browsing for a service..." type = "_http._tcp.local." listener = MyListener() browser = ServiceBrowser(r, type, listener)
def testParseOwnPacketQuestion(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)) parsed = r.DNSIncoming(generated.packet())
def testExceedinglyLongName(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) name = "%slocal." % ("part." * 1000) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet())
def testLaunchAndClose(self): rv = r.Zeroconf() rv.close()