You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
746 B

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()