def __init__(self, ip, port=12550, seed=()): """Constructor. A DHT is created based on the arguments.By default this is the first node of the DHT.If a tuple is provided, it is used as seed. Args: ip (ipaddress): IP address of the dht node. port (int): Port of the dht node (default 12550). seed (tuple): A single ip address along with port (default empty) """ self.ip = ip self.port = port self.seed = seed if not seed: self.ip_to_cpu = DHT(self.ip, self.port) self.ext_to_ip = DHT(self.ip, self.port + 1) else: self.ip_to_cpu = DHT(self.ip, self.port, seeds=[seed]) self.ext_to_ip = DHT(self.ip, self.port + 1, seeds=[seed])
from kad import DHT d = DHT('localhost', 3100) d['ciao'] = 'mondo' d['hello'] = 'world' for key in d: print(key, d[key])
ndht = DHT (nhost[0], nhost[1], boot_host=rhost[0], boot_port=rhost[1]) hosts.append (nhost) dhts.append (ndht) port += 1 testo = ["My", "json-serializable", "Object"] random.choice (dhts)["my_key"] = testo for x in range (0, 1): assert (random.choice (dhts)["my_key"] == testo) """ host1, port1 = 'localhost', 3000 dht1 = DHT(host1, port1) host2, port2 = 'localhost', 3001 dht2 = DHT(host2, port2, bootstrap_nodes=[(host1, port1)]) host3, port3 = 'localhost', 3002 dht3 = DHT(host3, port3, bootstrap_nodes=[(host2, port2)]) dht1["my_key"] = [u"My", u"json-serializable", u"Object"] dht2.get("my_key", lambda d: print('Find:', d)) dht3.get("my_key", lambda d: print('Find:', d)) #print (dht2["my_key"]) print(dht1.peers())
if sys.argv[0].find('pydoc') > 0: sys.argv = [sys.argv[0]] args = parser.parse_args() # set loglevel to use (DEBUG/INFO/WARNING) and message format loglevel = logging.INFO if args.verbose != None and args.verbose > 0: loglevel = logging.DEBUG logging.basicConfig(level=loglevel, format='%(asctime)s %(name)s %(levelname)s: %(message)s', datefmt='%d/%m/%Y %H:%M:%S') # create 2 DHT server nodes host1, port1 = 'localhost', 14900 if have_storage: store1 = pysos.Dict('sto%d.dat' % port1) else: store1 = {} dht1 = DHT(host1, port1, storage=store1) myseeds = [(host1, port1)] host2, port2 = 'localhost', 14901 if have_storage: store2 = pysos.Dict('sto%d.dat' % port2) else: store2 = {} dht2 = DHT(host2, port2, seeds=myseeds, storage=store2) # store some values via DHT1 or 2 dht1["my_key"] = [u"My", u"json-serializable", u"Object"] dht1["akey1"] = ["Some", "Strings", "for key1"] dht1["akey2"] = ["Other", "Strings", "for key2"] dht2["akey3"] = ["Further", "Strings", "for key3"] # demo retrieve data from nodes print("Blocking get from DHT1 = %s" % str(dht2["my_key"])) dht2.get("my_key",
class ImageDHT(object): """Store the list of resources in a DHT.""" def __init__(self, ip, port=12550, seed=()): """Constructor. A DHT is created based on the arguments.By default this is the first node of the DHT.If a tuple is provided, it is used as seed. Args: ip (ipaddress): IP address of the dht node. port (int): Port of the dht node (default 12550). seed (tuple): A single ip address along with port (default empty) """ self.ip = ip self.port = port self.seed = seed if not seed: self.ip_to_cpu = DHT(self.ip, self.port) self.ext_to_ip = DHT(self.ip, self.port + 1) self.loc_to_ip = DHT(self.ip, self.port + 2) else: self.ip_to_cpu = DHT(self.ip, self.port, seeds=[seed]) self.ext_to_ip = DHT(self.ip, self.port + 1, seeds=[seed]) self.loc_to_ip = DHT(self.ip, self.port + 2, seeds=[seed]) def get_iptocpu(self, key): #print(self.ip_to_cpu[key]) return self.ip_to_cpu[key] def get_exttoip(self, key): #print(self.ext_to_ip[key]) return self.ext_to_ip[key] def get_loctoip(self, key): #print(self.loc_to_ip[key]) return self.loc_to_ip[key] '''def set_item_iptocpu(self,key,value): self.ip_to_cpu[key]=value def set_item_exttoip(self,key,value): self.ext_to_ip[key] += [value] ''' def get_peers(self): for peer in self.ext_to_ip.peers(): print(peer) return self.ext_to_ip.peers() def get_all_iptocpu(self): for key in self.ip_to_cpu: print(key, " ", self.ip_to_cpu[key]) def get_all_exttoip(self): for key in self.ext_to_ip: print(key, " ", self.ext_to_ip[key]) def get_all_loctoip(self): for key in self.loc_to_ip: print(key, "xx", self.loc_to_ip[key])
ndht = DHT (nhost[0], nhost[1], boot_host=rhost[0], boot_port=rhost[1]) hosts.append (nhost) dhts.append (ndht) port += 1 testo = ["My", "json-serializable", "Object"] random.choice (dhts)["my_key"] = testo for x in range (0, 1): assert (random.choice (dhts)["my_key"] == testo) """ host1, port1 = 'localhost', 3001 dht1 = DHT(host1, port1, storage=shelve.open('sto1.dat')) host2, port2 = 'localhost', 3002 dht2 = DHT(host2, port2, seeds=[(host1, port1)], storage=shelve.open('sto2.dat')) host3, port3 = 'localhost', 3003 dht3 = DHT(host3, port3, seeds=[(host2, port2)], storage=shelve.open('sto3.dat')) dht1["my_key"] = [u"My", u"json-serializable", u"Object"] dht2.get("my_key", lambda d: print('Find:', d)) dht3.get("my_key", lambda d: print('Find:', d)) # print(dht2["my_key"]) print(dht1.peers()) print(dht2.peers()) print(dht3.peers())
ndht = DHT (nhost[0], nhost[1], boot_host=rhost[0], boot_port=rhost[1]) hosts.append (nhost) dhts.append (ndht) port += 1 testo = ["My", "json-serializable", "Object"] random.choice (dhts)["my_key"] = testo for x in range (0, 1): assert (random.choice (dhts)["my_key"] == testo) """ host1, port1 = 'localhost', 3001 dht1 = DHT(host1, port1, storage=shelve.open('sto1.dat')) host2, port2 = 'localhost', 3002 dht2 = DHT(host2, port2, seeds=[(host1, port1)], storage=shelve.open('sto2.dat')) host3, port3 = 'localhost', 3003 dht3 = DHT(host3, port3, seeds=[(host2, port2)], storage=shelve.open('sto3.dat')) dht1["my_key"] = [u"My", u"json-serializable", u"Object"]
from kad import DHT repo = Repo(".") VERSION = "0.0.1" REVISION = repo.commit("HEAD").hexsha[0:10] x = json.load(open("bootstrap-nodes.json")) nodes = [] for node in x: nodes.append(( node["host"], node["port"] )) dht = DHT("0.0.0.0", 49241, bootstrap_nodes=nodes ) def hat(bits=128, base=16): base = 16 i = 2 digits = math.log(math.pow(2, bits))/ math.log(base) while digits == math.inf: digits = math.log(math.pow(2, bits / i)) / math.log(base) * i rem = digits - math.floor(digits) res = ""