Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created May 18, 2021 01:47
Show Gist options
  • Save craigderington/6fa9d6d292f7aaafbde2afb4773931f6 to your computer and use it in GitHub Desktop.
Save craigderington/6fa9d6d292f7aaafbde2afb4773931f6 to your computer and use it in GitHub Desktop.
Py3 Asyncio
import asyncio
class EchoClientProtocol(asyncio.Protocol):
def __init__(self, message, loop):
self.message = message.encode()
def connection_made(self, transport):
self.transport = transport
self.write_data()
def data_received(self, data):
print('Data received: {!r}',len(data))
self.write_data()
def eof_received(self):
print("eof")
return True
def write_data(self):
print("write")
self.transport.write(self.message)
def connection_lost(self, exc):
print('The server closed the connection')
print('Stop the event loop')
loop = asyncio.get_event_loop()
message = 'Hello World!'
coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
'127.0.0.1', 5676)
loop.run_until_complete(coro)
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment