Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created May 4, 2021 23:13
Show Gist options
  • Save craigderington/e4e120ca28c1092a1b348e6296010988 to your computer and use it in GitHub Desktop.
Save craigderington/e4e120ca28c1092a1b348e6296010988 to your computer and use it in GitHub Desktop.
Asyncio Echo Client
#!/usr/bin/env python3.4
import asyncio
class EchoClient(asyncio.Protocol):
message = 'Client Echo'
def connection_made(self, transport):
transport.write(self.message.encode())
print('data sent: {}'.format(self.message))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
print('server closed the connection')
asyncio.get_event_loop().stop()
loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment