def main(): client = Redis(host="127.0.0.1", db=2) bot = QQ() if True: # if not reload_login_state(client, bot): ok, message = bot.login_by_qrcode() if not ok: logging("Login failed: %s", message) return try: bot_handler = MsgHandler(bot) while True: time.sleep(0.5) try: new_msg = bot.check_msg() print "message: ", new_msg except socket.timeout as ex: logging.warning("check msg timeout, retrying... %s", ex) continue if new_msg is not None: bot_handler.handle(new_msg) except KeyboardInterrupt: logging.info("Stop Bot. Logout.") finally: dump_login_state(client, bot) logging.info("Dump login state... OK")
def run(socket, instances, access_rights, coder): print("ClientManager starting..") msg_handler = MsgHandler(instances) while True: try: print("Waiting msg") received_msg = get_msg(socket, coder) print("Received request '" + str(list(received_msg.keys())[0]) + "' from " + str(socket)) msg_for_client = msg_handler.handle(received_msg, access_rights) # print("Msg for client " + str(msg_for_client)) header, body = build_response(msg_for_client, coder) send_msg(socket, header, body) except ConnectionAbortedError: print("Connection lost with " + str(socket)) raise ConnectionAbortedError except Exception as e: print("An error has occurred " + str(e)) msg = build_response(instances, error_msg("ServerError", str(e))) send_msg(socket, *msg) pass