Skip to content

Instantly share code, notes, and snippets.

@matthewrobertbell
Created September 28, 2011 02:24
Show Gist options
  • Select an option

  • Save matthewrobertbell/1246836 to your computer and use it in GitHub Desktop.

Select an option

Save matthewrobertbell/1246836 to your computer and use it in GitHub Desktop.
An attempt a simple minimalist DHT
import hashlib
class DHT(object):
def __init__(self):
self.hashes = set()
def nodes(self,hash):
hash_int = long(hash,16)
closeness = {}
for v in self.hashes:
v_int = long(v,16)
closeness_value = hash_int - v_int
if closeness_value < 0:
closeness_value *= -1
if not v in closeness:
closeness[v] = closeness_value
elif closeness_value < closeness[v]:
closeness[v] = closeness_value
return sorted(closeness, key=closeness.get)[:3]
if __name__ == '__main__':
dht = DHT()
for i in range(100):
node = 'Node %s' % i
dht.hashes.add(hashlib.sha256(node).hexdigest())
print dht.nodes(hashlib.sha256('example to be hashed').hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment