Created
May 4, 2021 23:13
-
-
Save craigderington/e4e120ca28c1092a1b348e6296010988 to your computer and use it in GitHub Desktop.
Asyncio Echo Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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