def test_streamreaderprotocol_constructor(self): self.addCleanup(asyncio.set_event_loop, None) asyncio.set_event_loop(self.loop) # asyncio issue #184: Ensure that StreamReaderProtocol constructor # retrieves the current loop if the loop parameter is not set reader = mock.Mock() protocol = asyncio.StreamReaderProtocol(reader) self.assertIs(protocol._loop, self.loop)
def _start_tls(self): '''Start SSL protocol on the socket.''' socket_ = self._writer.get_extra_info('socket') try: trollius.get_event_loop().remove_reader(socket_.fileno()) except ValueError as error: raise trollius.ConnectionAbortedError() from error self._writer.write(b'HTTP/1.1 200 Connection established\r\n\r\n') yield From(self._writer.drain()) try: trollius.get_event_loop().remove_writer(socket_.fileno()) except ValueError as error: raise trollius.ConnectionAbortedError() from error ssl_socket = ssl.wrap_socket(socket_, server_side=True, certfile=self._cert_filename, keyfile=self._key_filename, do_handshake_on_connect=False) # FIXME: this isn't how to START TLS for dummy in range(1200): try: ssl_socket.do_handshake() break except ssl.SSLError as error: if error.errno in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): _logger.debug('Do handshake %s', error) yield From(trollius.sleep(0.05)) else: raise else: _logger.error(_('Unable to handshake.')) ssl_socket.close() self._reject_request('Could not start TLS') raise trollius.ConnectionAbortedError('Could not start TLS') loop = trollius.get_event_loop() reader = trollius.StreamReader(loop=loop) protocol = trollius.StreamReaderProtocol(reader, loop=loop) transport, dummy = yield From( loop.create_connection(lambda: protocol, sock=ssl_socket)) writer = trollius.StreamWriter(transport, protocol, reader, loop) self._reader = reader self._writer = writer
def task(): rfd, wfd = os.pipe() args = [sys.executable, '-c', code, str(wfd)] pipe = os.fdopen(rfd, 'rb', 0) reader = asyncio.StreamReader(loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = yield From(loop.connect_read_pipe(lambda: protocol, pipe)) kwds = {} if sys.version_info >= (3, 2): kwds['pass_fds'] = (wfd, ) proc = yield From(asyncio.create_subprocess_exec(*args, **kwds)) yield From(proc.wait()) os.close(wfd) data = yield From(reader.read()) print("read = %r" % data.decode())
def test_read_all_from_pipe_reader(self): # See asyncio issue 168. This test is derived from the example # subprocess_attach_read_pipe.py, but we configure the # StreamReader's limit so that twice it is less than the size # of the data writter. Also we must explicitly attach a child # watcher to the event loop. code = """\ import os, sys fd = int(sys.argv[1]) os.write(fd, b'data') os.close(fd) """ rfd, wfd = os.pipe() args = [sys.executable, '-c', code, str(wfd)] pipe = io.open(rfd, 'rb', 0) reader = asyncio.StreamReader(loop=self.loop, limit=1) protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop) transport, _ = self.loop.run_until_complete( self.loop.connect_read_pipe(lambda: protocol, pipe)) watcher = asyncio.SafeChildWatcher() watcher.attach_loop(self.loop) try: asyncio.set_child_watcher(watcher) kw = {'loop': self.loop} if six.PY3: kw['pass_fds'] = set((wfd, )) create = asyncio.create_subprocess_exec(*args, **kw) proc = self.loop.run_until_complete(create) self.loop.run_until_complete(proc.wait()) finally: asyncio.set_child_watcher(None) os.close(wfd) data = self.loop.run_until_complete(reader.read(-1)) self.assertEqual(data, b'data')
def _test_pipe(self): ADDRESS = r'\\.\pipe\_test_pipe-%s' % os.getpid() with self.assertRaises(FileNotFoundError): yield From(self.loop.create_pipe_connection( asyncio.Protocol, ADDRESS)) [server] = yield From(self.loop.start_serving_pipe( UpperProto, ADDRESS)) self.assertIsInstance(server, windows_events.PipeServer) clients = [] for i in range(5): stream_reader = asyncio.StreamReader(loop=self.loop) protocol = asyncio.StreamReaderProtocol(stream_reader, loop=self.loop) trans, proto = yield From(self.loop.create_pipe_connection( lambda: protocol, ADDRESS)) self.assertIsInstance(trans, asyncio.Transport) self.assertEqual(protocol, proto) clients.append((stream_reader, trans)) for i, (r, w) in enumerate(clients): w.write('lower-{0}\n'.format(i).encode()) for i, (r, w) in enumerate(clients): response = yield From(r.readline()) self.assertEqual(response, 'LOWER-{0}\n'.format(i).encode()) w.close() server.close() with self.assertRaises(FileNotFoundError): yield From(self.loop.create_pipe_connection( asyncio.Protocol, ADDRESS)) raise Return('done')
def factory(): return asyncio.StreamReaderProtocol(stream_reader)