Skip to content

Instantly share code, notes, and snippets.

@dtoma
Created January 8, 2020 13:14
Show Gist options
  • Save dtoma/118e2ee734a4f3e02d6539074ddf58d1 to your computer and use it in GitHub Desktop.
Save dtoma/118e2ee734a4f3e02d6539074ddf58d1 to your computer and use it in GitHub Desktop.
import asyncio
from collections import defaultdict
import numpy as np
TSDB = defaultdict(np.array)
class Server:
def __init__(self, message, loop):
self.message = message
self.loop = loop
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("Socket closed, stop the event loop")
loop = asyncio.get_event_loop()
loop.stop()
loop = asyncio.get_event_loop()
message = "Hello World!"
connect = loop.create_datagram_endpoint(
lambda: Server(message, loop),
remote_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(connect)
loop.run_forever()
transport.close()
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment