Exemplo n.º 1
0
        def txt(localname):
            if localname.endswith('._device-info._tcp.local.'):
                info = ServiceInfo(localname, localname)
                info.request(self.zeroconf, timeout * 1000)
                if not info.text:
                    return [], [], []
            else:
                info = self.zeroconf.get_service_info(localname, localname,
                                                      timeout * 1000)
                if info is None:
                    return [], [], []

            order = []
            i = 0
            while i < len(info.text):
                length = info.text[i]
                i += 1
                kv = info.text[i:i + length].split(b'=')
                order.append(kv[0])
                i += length

            data = [
                b"%s=%s" % (p, info.properties[p])
                for p in sorted(info.properties,
                                key=lambda k: order.index(k)
                                if k in order else 1000)
            ]
            answers = [
                dns.RRHeader(name=localname[:-6] + domain,
                             ttl=ttl,
                             type=dns.TXT,
                             payload=dns.Record_TXT(*data))
            ]
            return answers, [], []
Exemplo n.º 2
0
    def service_from_zeroconf_query(self, topic, uuid):
        """Helper function to create a colguo.py.Service from a zeroconf query

        Args:
            topic: Topic string associated with the socket
            uuid: Unique identifier of the local node where the socket is located
        
        Returns:
            colugo.py.Service|None: Populated service if found on the network, otherwise None
        """
        def fix_socket_type(info):
            # For whatever reason, zeroconf is casting a property=1 to property=True
            # This just undoes that cast since socket_type will be an int (not bool)
            if info.properties['socket_type'.encode('utf-8')] == True:
                info.properties['socket_type'.encode('utf-8')] = 1
            return info

        info = ServiceInfo(type_=COLUGO_TYPE_STR,
                           name="_{}._{}.{}".format(topic, uuid,
                                                    COLUGO_TYPE_STR))
        res = info.request(self.zeroconf, 1000)
        address = port = node_uuid = None
        service = Service()
        if res:
            info = fix_socket_type(info)
            service.fill_from_info(info)
            return service
        return None