Files
notification-client/notification_client/client.py
T

29 lines
746 B
Python

import asyncio
import struct
class Client:
BUFFER_SIZE = 1024
def __init__(self, ip: str, port: int):
self.__ip = ip
self.__port = port
async def connect(self):
reader, writer = await asyncio.open_connection(self.__ip, self.__port)
await self.handle(reader, writer)
async def handle(self, reader, writer):
try:
while True:
size, = struct.unpack('<L', await reader.readexactly(4))
message = await reader.readexactly(size)
print(f"{message.decode('utf-8') =}")
except asyncio.CancelledError:
print('Something went wrong')
finally:
writer.close()
await writer.wait_closed()