import struct from rich.console import Console import asyncio class Server: BUFFER_SIZE = 1024 NOTIFICATION_DIR = 'notifications' console = Console() def __init__(self, ip: str, port: int, producer_port: int, notification_dir: str = 'notifications'): self.NOTIFICATION_DIR = notification_dir self.__client_socket = None self.__producer_socket = None self.__ip = ip self.__port = port self.__producer_port = producer_port self.__client_writers = [] async def up(self): self.__client_socket = await asyncio.start_server( self.handle_client, self.__ip, self.__port ) self.__producer_socket = await asyncio.start_server( lambda reader, writer: self.handle_producer(reader, writer, self.NOTIFICATION_DIR), self.__ip, self.__producer_port ) self.console.print(f'Serves on {self.__ip}:{self.__port} for clients.') self.console.print(f'Receives notifications from producers at {self.__ip}:{self.__producer_port}.') async with self.__producer_socket, self.__client_socket: await asyncio.gather( self.__producer_socket.serve_forever(), self.__client_socket.serve_forever() ) @staticmethod async def send_message(writer, message: str): writer.write(struct.pack(' str: size, = struct.unpack('