You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

30 lines
853 B

  1. from xmlrpc.client import ServerProxy
  2. import random
  3. M = 5
  4. PORT = 1234
  5. RING = [2, 7, 11, 17, 22, 27]
  6. def lookup(node_id: int, key: int) -> None:
  7. """Calls the get method for a remote node over RPC and print the result"""
  8. with ServerProxy(f'http://node_{node_id}:{PORT}') as node:
  9. print(f"lookup({node_id}, {key}) = {node.get(key)}")
  10. if __name__ == '__main__':
  11. print("Client started")
  12. # Asking a random node to insert an entry into the DHT
  13. # String keys should be consistently hashed to an integer value between 0 and (2**M)-1
  14. for i in range(2 ** M):
  15. with ServerProxy(f'http://node_{random.choice(RING)}:{PORT}') as node:
  16. node.put(i, f"value_{i}")
  17. # Querying a DHT node for the value of a certain key.
  18. lookup(2, 20)
  19. lookup(11, 15)
  20. lookup(17, 1)
  21. lookup(22, 27)
  22. lookup(2, 5)