當前位置: 首頁>>代碼示例>>Python>>正文


Python asyncio.DatagramTransport方法代碼示例

本文整理匯總了Python中asyncio.DatagramTransport方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.DatagramTransport方法的具體用法?Python asyncio.DatagramTransport怎麽用?Python asyncio.DatagramTransport使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在asyncio的用法示例。


在下文中一共展示了asyncio.DatagramTransport方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: setUp

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def setUp(self):
        self.server = Mock(autospec=pybtracker.TrackerServer)
        self.server.logger = logger
        self.server.connids = {}
        self.server.activity = {}
        self.server.connid_valid_period = 100
        self.server.interval = 200
        self.server.torrents = {
            b'01234567890123456789': {
                b'11111111111111111111': ('1.1.1.1', 1111, 0, 0, 0, False),
                b'22222222222222222222': ('2.2.2.2', 2222, 0, 0, 0, False),
                b'33333333333333333333': ('3.3.3.3', 3333, 0, 0, 0, False),
            },
            b'ABCDEFGHIJKLMNOPQRST': {
                b'11111111111111111111': ('1.1.1.1', 1111, 0, 0, 0, False),
                b'44444444444444444444': ('4.4.4.4', 4444, 0, 0, 0, False),
            }
        }
        self.proto = pybtracker.server.UdpTrackerServerProto(self.server, allowed_torrents={})
        self.proto.transport = Mock(asyncio.DatagramTransport) 
開發者ID:elektito,項目名稱:pybtracker,代碼行數:22,代碼來源:test.py

示例2: connection_made

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def connection_made(self, transport: asyncio.BaseTransport) -> None:
        # we need to cast here because the signature in the base class dicates BaseTransport
        # and arguments can only be redefined contravariantly
        self.transport = cast(asyncio.DatagramTransport, transport) 
開發者ID:QuarkChain,項目名稱:pyquarkchain,代碼行數:6,代碼來源:discovery.py

示例3: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def __init__(self, app: ASGIFramework, loop: asyncio.AbstractEventLoop, config: Config) -> None:
        self.app = app
        self.config = config
        self.loop = loop
        self.protocol: "QuicProtocol"
        self.protocol_queue: asyncio.Queue = asyncio.Queue(10)
        self.transport: Optional[asyncio.DatagramTransport] = None

        self.loop.create_task(self._consume_events()) 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:11,代碼來源:udp_server.py

示例4: connection_made

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def connection_made(self, transport: asyncio.DatagramTransport) -> None:  # type: ignore
        # h3/Quic is an optional part of Hypercorn
        from ..protocol.quic import QuicProtocol  # noqa: F811

        self.transport = transport
        socket = self.transport.get_extra_info("socket")
        server = parse_socket_addr(socket.family, socket.getsockname())
        task_group = TaskGroup(self.loop)
        context = Context(task_group)
        self.protocol = QuicProtocol(
            self.app, self.config, cast(Any, context), server, self.protocol_send
        ) 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:14,代碼來源:udp_server.py

示例5: test_dgram_not_implemented

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def test_dgram_not_implemented(self):
        transport = asyncio.DatagramTransport()

        self.assertRaises(NotImplementedError, transport.sendto, 'data')
        self.assertRaises(NotImplementedError, transport.abort) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:7,代碼來源:test_transports.py

示例6: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def __init__(self, receiver) -> None:
        self.__closed: asyncio.Future[bool] = asyncio.Future()
        self.id = next(protocol_id)
        self.local_candidate: Optional[Candidate] = None
        self.receiver = receiver
        self.transport: Optional[asyncio.DatagramTransport] = None
        self.transactions: Dict[bytes, stun.Transaction] = {} 
開發者ID:aiortc,項目名稱:aioice,代碼行數:9,代碼來源:ice.py

示例7: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def __init__(self, loop: asyncio.AbstractEventLoop, peer_manager: 'PeerManager', node_id: bytes, udp_port: int,
                 internal_udp_port: int, peer_port: int, external_ip: str, rpc_timeout: float = constants.RPC_TIMEOUT,
                 split_buckets_under_index: int = constants.SPLIT_BUCKETS_UNDER_INDEX,
                 storage: typing.Optional['SQLiteStorage'] = None):
        self.loop = loop
        self.internal_udp_port = internal_udp_port
        self.protocol = KademliaProtocol(loop, peer_manager, node_id, external_ip, udp_port, peer_port, rpc_timeout,
                                         split_buckets_under_index)
        self.listening_port: asyncio.DatagramTransport = None
        self.joined = asyncio.Event(loop=self.loop)
        self._join_task: asyncio.Task = None
        self._refresh_task: asyncio.Task = None
        self._storage = storage 
開發者ID:lbryio,項目名稱:lbry-sdk,代碼行數:15,代碼來源:node.py

示例8: mock_network_loop

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def mock_network_loop(loop: asyncio.AbstractEventLoop,
                      dht_network: typing.Optional[typing.Dict[typing.Tuple[str, int], 'KademliaProtocol']] = None):
    dht_network: typing.Dict[typing.Tuple[str, int], 'KademliaProtocol'] = dht_network if dht_network is not None else {}

    async def create_datagram_endpoint(proto_lam: typing.Callable[[], 'KademliaProtocol'],
                                       from_addr: typing.Tuple[str, int]):
        def sendto(data, to_addr):
            rx = dht_network.get(to_addr)
            if rx and rx.external_ip:
                # print(f"{from_addr[0]}:{from_addr[1]} -{len(data)} bytes-> {rx.external_ip}:{rx.udp_port}")
                return rx.datagram_received(data, from_addr)

        protocol = proto_lam()
        transport = asyncio.DatagramTransport(extra={'socket': mock_sock})
        transport.is_closing = lambda: False
        transport.close = lambda: mock_sock.close()
        mock_sock.sendto = sendto
        transport.sendto = mock_sock.sendto
        protocol.connection_made(transport)
        dht_network[from_addr] = protocol
        return transport, protocol

    with mock.patch('socket.socket') as mock_socket:
        mock_sock = mock.Mock(spec=socket.socket)
        mock_sock.setsockopt = lambda *_: None
        mock_sock.bind = lambda *_: None
        mock_sock.setblocking = lambda *_: None
        mock_sock.getsockname = lambda: "0.0.0.0"
        mock_sock.getpeername = lambda: ""
        mock_sock.close = lambda: None
        mock_sock.type = socket.SOCK_DGRAM
        mock_sock.fileno = lambda: 7
        mock_socket.return_value = mock_sock
        loop.create_datagram_endpoint = create_datagram_endpoint
        yield 
開發者ID:lbryio,項目名稱:lbry-sdk,代碼行數:37,代碼來源:dht_mocks.py

示例9: _test_send_msg

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import DatagramTransport [as 別名]
def _test_send_msg(self, expected_failures, mock_asyncio):
        tid = 1000
        msg = b'x' * 20
        self.proto.sent_msgs[tid] = Mock(spec=asyncio.Event)
        self.proto.transport = Mock(spec=asyncio.DatagramTransport)
        self.proto.connect = Mock()
        wait_time = 0
        failures = 0
        async def wait_for_side_effect(task, timeout):
            nonlocal wait_time, failures
            if failures == expected_failures:
                return
            failures += 1
            wait_time += timeout
            raise asyncio.TimeoutError

        expected_wait_time = 0
        for i in range(min(expected_failures, self.client.max_retransmissions)):
            expected_wait_time += 15 * 2 ** i

        mock_asyncio.TimeoutError = asyncio.TimeoutError
        mock_asyncio.wait_for.side_effect = wait_for_side_effect
        if expected_failures > self.client.max_retransmissions:
            with self.assertRaises(TimeoutError):
                await self.proto.send_msg(msg, tid)
        else:
            await self.proto.send_msg(msg, tid)
        self.proto.transport.sendto(msg)
        self.assertEqual(wait_time, expected_wait_time)
        self.proto.connect.called_with()
        self.assertNotIn(tid, self.proto.sent_msgs)
        self.assertEqual(failures, min(expected_failures,
                                       self.client.max_retransmissions)) 
開發者ID:elektito,項目名稱:pybtracker,代碼行數:35,代碼來源:test.py


注:本文中的asyncio.DatagramTransport方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。