class EchoClientProtocol(asyncio.Protocol):
- def __init__(self, message, on_con_lost, loop):
+ def __init__(self, message, on_con_lost):
self.message = message
- self.loop = loop
self.on_con_lost = on_con_lost
def connection_made(self, transport):
class EchoClientProtocol:
- def __init__(self, message, loop):
+ def __init__(self, message, on_con_lost):
self.message = message
- self.loop = loop
self.transport = None
- self.on_con_lost = loop.create_future()
+ self.on_con_lost = on_con_lost
def connection_made(self, transport):
self.transport = transport
# low-level APIs.
loop = asyncio.get_running_loop()
+ on_con_lost = loop.create_future()
message = "Hello World!"
+
transport, protocol = await loop.create_datagram_endpoint(
- lambda: EchoClientProtocol(message, loop),
+ lambda: EchoClientProtocol(message, on_con_lost),
remote_addr=('127.0.0.1', 9999))
try:
- await protocol.on_con_lost
+ await on_con_lost
finally:
transport.close()