From 8c3552dd34314212d2682c19ae9f97778cbe1d59 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 10 Aug 2018 00:49:17 -0300 Subject: [PATCH] port reflector --- lbrynet/reflector/client/blob.py | 8 ++++---- lbrynet/reflector/server/server.py | 8 ++++---- tests/functional/test_reflector.py | 10 ++++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lbrynet/reflector/client/blob.py b/lbrynet/reflector/client/blob.py index d2533cb02..ccb487168 100644 --- a/lbrynet/reflector/client/blob.py +++ b/lbrynet/reflector/client/blob.py @@ -16,7 +16,7 @@ class BlobReflectorClient(Protocol): def connectionMade(self): self.blob_manager = self.factory.blob_manager - self.response_buff = '' + self.response_buff = b'' self.outgoing_buff = '' self.blob_hashes_to_send = self.factory.blobs self.next_blob_to_send = None @@ -39,7 +39,7 @@ class BlobReflectorClient(Protocol): except IncompleteResponse: pass else: - self.response_buff = '' + self.response_buff = b'' d = self.handle_response(msg) d.addCallback(lambda _: self.send_next_request()) d.addErrback(self.response_failure_handler) @@ -73,7 +73,7 @@ class BlobReflectorClient(Protocol): def send_handshake(self): log.debug('Sending handshake') - self.write(json.dumps({'version': self.protocol_version})) + self.write(json.dumps({'version': self.protocol_version}).encode()) return defer.succeed(None) def parse_response(self, buff): @@ -150,7 +150,7 @@ class BlobReflectorClient(Protocol): self.write(json.dumps({ 'blob_hash': self.next_blob_to_send.blob_hash, 'blob_size': self.next_blob_to_send.length - })) + }).encode()) def disconnect(self, err): self.transport.loseConnection() diff --git a/lbrynet/reflector/server/server.py b/lbrynet/reflector/server/server.py index c2ac4a3b6..a5604d204 100644 --- a/lbrynet/reflector/server/server.py +++ b/lbrynet/reflector/server/server.py @@ -40,7 +40,7 @@ class ReflectorServer(Protocol): self.receiving_blob = False self.incoming_blob = None self.blob_finished_d = None - self.request_buff = "" + self.request_buff = b"" self.blob_writer = None @@ -52,7 +52,7 @@ class ReflectorServer(Protocol): self.transport.loseConnection() def send_response(self, response_dict): - self.transport.write(json.dumps(response_dict)) + self.transport.write(json.dumps(response_dict).encode()) ############################ # Incoming blob file stuff # @@ -122,7 +122,7 @@ class ReflectorServer(Protocol): self.request_buff += data msg, extra_data = self._get_valid_response(self.request_buff) if msg is not None: - self.request_buff = '' + self.request_buff = b'' d = self.handle_request(msg) d.addErrback(self.handle_error) if self.receiving_blob and extra_data: @@ -134,7 +134,7 @@ class ReflectorServer(Protocol): response = None curr_pos = 0 while not self.receiving_blob: - next_close_paren = response_msg.find('}', curr_pos) + next_close_paren = response_msg.find(b'}', curr_pos) if next_close_paren != -1: curr_pos = next_close_paren + 1 try: diff --git a/tests/functional/test_reflector.py b/tests/functional/test_reflector.py index 306aaee23..ac178ce2f 100644 --- a/tests/functional/test_reflector.py +++ b/tests/functional/test_reflector.py @@ -1,4 +1,6 @@ import os +from binascii import hexlify + from twisted.internet import defer, error from twisted.trial import unittest from lbrynet.core.StreamDescriptor import get_sd_info @@ -81,12 +83,12 @@ class TestReflector(unittest.TestCase): return d def create_stream(): - test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in range(0, 64, 6)])) + test_file = mocks.GenFile(5209343, bytes([(i + 3) for i in range(0, 64, 6)])) d = EncryptedFileCreator.create_lbry_file( self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager, "test_file", test_file, - key="0123456701234567", + key=b"0123456701234567", iv_generator=iv_generator() ) d.addCallback(lambda lbry_file: lbry_file.stream_hash) @@ -165,7 +167,7 @@ class TestReflector(unittest.TestCase): self.assertEqual(1, len(streams)) stream_info = yield self.server_storage.get_stream_info(self.stream_hash) self.assertEqual(self.sd_hash, stream_info[3]) - self.assertEqual('test_file'.encode('hex'), stream_info[0]) + self.assertEqual(hexlify(b'test_file').decode(), stream_info[0]) # check should_announce blobs on blob_manager blob_hashes = yield self.server_storage.get_all_should_announce_blobs() @@ -334,4 +336,4 @@ def iv_generator(): iv = 0 while True: iv += 1 - yield "%016d" % iv + yield b"%016d" % iv