From 07a12b66e8b7950cfeee36feaf163689e4c66c28 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 17 Jul 2018 21:34:53 -0300 Subject: [PATCH] port dht.contacts ~> py3 --- lbrynet/dht/contact.py | 11 +++++++---- tests/unit/dht/test_contact.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lbrynet/dht/contact.py b/lbrynet/dht/contact.py index 2df93a675..f84670e7f 100644 --- a/lbrynet/dht/contact.py +++ b/lbrynet/dht/contact.py @@ -1,10 +1,13 @@ import ipaddress +from binascii import hexlify +from functools import reduce + from lbrynet.dht import constants def is_valid_ipv4(address): try: - ip = ipaddress.ip_address(address.decode()) # this needs to be unicode, thus the decode() + ip = ipaddress.ip_address(address.encode().decode()) # this needs to be unicode, thus re-encode-able return ip.version == 4 except ipaddress.AddressValueError: return False @@ -20,7 +23,7 @@ class _Contact(object): def __init__(self, contactManager, id, ipAddress, udpPort, networkProtocol, firstComm): if id is not None: if not len(id) == constants.key_bits / 8: - raise ValueError("invalid node id: %s" % id.encode('hex')) + raise ValueError("invalid node id: %s" % hexlify(id.encode())) if not 0 <= udpPort <= 65536: raise ValueError("invalid port") if not is_valid_ipv4(ipAddress): @@ -113,7 +116,7 @@ class _Contact(object): def compact_ip(self): compact_ip = reduce( lambda buff, x: buff + bytearray([int(x)]), self.address.split('.'), bytearray()) - return str(compact_ip) + return compact_ip def set_id(self, id): if not self._id: @@ -171,7 +174,7 @@ class ContactManager(object): self._rpc_failures = {} def get_contact(self, id, address, port): - for contact in self._contacts.itervalues(): + for contact in self._contacts.values(): if contact.id == id and contact.address == address and contact.port == port: return contact diff --git a/tests/unit/dht/test_contact.py b/tests/unit/dht/test_contact.py index 9a6b3cf55..9ad4fec99 100644 --- a/tests/unit/dht/test_contact.py +++ b/tests/unit/dht/test_contact.py @@ -52,8 +52,8 @@ class ContactOperatorsTest(unittest.TestCase): msg.format('ne', type(item).__name__)) def testCompactIP(self): - self.assertEqual(self.firstContact.compact_ip(), '\x7f\x00\x00\x01') - self.assertEqual(self.secondContact.compact_ip(), '\xc0\xa8\x00\x01') + self.assertEqual(self.firstContact.compact_ip(), b'\x7f\x00\x00\x01') + self.assertEqual(self.secondContact.compact_ip(), b'\xc0\xa8\x00\x01') class TestContactLastReplied(unittest.TestCase):