本文整理汇总了Python中asyncio.DatagramProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.DatagramProtocol方法的具体用法?Python asyncio.DatagramProtocol怎么用?Python asyncio.DatagramProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio
的用法示例。
在下文中一共展示了asyncio.DatagramProtocol方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_empty
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def test_empty(self):
f = mock.Mock()
p = asyncio.Protocol()
self.assertIsNone(p.connection_made(f))
self.assertIsNone(p.connection_lost(f))
self.assertIsNone(p.data_received(f))
self.assertIsNone(p.eof_received())
dp = asyncio.DatagramProtocol()
self.assertIsNone(dp.connection_made(f))
self.assertIsNone(dp.connection_lost(f))
self.assertIsNone(dp.error_received(f))
self.assertIsNone(dp.datagram_received(f, f))
sp = asyncio.SubprocessProtocol()
self.assertIsNone(sp.connection_made(f))
self.assertIsNone(sp.connection_lost(f))
self.assertIsNone(sp.pipe_data_received(1, f))
self.assertIsNone(sp.pipe_connection_lost(1, f))
self.assertIsNone(sp.process_exited())
示例2: create_socket
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def create_socket(self) -> None:
"""Use the loop.create_datagram_endpoint method to create a socket."""
self._transport, _ = await self._loop.create_datagram_endpoint(
asyncio.DatagramProtocol,
family=socket.AF_INET,
remote_addr=self._addr
)
示例3: setUp
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def setUp(self):
self.loop = self.new_test_loop()
self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
self.sock = mock.Mock(spec_set=socket.socket)
self.sock.fileno.return_value = 7
示例4: test_create_datagram_endpoint_connect_err
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def test_create_datagram_endpoint_connect_err(self):
self.loop.sock_connect = mock.Mock()
self.loop.sock_connect.side_effect = OSError
coro = self.loop.create_datagram_endpoint(
asyncio.DatagramProtocol, remote_addr=('127.0.0.1', 0))
self.assertRaises(
OSError, self.loop.run_until_complete, coro)
示例5: test_create_datagram_endpoint_socket_err
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def test_create_datagram_endpoint_socket_err(self, m_socket):
m_socket.getaddrinfo = socket.getaddrinfo
m_socket.socket.side_effect = OSError
coro = self.loop.create_datagram_endpoint(
asyncio.DatagramProtocol, family=socket.AF_INET)
self.assertRaises(
OSError, self.loop.run_until_complete, coro)
coro = self.loop.create_datagram_endpoint(
asyncio.DatagramProtocol, local_addr=('127.0.0.1', 0))
self.assertRaises(
OSError, self.loop.run_until_complete, coro)
示例6: test_create_datagram_endpoint_no_matching_family
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def test_create_datagram_endpoint_no_matching_family(self):
coro = self.loop.create_datagram_endpoint(
asyncio.DatagramProtocol,
remote_addr=('127.0.0.1', 0), local_addr=('::1', 0))
self.assertRaises(
ValueError, self.loop.run_until_complete, coro)
示例7: test_create_datagram_endpoint_noaddr_nofamily
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def test_create_datagram_endpoint_noaddr_nofamily(self):
coro = self.loop.create_datagram_endpoint(
asyncio.DatagramProtocol)
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
示例8: __init__
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def __init__(self):
asyncio.DatagramProtocol.__init__(self)
OscProtocol.__init__(self)
self.loop = None
### PRIVATE METHODS ###
示例9: open_udp_connection
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def open_udp_connection(self, host, port, data, addr, reply):
class Protocol(asyncio.DatagramProtocol):
def __init__(prot, data):
self.udpmap[addr] = prot
prot.databuf = [data]
prot.transport = None
prot.update = 0
def connection_made(prot, transport):
prot.transport = transport
for data in prot.databuf:
transport.sendto(data)
prot.databuf.clear()
prot.update = time.perf_counter()
def new_data_arrived(prot, data):
if prot.transport:
prot.transport.sendto(data)
else:
prot.databuf.append(data)
prot.update = time.perf_counter()
def datagram_received(prot, data, addr):
data = self.cipher.datagram.decrypt(data) if self.cipher else data
data = self.rproto.udp_client(data) if not self.direct else data
reply(data)
prot.update = time.perf_counter()
def connection_lost(prot, exc):
self.udpmap.pop(addr, None)
if addr in self.udpmap:
self.udpmap[addr].new_data_arrived(data)
else:
if self.direct and host == 'tunnel':
raise Exception('Unknown tunnel endpoint')
self.connection_change(1)
if len(self.udpmap) > UDP_LIMIT:
min_addr = min(self.udpmap, key=lambda x: self.udpmap[x].update)
prot = self.udpmap.pop(min_addr)
if prot.transport:
prot.transport.close()
prot = Protocol(data)
remote_addr = (host, port) if self.direct else (self.host_name, self.port)
await asyncio.get_event_loop().create_datagram_endpoint(lambda: prot, remote_addr=remote_addr)
示例10: start_udp_server
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def start_udp_server(self, args):
class Protocol(asyncio.DatagramProtocol):
def connection_made(prot, transport):
prot.transport = transport
def datagram_received(prot, data, addr):
asyncio.ensure_future(datagram_handler(prot.transport, data, addr, **vars(self), **args))
return asyncio.get_event_loop().create_datagram_endpoint(Protocol, local_addr=(self.host_name, self.port))
示例11: setUp
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import DatagramProtocol [as 别名]
def setUp(self):
super().setUp()
self.loop = self.new_test_loop()
self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
self.sock = mock.Mock(spec_set=socket.socket)
self.sock.fileno.return_value = 7