Created
May 18, 2021 01:47
-
-
Save craigderington/6fa9d6d292f7aaafbde2afb4773931f6 to your computer and use it in GitHub Desktop.
Py3 Asyncio
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
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