def receive(self, conn, addr): msg = None try: msg = conn.recv(MESSAGE_LENGTH) log.debug(msg) msg, params = msg.split("~") parameters = self.extractParams(params) except Exception as e: log.error('Error while receiving message from %s' % addr) log.error(e) raise e return msg, parameters
def start(self, conn, addr): self.respond(conn, "connecting...", str("type=valid")) log.info('Session started with %s' % addr) login_params = None debug = None try: while True: request_message, request_params = self.receive(conn, addr) # Get response message and parameters response_params = None response_msg = None debug = False log.info('Request from %s - %s' % (addr, request_message)) if request_message == "authenticate": login_params = request_params response_msg, account_type = Bank().login( request_params['email'], request_params['password']) response_params = str("type=" + account_type) elif request_message == "logout": response_msg = "Logout Successful" del Bank().logged_ins[login_params['email']] else: response_msg, response_params, debug = self.bank_operation( request_message, request_params) # Respond to client self.respond(conn, response_msg, response_params) if debug: log.debug('Response to %s - %s' % (addr, response_msg)) log.info('Passbook sent to %s' % (addr)) else: log.info('Response to %s - %s' % (addr, response_msg)) # Close connection if authentication failed or logout if ("Login Unsuccessful" in response_msg or response_msg == "Logout Successful"): conn.close() break except Exception as e: if login_params['email'] in Bank().logged_ins: del Bank().logged_ins[login_params['email']] log.error(e) log.error('Error after menu ' + str(addr)) finally: self.count -= 1 conn.close()
def bank_operation(self, request_message, request_params): response_msg = None response_params = None debug = False if request_message == "addAccount": response_msg = Bank().addAccount( Account(request_params['name'], request_params['email'], request_params['password'], request_params['type'])) elif request_message == "deleteAccount": response_msg = Bank().deleteAccount(request_params['email']) elif request_message == "changePassword": response_msg = Bank().changePassword(request_params['email'], request_params['password']) elif request_message == "withdraw": log.debug('withDraw: %s' % str(request_params)) response_msg = Bank().withDraw(request_params['email'], request_params['amount']) elif request_message == "deposit": response_msg = Bank().deposit(request_params['email'], request_params['amount']) elif request_message == "getPassbook": response_msg = Bank().getPassbook(request_params['email']) debug = True return response_msg, response_params, debug
def respond(self, client, msg, parameters): log.debug('msg: %s' % msg) log.debug('Parameters: %s' % parameters) if parameters is None: parameters = "" client.send(str(msg + "~" + parameters))