def read_stream(self, file: IO, data_stream: DataStream) -> Reply: '''Read from the data stream. Args: file: A destination file object or a stream writer. data_stream: The stream of which to read from. Coroutine. Returns: Reply: The final reply. ''' yield from data_stream.read_file(file=file) reply = yield from self._control_stream.read_reply() self.raise_if_not_match( 'End stream', ReplyCodes.closing_data_connection, reply ) data_stream.close() return reply
def test_control_stream(self): def log_cb(data_type, data): _logger.debug(__('{0}={1}', data_type, data)) connection = Connection(('127.0.0.1', self.server_port())) yield from connection.connect() control_stream = ControlStream(connection) control_stream.data_event_dispatcher.add_read_listener( functools.partial(log_cb, 'read')) control_stream.data_event_dispatcher.add_write_listener( functools.partial(log_cb, 'write')) reply = yield from control_stream.read_reply() self.assertEqual(220, reply.code) yield from control_stream.write_command(Command('USER', 'smaug')) reply = yield from control_stream.read_reply() self.assertEqual(331, reply.code) yield from control_stream.write_command(Command('PASS', 'gold1')) reply = yield from control_stream.read_reply() self.assertEqual(230, reply.code) yield from control_stream.write_command(Command('PASV')) reply = yield from control_stream.read_reply() self.assertEqual(227, reply.code) address = parse_address(reply.text) data_connection = Connection(address) yield from data_connection.connect() data_stream = DataStream(data_connection) yield from control_stream.write_command(Command('RETR', 'example (copy).txt')) reply = yield from control_stream.read_reply() self.assertEqual(150, reply.code) my_file = io.BytesIO() yield from data_stream.read_file(my_file) reply = yield from control_stream.read_reply() self.assertEqual(226, reply.code) self.assertEqual( 'The real treasure is in Smaug’s heart 💗.\n', my_file.getvalue().decode('utf-8') )
def test_control_stream(self): def log_cb(data_type, data): _logger.debug(__('{0}={1}', data_type, data)) connection = Connection(('127.0.0.1', self.server_port())) yield from connection.connect() control_stream = ControlStream(connection) control_stream.data_event_dispatcher.add_read_listener( functools.partial(log_cb, 'read')) control_stream.data_event_dispatcher.add_write_listener( functools.partial(log_cb, 'write')) reply = yield from control_stream.read_reply() self.assertEqual(220, reply.code) yield from control_stream.write_command(Command('USER', 'smaug')) reply = yield from control_stream.read_reply() self.assertEqual(331, reply.code) yield from control_stream.write_command(Command('PASS', 'gold1')) reply = yield from control_stream.read_reply() self.assertEqual(230, reply.code) yield from control_stream.write_command(Command('PASV')) reply = yield from control_stream.read_reply() self.assertEqual(227, reply.code) address = parse_address(reply.text) data_connection = Connection(address) yield from data_connection.connect() data_stream = DataStream(data_connection) yield from control_stream.write_command( Command('RETR', 'example (copy).txt')) reply = yield from control_stream.read_reply() self.assertEqual(150, reply.code) my_file = io.BytesIO() yield from data_stream.read_file(my_file) reply = yield from control_stream.read_reply() self.assertEqual(226, reply.code) self.assertEqual('The real treasure is in Smaug’s heart 💗.\n', my_file.getvalue().decode('utf-8'))
def read_stream(self, file: IO, data_stream: DataStream) -> Reply: '''Read from the data stream. Args: file: A destination file object or a stream writer. data_stream: The stream of which to read from. Coroutine. Returns: Reply: The final reply. ''' yield from data_stream.read_file(file=file) reply = yield from self._control_stream.read_reply() self.raise_if_not_match('End stream', ReplyCodes.closing_data_connection, reply) data_stream.close() return reply