Created
September 28, 2011 02:24
-
-
Save matthewrobertbell/1246836 to your computer and use it in GitHub Desktop.
An attempt a simple minimalist DHT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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