Skip to content
Snippets Groups Projects
Commit 9f30d18d authored by jkerdreu's avatar jkerdreu
Browse files

Finaly the async datagram protocol is working.. looks like a bug a in Python < 3.8.2

Next step put in Engine



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2708 b32b6428-25c9-4566-ad07-03861ab6144f
parent d2e97fdf
No related branches found
No related tags found
No related merge requests found
import asyncio
class EchoClientProtocol:
def __init__(self, message, on_con_lost):
self.message = message
self.on_con_lost = on_con_lost
self.transport = None
def connection_made(self, transport):
self.transport = transport
print('Send:', self.message)
self.transport.sendto(self.message.encode())
def datagram_received(self, data, addr):
print("Received:", data.decode())
print("Close the socket")
self.transport.close()
def error_received(self, exc):
print('Error received:', exc)
def connection_lost(self, exc):
print("Connection closed")
self.on_con_lost.set_result(True)
async def main():
# Get a reference to the event loop as we plan to use
# 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, on_con_lost),
remote_addr=('127.0.0.1', 9999))
try:
await on_con_lost
finally:
transport.close()
asyncio.run(main())
\ No newline at end of file
import asyncio
import socket
import struct
ADDR = '224.0.29.200'
PORT = 1235
class EchoServerProtocol(asyncio.Protocol):
def __init__(self,on_con_lost):
self.on_con_lost = on_con_lost
def connection_made(self, transport):
#import pdb;pdb.set_trace()
sock = transport.get_extra_info('socket')
(addr,port) = sock.getsockname()
mreq = struct.pack('4sl',socket.inet_aton(addr),socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP,socket.IP_ADD_MEMBERSHIP,mreq)
sock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,10)
self.transport = transport
def error_received(self, exc):
print('Error received:', exc)
def connection_lost(self, exc):
print("Connection closed")
self.on_con_lost.set_result(True)
def datagram_send(self,data):
self.transport.sendto(data,(ADDR,PORT))
def datagram_received(self, data, addr):
print('Received %r from %s' % (data, addr))
async def test_send(protocol):
while True:
protocol.datagram_send("FooBar".encode("utf-8"))
await asyncio.sleep(10)
async def main():
loop = asyncio.get_running_loop()
print("Starting UDP server")
on_con_lost = loop.create_future()
transport, protocol = await loop.create_datagram_endpoint( lambda: EchoServerProtocol(on_con_lost), local_addr=(ADDR, PORT),reuse_port=True)
asyncio.ensure_future(test_send(protocol))
try:
await asyncio.sleep(3600) # Serve for 1 hour.
finally:
transport.close()
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Bye Bye")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment