我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用asyncio.Transport()。
def test_flowcontrol_mixin_set_write_limits(self): class MyTransport(transports._FlowControlMixin, transports.Transport): def get_write_buffer_size(self): return 512 loop = mock.Mock() transport = MyTransport(loop=loop) transport._protocol = mock.Mock() self.assertFalse(transport._protocol_paused) with self.assertRaisesRegex(ValueError, 'high.*must be >= low'): transport.set_write_buffer_limits(high=0, low=1) transport.set_write_buffer_limits(high=1024, low=128) self.assertFalse(transport._protocol_paused) self.assertEqual(transport.get_write_buffer_limits(), (128, 1024)) transport.set_write_buffer_limits(high=256, low=128) self.assertTrue(transport._protocol_paused) self.assertEqual(transport.get_write_buffer_limits(), (128, 256))
def connection_made(self, transport): """ A peer is now connected and we receive an instance of the underlying :class:`asyncio.Transport`. We :class:`asyncio.StreamReader` is created and the transport is associated before the initial HTTP handshake is undertaken. """ #self.transport = transport #self.stream = asyncio.StreamReader() #self.stream.set_transport(transport) asyncio.StreamReaderProtocol.connection_made(self, transport) # Let make it concurrent for others to tag along f = asyncio.async(self.handle_initial_handshake()) f.add_done_callback(self.terminated)
def connect_to_device(self, protocol_factory, device_id, port): '''Open a TCP connection to a port on a device. Args: protocol_factory (callable): A callable that returns an asyncio.Protocol implementation device_id (int): The id of the device to connect to port (int): The port to connect to on the target device Returns: (dict, asyncio.Transport, asyncio.Protocol): The device information, connected transport and protocol. Raises: A USBMuxError subclass instance such as ConnectionRefused ''' waiter = asyncio.Future(loop=self.loop) connector = USBMuxConnector(device_id, port, waiter) transport, switcher = await self._connect_transport(lambda: _ProtoSwitcher(self.loop, connector)) # wait for the connection to succeed or fail await waiter app_protocol = switcher.switch_protocol(protocol_factory) device_info = self.attached.get(device_id) or {} return device_info, transport, app_protocol
def test_stream_response_writes_correct_content_to_transport(streaming_app): response = StreamingHTTPResponse(sample_streaming_fn) response.transport = MagicMock(asyncio.Transport) @streaming_app.listener('after_server_start') async def run_stream(app, loop): await response.stream() assert response.transport.write.call_args_list[1][0][0] == ( b'4\r\nfoo,\r\n' ) assert response.transport.write.call_args_list[2][0][0] == ( b'3\r\nbar\r\n' ) assert response.transport.write.call_args_list[3][0][0] == ( b'0\r\n\r\n' ) app.stop() streaming_app.run(host=HOST, port=PORT)
def test_create_datagram_endpoint_sock(self): sock = None local_address = ('127.0.0.1', 0) infos = self.loop.run_until_complete( self.loop.getaddrinfo( *local_address, type=socket.SOCK_DGRAM)) for family, type, proto, cname, address in infos: try: sock = socket.socket(family=family, type=type, proto=proto) sock.setblocking(False) sock.bind(address) except: pass else: break else: assert False, 'Can not create socket.' f = self.loop.create_connection( lambda: MyDatagramProto(loop=self.loop), sock=sock) tr, pr = self.loop.run_until_complete(f) self.assertIsInstance(tr, asyncio.Transport) self.assertIsInstance(pr, MyDatagramProto) tr.close() self.loop.run_until_complete(pr.done)
def test_flowcontrol_mixin_set_write_limits(self): class MyTransport(transports._FlowControlMixin, transports.Transport): def get_write_buffer_size(self): return 512 transport = MyTransport() transport._protocol = mock.Mock() self.assertFalse(transport._protocol_paused) with self.assertRaisesRegex(ValueError, 'high.*must be >= low'): transport.set_write_buffer_limits(high=0, low=1) transport.set_write_buffer_limits(high=1024, low=128) self.assertFalse(transport._protocol_paused) self.assertEqual(transport.get_write_buffer_limits(), (128, 1024)) transport.set_write_buffer_limits(high=256, low=128) self.assertTrue(transport._protocol_paused) self.assertEqual(transport.get_write_buffer_limits(), (128, 256))
def _basetest_create_connection(self, connection_fut, check_sockname=True): tr, pr = self.loop.run_until_complete(connection_fut) self.assertIsInstance(tr, asyncio.Transport) self.assertIsInstance(pr, asyncio.Protocol) self.assertIs(pr.transport, tr) if check_sockname: self.assertIsNotNone(tr.get_extra_info('sockname')) self.loop.run_until_complete(pr.done) self.assertGreater(pr.nbytes, 0) tr.close()
def test_create_connection_sock(self): with test_utils.run_test_server() as httpd: sock = None infos = self.loop.run_until_complete( self.loop.getaddrinfo( *httpd.address, type=socket.SOCK_STREAM)) for family, type, proto, cname, address in infos: try: sock = socket.socket(family=family, type=type, proto=proto) sock.setblocking(False) self.loop.run_until_complete( self.loop.sock_connect(sock, address)) except: pass else: break else: assert False, 'Can not create socket.' f = self.loop.create_connection( lambda: MyProto(loop=self.loop), sock=sock) tr, pr = self.loop.run_until_complete(f) self.assertIsInstance(tr, asyncio.Transport) self.assertIsInstance(pr, asyncio.Protocol) self.loop.run_until_complete(pr.done) self.assertGreater(pr.nbytes, 0) tr.close()
def _basetest_create_ssl_connection(self, connection_fut, check_sockname=True): tr, pr = self.loop.run_until_complete(connection_fut) self.assertIsInstance(tr, asyncio.Transport) self.assertIsInstance(pr, asyncio.Protocol) self.assertTrue('ssl' in tr.__class__.__name__.lower()) if check_sockname: self.assertIsNotNone(tr.get_extra_info('sockname')) self.loop.run_until_complete(pr.done) self.assertGreater(pr.nbytes, 0) tr.close()
def test_ctor_extra_is_none(self): transport = asyncio.Transport() self.assertEqual(transport._extra, {})
def test_writelines(self): transport = asyncio.Transport() transport.write = mock.Mock() transport.writelines([b'line1', bytearray(b'line2'), memoryview(b'line3')]) self.assertEqual(1, transport.write.call_count) transport.write.assert_called_with(b'line1line2line3')
def test_not_implemented(self): transport = asyncio.Transport() self.assertRaises(NotImplementedError, transport.set_write_buffer_limits) self.assertRaises(NotImplementedError, transport.get_write_buffer_size) self.assertRaises(NotImplementedError, transport.write, 'data') self.assertRaises(NotImplementedError, transport.write_eof) self.assertRaises(NotImplementedError, transport.can_write_eof) self.assertRaises(NotImplementedError, transport.pause_reading) self.assertRaises(NotImplementedError, transport.resume_reading) self.assertRaises(NotImplementedError, transport.close) self.assertRaises(NotImplementedError, transport.abort)
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-{}\n'.format(i).encode()) for i, (r, w) in enumerate(clients): response = yield from r.readline() self.assertEqual(response, 'LOWER-{}\n'.format(i).encode()) w.close() server.close() with self.assertRaises(FileNotFoundError): yield from self.loop.create_pipe_connection( asyncio.Protocol, ADDRESS) return 'done'
def setUp(self): self._ipa = OsmoIPAServer(None) # Mock the writes to check responses self._writes = Mock() def convert_memview_to_bytes(memview): """ Deep copy the memoryview for checking later """ return self._writes(memview.tobytes()) self._transport = asyncio.Transport() self._transport.write = Mock(side_effect=convert_memview_to_bytes) # Here goes nothing.. self._ipa.connection_made(self._transport)
def connection_made(self, transport: asyncio.Transport): self.transport = transport self.client_cert = transport.get_extra_info("peercert") ssl_context = transport.get_extra_info("ssl_object") if ssl.HAS_ALPN: agreed_protocol = ssl_context.selected_alpn_protocol() else: agreed_protocol = ssl_context.selected_npn_protocol() if agreed_protocol is None: error("Connection error, client does not support HTTP/2") self.transport.close() else: self.conn.initiate_connection()
def connect_to_first_device(self, protocol_factory, port, timeout=DEFAULT_MAX_WAIT, include=None, exclude=None): '''Open a TCP connection to the first device that has the requested port open. Args: protocol_factory (callable): A callable that returns an asyncio.Protocol implementation. port (int): The port to connect to on the target device. timeout (float): The maximum amount of time to wait for a suitable device to be connected. Returns: (dict, asyncio.Transport, asyncio.Protocol): The device information, connected transport and protocol. Raises: asyncio.TimeoutError if no devices with the requested port become available in the specified time. ''' with self.attach_watcher(include_existing=True) as watcher: timeout = Timeout(timeout) while not timeout.expired: action, device_id, info = await watcher.wait_for_next(timeout.remaining) if action != ACTION_ATTACHED: continue if exclude is not None and device_id in exclude: continue if include is not None and device_id not in include: continue try: return await self.connect_to_device(protocol_factory, device_id, port) except USBMuxError: pass raise asyncio.TimeoutError("No available devices")
def __init__(self, loop: asyncio.BaseEventLoop, dispatcher: AbstractDispatcher, *, request_timeout: int=15): """ This is a specialized HTTP protocol meant to be a low-latency API Gateway. For the HTTP dispatcher this means that data in/out will be streamed each direction, other dispatchers may require full request/response bodies be read into memory. :param loop: event loop :param dispatcher: dispatcher strategy, should implement the methods defined in the AbstractDispatcher :param request_timeout: Max length of a request cycle in secs (def: 15s) """ self.dispatcher = dispatcher self.loop = loop self.request_timeout = request_timeout self.parser = None # httptools.HttpRequestParser self.transport = None # type: Optional[asyncio.Transport] self.reader = None # type: Optional[asyncio.StreamReader] self.timeout = None # type: Optional[Task] # request info self.url = None # type: Optional[str] self.headers = None # type: Optional[List[Tuple[bytes, bytes]]] # =========================== # asyncio.Protocol callbacks # ===========================
def connection_made(self, transport: asyncio.Transport) -> None: self.transport = transport self.parser = httptools.HttpRequestParser(self) self.reader = asyncio.StreamReader(loop=self.loop) self.start_timeout()
def transport(self) -> asyncio.Transport: # TODO: remove this being exposed. return self._transport
def connection_made(self, transport: asyncio.Transport): peername = transport.get_extra_info('peername') logger.info('Connection from %s', peername) self.transport = transport self.data_in = Subject() self.data_out = AnonymousObserver(self.on_data_out_next, self.on_data_out_error, self.on_data_out_completed) self.on_connect(self)
def _basetest_create_ssl_connection(self, connection_fut, check_sockname=True, peername=None): tr, pr = self.loop.run_until_complete(connection_fut) self.assertIsInstance(tr, asyncio.Transport) self.assertIsInstance(pr, asyncio.Protocol) self.assertTrue('ssl' in tr.__class__.__name__.lower()) self.check_ssl_extra_info(tr, check_sockname, peername) self.loop.run_until_complete(pr.done) self.assertGreater(pr.nbytes, 0) tr.close()
def test_get_extra_info(self): transport = asyncio.Transport({'extra': 'info'}) self.assertEqual('info', transport.get_extra_info('extra')) self.assertIsNone(transport.get_extra_info('unknown')) default = object() self.assertIs(default, transport.get_extra_info('unknown', default))
def __init__(self, authority: str, logger: Optional[BoundLogger], on_close: Callable[[], None]): self.authority = authority self.logger = logger or wrap_logger(PrintLogger(open(os.devnull, 'w'))) self.on_close = on_close self.conn = H2Connection() self.transport: Union[Transport, None] = None self.responses: Dict[int, PendingResponse] = {}
def connection_made(self, transport: Transport): self.logger.debug('connected') self.transport = transport self.conn.initiate_connection() # This reproduces the error in #396, by changing the header table size. self.conn.update_settings({SettingsFrame.HEADER_TABLE_SIZE: SIZE}) self.transport.write(self.conn.data_to_send())
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) 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-{}\n'.format(i).encode()) for i, (r, w) in enumerate(clients): response = yield from r.readline() self.assertEqual(response, 'LOWER-{}\n'.format(i).encode()) w.close() server.close() with self.assertRaises(FileNotFoundError): yield from self.loop.create_pipe_connection( asyncio.Protocol, ADDRESS) return 'done'
def test_create_datagram_endpoint(self): class TestMyDatagramProto(MyDatagramProto): def __init__(inner_self): super().__init__(loop=self.loop) def datagram_received(self, data, addr): super().datagram_received(data, addr) self.transport.sendto(b'resp:'+data, addr) coro = self.loop.create_datagram_endpoint( TestMyDatagramProto, local_addr=('127.0.0.1', 0)) s_transport, server = self.loop.run_until_complete(coro) host, port = s_transport.get_extra_info('sockname') self.assertIsInstance(s_transport, asyncio.Transport) self.assertIsInstance(server, TestMyDatagramProto) self.assertEqual('INITIALIZED', server.state) self.assertIs(server.transport, s_transport) coro = self.loop.create_datagram_endpoint( lambda: MyDatagramProto(loop=self.loop), remote_addr=(host, port)) transport, client = self.loop.run_until_complete(coro) self.assertIsInstance(transport, asyncio.Transport) self.assertIsInstance(client, MyDatagramProto) self.assertEqual('INITIALIZED', client.state) self.assertIs(client.transport, transport) transport.sendto(b'xxx') test_utils.run_until(self.loop, lambda: server.nbytes) self.assertEqual(3, server.nbytes) test_utils.run_until(self.loop, lambda: client.nbytes) # received self.assertEqual(8, client.nbytes) # extra info is available self.assertIsNotNone(transport.get_extra_info('sockname')) # close connection transport.close() self.loop.run_until_complete(client.done) self.assertEqual('CLOSED', client.state) server.transport.close()
def run_request_handler(self, headers: OrderedDict, stream_id: int, data: Optional[str]): url_path = headers[":path"].split("?")[0].rstrip("/") method = headers[":method"] ################ # if url_path == "/evtest": # self.ev_stream_start_response(stream_id) # i = 0 # # def cb(): # self.ev_stream_send_data("ahoj\ncau\n1", stream_id) # # nonlocal i # i += 1 # if i < 5: # self.LOOP.call_later(1, cb) # elif stream_id in self.conn.streams.keys(): # self.conn.end_stream(stream_id) # # dts = self.conn.data_to_send() # if dts: # self.transport.write(dts) # # cb() # return ############### if method == "HEAD": h = self.HTTP_HANDLERS.list.get("GET", url_path) else: h = self.HTTP_HANDLERS.list.get(method, url_path) if not h: self.send_response( HttpResponse.error(HttpStatus.BadRequest, RestconfErrType.Transport, ERRTAG_MALFORMED), stream_id ) else: # Run handler and send HTTP response resp = h(headers, data, self.client_cert) if method == "HEAD": resp.data = bytes() self.send_response(resp, stream_id)