def process_get_peers_request(crawler, msg, address): # 加入nodes crawler.nodes.add(dht_node.Node(nid=msg[b'a'][b'id'], ip=address[0], port=address[1])) # 最近8个邻居节点信息 close_nodes = crawler.nodes.get_close_nodes() # 发送response response = { 't': msg[b't'], 'y':'r', 'r':{ 'id':crawler.crawler_nid, 'token':dht_id.gen_random_nid(), 'nodes':dht_node.encode_nodes(close_nodes) } } send_response(crawler.sock, address, response)
def test_encode_nodes(self): nodes = [dht_node.Node(b'12345678901234567890', '65.66.67.68', 17990), dht_node.Node(b'09876543210987654321', '68.67.66.65', 17990)] compact_nodes = b'12345678901234567890ABCDFF09876543210987654321DCBAFF' self.assertEqual(compact_nodes, dht_node.encode_nodes(nodes))