Exemplo n.º 1
0
class DHT(object):
    def __init__(self):
        # Session key
        self._key = os.urandom(20)  # 20 random bytes == 160 bits
        self.krpc = KRPCServer(9001, "1.1.1")
        self.routing_table = RoutingTable()

    def ping_callback(self, message, connection):
        if "r" in message and "id" in message["r"]:
            message = message["r"]
            node_to_add = Node(connection, node_id=message["id"])
            self.routing_table.add_or_update_node(node_to_add)

    def find_node_callback(self, message, connection):
        if "r" in message and "nodes" in message["r"]:
            for node in decode_nodes(message["r"]["nodes"]):
                found_node = Node(node[1], node_id=node[0])
                self.krpc.send_query(build_ping_krpc_query(found_node.id), found_node, self.ping_callback)
Exemplo n.º 2
0
 def __init__(self):
     # Session key
     self._key = os.urandom(20)  # 20 random bytes == 160 bits
     self.krpc = KRPCServer(9001, "1.1.1")
     self.routing_table = RoutingTable()