class SocketClientTCP: def __init__(self): super(SocketClientTCP, self).__init__() self.config_manager = ConfigManager() self.sock = socket(AF_INET, SOCK_STREAM) self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.__connect__() def __connect__(self): """ this function try connection to server :param: :return: """ print(Colors.FORE_YELLOW + "[+] try connection to ChatApp server ...") response_code = self.sock.connect_ex(( self.config_manager.get().socket_server.IP, self.config_manager.get().socket_server.PORT, )) if response_code == 0: CommandHandler(client_socket=self.sock).start() elif response_code == 111: print(Colors.FORE_GREEN + "[-] ChatApp server not respod, maybe it is down...")
class SocketServer: """ This class creates socket stream on TCP protocol """ def __init__(self): super(SocketServer, self).__init__() self.config_manager = ConfigManager() self.sock_connection = socket(AF_INET, SOCK_STREAM) self.sock_connection.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.__start__() def __start__(self): """ This method creates socket connection :params: :return: """ self.sock_connection.bind(( self.config_manager.get().socket_server.IP, # TODO: change get() => property self.config_manager.get().socket_server.PORT )) self.sock_connection.listen( self.config_manager.get().socket_server.LISTEN_CLIENT ) print(str(datetime.now()).split('.')[0] + " => [+] Server is ready to listening") def run(self): """ start server for listening clients :params: :return: """ client, client_address = self.sock_connection.accept() # we create new thread per client that connect to server ClientHandler( client, client_address ).start() self.run()