port dht.distance ~> py3

This commit is contained in:
Victor Shyba 2018-07-17 21:45:51 -03:00 committed by Jack Robison
parent 9967857a57
commit 5b35c4e8f0
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 8 additions and 3 deletions

View file

@ -1,4 +1,9 @@
from binascii import hexlify
from lbrynet.dht import constants
import sys
if sys.version_info > (3,):
long = int
class Distance(object):
@ -12,10 +17,10 @@ class Distance(object):
if len(key) != constants.key_bits / 8:
raise ValueError("invalid key length: %i" % len(key))
self.key = key
self.val_key_one = long(key.encode('hex'), 16)
self.val_key_one = long(hexlify(key), 16)
def __call__(self, key_two):
val_key_two = long(key_two.encode('hex'), 16)
val_key_two = long(hexlify(key_two), 16)
return self.val_key_one ^ val_key_two
def is_closer(self, a, b):

View file

@ -14,7 +14,7 @@ from lbrynet.dht import constants
def address_generator(address=(10, 42, 42, 1)):
def increment(addr):
value = struct.unpack("I", "".join([chr(x) for x in list(addr)[::-1]]))[0] + 1
value = struct.unpack("I", "".join([chr(x) for x in list(addr)[::-1]]).encode())[0] + 1
new_addr = []
for i in range(4):
new_addr.append(value % 256)