def open_serial_connection(self, limit=None, **kwargs) ->\ Generator[int, None, Tuple[asyncio.StreamReader, asyncio.StreamWriter]]: """Open a serial connection using asyncio. A wrapper for create_serial_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to Serial(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader. This function is a coroutine. Args: loop: asyncio loop limit: line length limit """ if not limit: # pylint: disable-msg=protected-access limit = asyncio.streams._DEFAULT_LIMIT # type: ignore reader = asyncio.StreamReader(limit=limit, loop=self.loop) protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop) transport, _ = yield from create_serial_connection( loop=self.loop, protocol_factory=lambda: protocol, **kwargs) writer = asyncio.StreamWriter(transport, protocol, reader, self.loop) return reader, writer
def connect(self): loop = self._loop self._closed_fut = asyncio.Future(loop=loop) if self._secutity_protocol == "PLAINTEXT": ssl = None else: assert self._secutity_protocol == "SSL" assert self._ssl_context is not None ssl = self._ssl_context # Create streams same as `open_connection`, but using custom protocol reader = asyncio.StreamReader(limit=READER_LIMIT, loop=loop) protocol = AIOKafkaProtocol(self._closed_fut, reader, loop=loop) transport, _ = yield from asyncio.wait_for( loop.create_connection(lambda: protocol, self.host, self.port, ssl=ssl), loop=loop, timeout=self._request_timeout) writer = asyncio.StreamWriter(transport, protocol, reader, loop) self._reader, self._writer, self._protocol = reader, writer, protocol # Start reader task. self._read_task = ensure_future(self._read(), loop=loop) # Start idle checker if self._max_idle_ms is not None: self._idle_handle = self._loop.call_soon(self._idle_check) return reader, writer
async def start_tls(self, hostname: bytes, ssl_context: SSLContext, timeout: TimeoutDict) -> "SocketStream": loop = asyncio.get_event_loop() stream_reader = asyncio.StreamReader() protocol = asyncio.StreamReaderProtocol(stream_reader) transport = self.stream_writer.transport loop_start_tls = getattr(loop, "start_tls", backport_start_tls) transport = await asyncio.wait_for( loop_start_tls( transport, protocol, ssl_context, server_hostname=hostname.decode("ascii"), ), timeout=timeout.get("connect"), ) stream_reader.set_transport(transport) stream_writer = asyncio.StreamWriter(transport=transport, protocol=protocol, reader=stream_reader, loop=loop) ssl_stream = SocketStream(stream_reader, stream_writer) # When we return a new SocketStream with new StreamReader/StreamWriter instances # we need to keep references to the old StreamReader/StreamWriter so that they # are not garbage collected and closed while we're still using them. ssl_stream._inner = self # type: ignore return ssl_stream
async def connect(self): loop = self._loop self._closed_fut = create_future(loop=loop) if self._security_protocol in ["PLAINTEXT", "SASL_PLAINTEXT"]: ssl = None else: assert self._security_protocol in ["SSL", "SASL_SSL"] assert self._ssl_context is not None ssl = self._ssl_context # Create streams same as `open_connection`, but using custom protocol reader = asyncio.StreamReader(limit=READER_LIMIT, loop=loop) protocol = AIOKafkaProtocol(self._closed_fut, reader, loop=loop) transport, _ = await asyncio.wait_for( loop.create_connection( lambda: protocol, self.host, self.port, ssl=ssl), loop=loop, timeout=self._request_timeout) writer = asyncio.StreamWriter(transport, protocol, reader, loop) self._reader, self._writer, self._protocol = reader, writer, protocol # Start reader task. self._read_task = self._create_reader_task() # Start idle checker if self._max_idle_ms is not None: self._idle_handle = self._loop.call_soon( self._idle_check, weakref.ref(self)) if self._version_hint and self._version_hint >= (0, 10): await self._do_version_lookup() if self._security_protocol in ["SASL_SSL", "SASL_PLAINTEXT"]: await self._do_sasl_handshake() return reader, writer
async def create_async_stdin_stdout() -> Tuple[TextReader, TextWriter]: """ By default, `sys.stdin` and `sys.stdout` are synchronous channels: reading from `sys.stdin` or writing to `sys.stdout` will block until the read/write succeed, which is very different from the async socket channels created via `connect` or `connect_in_text_mode`. This function creates wrappers around `sys.stdin` and `sys.stdout` and makes them behave in the same way as other async socket channels. This makes it easier to write low-level-I/O-agonstic code, where the high-level logic does not need to worry about whether the underlying async I/O channel comes from sockets or from stdin/stdout. """ loop = asyncio.get_event_loop() stream_reader = asyncio.StreamReader(loop=loop) await loop.connect_read_pipe( lambda: asyncio.StreamReaderProtocol(stream_reader), sys.stdin) w_transport, w_protocol = await loop.connect_write_pipe( asyncio.streams.FlowControlMixin, sys.stdout) stream_writer = asyncio.StreamWriter(w_transport, w_protocol, stream_reader, loop) return ( TextReader(StreamBytesReader(stream_reader)), TextWriter(StreamBytesWriter(stream_writer)), )
def test_stream_writer_create_warning(self): reader = asyncio.StreamReader(loop=self.loop, _asyncio_internal=True) proto = asyncio.StreamReaderProtocol(reader, loop=self.loop, _asyncio_internal=True) with self.assertWarns(DeprecationWarning): asyncio.StreamWriter('transport', proto, reader, self.loop)
def make_stdio(self): """ Return (reader, writer) pair for sys.stdin, sys.stdout. This method is a coroutine. """ reader = asyncio.StreamReader() reader_protocol = asyncio.StreamReaderProtocol(reader) # Thanks: # # https://gist.github.com/nathan-hoad/8966377 # # After some experimentation, this 'sameopenfile' conditional seems # allow us to handle stdin as a pipe or a keyboard. In the case of # a tty, 0 and 1 are the same open file, we use: # # https://github.com/orochimarufan/.files/blob/master/bin/mpr write_fobj = sys.stdout if self._istty: write_fobj = sys.stdin writer_transport, writer_protocol = yield from ( self.loop.connect_write_pipe(asyncio.streams.FlowControlMixin, write_fobj)) writer = asyncio.StreamWriter(writer_transport, writer_protocol, None, self.loop) yield from self.loop.connect_read_pipe(lambda: reader_protocol, sys.stdin) return reader, writer
async def _start_tls(self, ssl_context: ssl.SSLContext, ssl_timeout: float) -> None: loop = asyncio.get_event_loop() ssl_reader = asyncio.StreamReader() protocol = asyncio.StreamReaderProtocol(ssl_reader) transport = await loop.start_tls( self.__writer.transport, protocol, ssl_context, server_side=True, ssl_handshake_timeout=ssl_timeout, ) ssl_reader.set_transport(transport) ssl_writer = asyncio.StreamWriter( transport=transport, protocol=protocol, reader=ssl_reader, loop=loop, ) self.__reader = ssl_reader self.__writer = ssl_writer
async def stdout_writer(): """ This is a bit complex, as stdout can be a pipe or a file. If it is a file, we cannot use :meth:`asycnio.BaseEventLoop.connect_write_pipe`. """ if sys.stdout.seekable(): # it’s a file return sys.stdout.buffer.raw if os.isatty(sys.stdin.fileno()): # it’s a tty, use fd 0 fd_to_use = 0 else: fd_to_use = 1 twrite, pwrite = await loop.connect_write_pipe( asyncio.streams.FlowControlMixin, os.fdopen(fd_to_use, "wb"), ) swrite = asyncio.StreamWriter( twrite, pwrite, None, loop, ) return swrite
def open_serial_connection(*, loop=None, limit=asyncio.streams._DEFAULT_LIMIT, **kwargs): """A wrapper for create_serial_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to Serial(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader. This function is a coroutine. """ if loop is None: loop = asyncio.get_event_loop() reader = asyncio.StreamReader(limit=limit, loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = yield from create_serial_connection( loop=loop, protocol_factory=lambda: protocol, **kwargs) writer = asyncio.StreamWriter(transport, protocol, reader, loop) return reader, writer
async def create_pipe_writer(path): loop = asyncio.get_event_loop() protocol = FlowControlMixin() pipe = open(path, "w") transport, _ = await loop.connect_write_pipe(lambda: protocol, pipe) writer = asyncio.StreamWriter(transport, protocol, None, loop) # Return reader and writers return writer
async def open_connection(host, port, create_connection, ssl=None): reader = asyncio.StreamReader() protocol = asyncio.StreamReaderProtocol(reader) transport, _ = await create_connection(lambda: protocol, host, port, ssl=ssl) writer = asyncio.StreamWriter(transport, protocol, reader, asyncio.get_event_loop()) return reader, writer
def _create_stream( self, stream_id: int ) -> Tuple[asyncio.StreamReader, asyncio.StreamWriter]: adapter = QuicStreamAdapter(self, stream_id) reader = asyncio.StreamReader() writer = asyncio.StreamWriter(adapter, None, reader, self._loop) self._stream_readers[stream_id] = reader return reader, writer
async def open_connection(self, host=None, port=None, **kwargs): loop = asyncio.get_event_loop() reader = asyncio.StreamReader(loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = await self.create_connection(lambda: protocol, host, port, **kwargs) writer = asyncio.StreamWriter(transport, protocol, reader, loop) return reader, writer
def connection_made(self, transport): self.transport = transport stdin_transport = self.transport.get_pipe_transport(0) self.stdin = asyncio.StreamWriter(stdin_transport, protocol=self, reader=None, loop=asyncio.get_event_loop())
async def setup_writer(self, loop): # Create writer # (see https://gist.github.com/oconnor663/08c081904264043e55bf) write_protocol = asyncio.StreamReaderProtocol(asyncio.StreamReader()) write_transport, _ = await loop.connect_write_pipe( lambda: write_protocol, os.fdopen(self.master, 'w')) self.writer = asyncio.StreamWriter(write_transport, write_protocol, None, loop)
async def f(): write_protocol = asyncio.StreamReaderProtocol(asyncio.StreamReader()) dest = open(fd, 'wb', 0) loop = asyncio.get_running_loop() (transport, p) = await loop.connect_write_pipe(lambda: write_protocol, dest) writer = asyncio.StreamWriter(transport, write_protocol, None, loop) return writer
async def connect_stdin_stdout(): loop = asyncio.get_event_loop() reader = asyncio.StreamReader() protocol = asyncio.StreamReaderProtocol(reader) dummy = asyncio.Protocol() await loop.connect_read_pipe(lambda: protocol, sys.stdin) w_transport, _ = await loop.connect_write_pipe(lambda: dummy, sys.stdout) writer = asyncio.StreamWriter(w_transport, protocol, reader, loop) return reader, writer
def connect(self): wtransport, wprotocol = yield from self.loop.connect_write_pipe( asyncio.Protocol, self.port) self.reader = asyncio.StreamReader(loop=self.loop) rprotocol = asyncio.StreamReaderProtocol(self.reader, loop=self.loop) rtransport, _ = yield from self.loop.connect_read_pipe( lambda: rprotocol, self.port) self.writer = asyncio.StreamWriter(wtransport, wprotocol, self.reader, self.loop)
async def open_connection(host, port, loop, create_connection, ssl=None): reader = asyncio.StreamReader(loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = await create_connection(lambda: protocol, host, port, ssl=ssl) writer = asyncio.StreamWriter(transport, protocol, reader, loop) return reader, writer
async def _connect_stdin_stdout(loop): reader = asyncio.StreamReader() protocol = asyncio.StreamReaderProtocol(reader) await loop.connect_read_pipe(lambda: protocol, sys.stdin) w_transport, w_protocol = await loop.connect_write_pipe( asyncio.streams.FlowControlMixin, sys.stdout) writer = asyncio.StreamWriter(w_transport, w_protocol, reader, loop) return reader, writer
async def test_send_message_to_leader(node_instance, message_instance): node_instance.leader_writer = asyncio.StreamWriter(None, None, None, None) with patch('asyncio.StreamWriter.drain', new=CoroutineMock()) as mocked_drain: with patch('asyncio.StreamWriter.write') as mocked_write: await node_instance.send_message_to_leader(message_instance) mocked_write.assert_called_once_with( message_instance.convert_to_string().encode()) mocked_drain.assert_called_once()
def connection_made(self, transport: asyncio.Transport): """ Called upon a connection being made. This will automatically call your BFHandler.on_connection(). :param transport: The transport to set the streamreader/streamwriter to. """ self._streamreader.set_transport(transport) self._streamwriter = asyncio.StreamWriter(transport, self, self._streamreader, self._loop) super().connection_made(transport)
def get_directly_connected_streams(alice_extra_info: Dict[str, Any] = None, bob_extra_info: Dict[str, Any] = None) -> TConnectedStreams: alice_reader = asyncio.StreamReader() bob_reader = asyncio.StreamReader() alice_transport = MemoryWriteTransport(bob_reader, extra=alice_extra_info) bob_transport = MemoryWriteTransport(alice_reader, extra=bob_extra_info) alice_protocol = MemoryProtocol() bob_protocol = MemoryProtocol() # Link the alice's writer to the bob's reader, and the bob's writer to the # alice's reader. bob_writer = asyncio.StreamWriter(bob_transport, bob_protocol, alice_reader, loop=None) alice_writer = asyncio.StreamWriter(alice_transport, alice_protocol, bob_reader, loop=None) return ( (alice_reader, alice_writer), (bob_reader, bob_writer), )
def _rewrap_socket(self, new_socket): loop = asyncio.get_event_loop() reader = asyncio.StreamReader(loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, dummy = yield from loop.create_connection( lambda: protocol, sock=new_socket) writer = asyncio.StreamWriter(transport, protocol, reader, loop) self._reader = reader self._writer = writer
def test_isalive(asynctelnet_transport): # lie like connection is open asynctelnet_transport.stdout = asyncio.StreamReader( loop=asyncio.get_event_loop_policy().get_event_loop() ) # make a stupid streamwriter... just enough to instantiate a real one :) asynctelnet_transport.stdin = asyncio.StreamWriter( BytesIO(), "", None, asyncio.get_event_loop_policy().get_event_loop() ) assert asynctelnet_transport.isalive() is True
def connection_made(self, transport): peername = transport.get_extra_info('peername') print('Connection from {}'.format(peername)) self.transport = transport self._connection_lost = False self._paused = False self._stream_writer = asyncio.StreamWriter( transport, self, self._stream_reader, self.loop) asyncio.ensure_future(self.init_connection()) asyncio.ensure_future(self.get_response())
def open_unix_connection(address, *, limit, loop=None, parser=None, **kwds): # XXX: parser is not used (yet) if loop is None: loop = asyncio.get_event_loop() reader = StreamReader(limit=limit, loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = yield from loop.create_unix_connection( lambda: protocol, address, **kwds) writer = asyncio.StreamWriter(transport, protocol, reader, loop) return reader, writer
async def connect(self): loop = asyncio.get_event_loop() self.reader = asyncio.StreamReader(loop=loop, limit=4 * 1024 * 1024) reader_protocol = asyncio.StreamReaderProtocol(self.reader, loop=loop) transport, _ = await loop.create_pipe_connection( lambda: reader_protocol, self.address) self.writer = asyncio.StreamWriter(transport, reader_protocol, self.reader, loop)
async def open_unix_connection(path=None, *, limit=2**16, **kwds): """Similar to `open_connection` but works with UNIX Domain Sockets.""" loop = aio.events.get_running_loop() reader = aio.StreamReader(limit=limit, loop=loop) protocol = aio.StreamReaderProtocol(reader, loop=loop) transport, _ = await create_unix_connection(loop, lambda: protocol, path, **kwds) writer = aio.StreamWriter(transport, protocol, reader, loop) return reader, writer