def recvMessage(): """队列处理消息 """ while 1: nxt = sleep(0.01) message = yield MsgHandler.MessageInQueue.get() try: yield MsgHandler._recv_message(message) except Exception as e: logging.exception(e) yield nxt
def sendMessage(): """队列发送消息 """ while 1: nxt = sleep(0.01) message, kwargs = yield MsgHandler.MessageOutQueue.get() try: yield MsgHandler._send_message(message, **kwargs) except Exception as e: logging.warn(str(e)) yield nxt
def response(self, msg): print msg if msg.sender in self.targetListened_Obj: MsgHandler.handle(msg) else: print '[Robot]receive msg,but the sender isn`t in the registered list'
#!/usr/bin/env python # -*- coding: utf-8 -*- # Code by Yinzo: https://github.com/Yinzo # Origin repository: https://github.com/Yinzo/SmartQQBot import sys from MsgHandler import * reload(sys) sys.setdefaultencoding("utf-8") if __name__ == '__main__': bot = QQ() bot.login() bot_handler = MsgHandler(bot) while 1: try: new_msg = bot.check_msg() except socket.timeout, e: logging.warning("RUNTIMELOG check msg timeout, retrying...") continue except: continue else: if new_msg is not None: bot_handler.handle(new_msg)
def run(self): t1 = time.time() while self.run_flag: try: events = self.epl.poll(self.timeout) if not events: continue except: continue for fd, event in events: sock = self.fd_to_socket[fd] # 如果活动socket为当前服务socket,表示有新的连接 if sock == self.server_sock: conn, addr = self.server_sock.accept() Log().d("新连接:" + str(addr)) # 新的socket设置为非阻塞 conn.setblocking(False) # 注册新连接fd到待读事件集合 self.epl.register(conn.fileno(), EPOLLIN) # 把新连接的文件句柄保存到字典 self.fd_to_socket[conn.fileno()] = conn # 以新连接的对象为键值,值存储在队列中,保存每个连接的信息 user_data = {} user_data["fd"] = conn.fileno() user_data["rbuf"] = "" user_data["wbuf"] = "" user_data["epl"] = self.epl self.user_map[conn] = user_data # 关闭事件 elif event & EPOLLHUP: Log().d("连接关闭") self.epl.unregister(fd) self.fd_to_socket[fd].close() del self.fd_to_socket[fd] # 可读事件 elif event & EPOLLIN: # 接收数据 data = sock.recv(1024) if data: Log().d("收到数据,客户端:" + str(sock.getpeername())) self.user_map[sock]["rbuf"] += data bufdata = self.user_map[sock]["rbuf"] while len(bufdata) >= 8: msg_id = struct.unpack('!I', bufdata[0:4])[0] data_len = struct.unpack('!I', bufdata[4:8])[0] if len(bufdata) >= data_len: packet_data = bufdata[8:data_len] self.user_map[sock]["rbuf"] = bufdata[ data_len:] bufdata = self.user_map[sock]["rbuf"] user_data = self.user_map[sock] user_data["msg_id"] = msg_id user_data["msg_data"] = packet_data MsgHandler().handleMsg(user_data) else: break else: Log().d("连接关闭,客户端:" + str(sock.getpeername())) self.epl.unregister(fd) self.user_map.pop(sock) sock.close() del self.fd_to_socket[fd] # 可写事件 elif event & EPOLLOUT: if len(self.user_map[sock]["wbuf"]) > 0: sock.send(self.user_map[sock]["wbuf"]) self.user_map[sock]["wbuf"] = "" Log().d("发送数据,客户端:" + str(sock.getpeername())) else: self.epl.modify(fd, EPOLLIN) t2 = time.time() delta = t2 - t1 t1 = t2 TM().step(delta) for fd in self.fd_to_socket: self.epl.unregister(fd) self.fd_to_socket[fd].close() self.epl.close()
# -*- coding: utf-8 -*- import sys from MsgHandler import * reload(sys) sys.setdefaultencoding("utf-8") if __name__ == '__main__': bot = QQ() bot.login_by_qrcode() bot_handler = MsgHandler(bot) while 1: new_msg = bot.check_msg() if new_msg is not None: bot_handler.handle(new_msg)
# -*- coding: utf8 -*- from wxpy import * import DouTuLa import MsgHandler import NetworkListener if __name__ == '__main__': MsgHandler.init() NetworkListener.init() NetworkListener.start()
def timer(self): MsgHandler.timer(managers, self.clients)
# Boots everything up. GUI in future? # Now simply beginning sequence with config load from VkApiHandler import * from MsgHandler import * from ActionHandler import * from DatabaseHandler import * from CodeList import CODE_DICT, DB_NAME # Vk API info ACCESS_TOKEN = '0405278c1d9aa5b5962e158cfc6e036ebb3703d671d5042df8d9d807e1a6cbbb577effb424664c474efe6' API_VERSION = '5.130' GROUP_ID = 199935063 db_handler = DatabaseHandler(DB_NAME) vkapi_handler = VkApiHandler(ACCESS_TOKEN, API_VERSION, GROUP_ID, debug=True) act_handler = ActionHandler(vkapi_handler, db_handler, debug_mode=True) msg_handler = MsgHandler(CODE_DICT, COMMAND_SYMBOL, IGNORE_SYMBOL, act_handler) vkapi_handler.setMsgHandler(msg_handler) vkapi_handler.main_loop()
def close(self, clientfd): state = self.clients[clientfd] MsgHandler.disconnect(managers, state) state.client_socket.close() del self.clients[clientfd]
# -*- coding: utf-8 -*- # Code by Yinzo: https://github.com/Yinzo # Origin repository: https://github.com/Yinzo/SmartQQBot import sys from MsgHandler import * reload(sys) # sys.setdefaultencoding("utf-8") if __name__ == '__main__': bot = QQ() bot.login_by_qrcode() bot_handler = MsgHandler(bot) while 1: try: new_msg = bot.check_msg() except socket.timeout, e: logging.warning("check msg timeout, retrying...") continue if new_msg is not None: bot_handler.handle(new_msg)
# -*- coding: utf-8 -*- # Code by Yinzo: https://github.com/Yinzo # Origin repository: https://github.com/Yinzo/SmartQQBot import sys from MsgHandler import * reload(sys) sys.setdefaultencoding("utf-8") if __name__ == '__main__': bot = QQ() bot.login_by_qrcode() bot_handler = MsgHandler(bot) print 'login successful' while 1: try: new_msg = bot.check_msg() except socket.timeout, e: logging.warning("check msg timeout, retrying...") continue if new_msg is not None: bot_handler.handle(new_msg)
bot_name = ['MsgHandler', 'dudleybot', 'petuniabot', 'hogford','hagrid','ollivander'] #bot_name = ['msgHandler','dudleybot'] def botexec(str): sys.stderr.write('I am '+str+ '\n') if(str == bot_name[0]): msgServ.msgHandler() else: loc = os.path.abspath('.') tmpstr=loc+'/env/bin/python '+loc+'/hp01/'+str+'.py' print(tmpstr) os.system(tmpstr) return if __name__ == "__main__": msgServ = MsgHandler(); jobs = [] loc = os.path.abspath('.') os.environ.setdefault('SLACK_BOT_TOKEN_DUDLEY', 'xoxb-256688870500-YufJlAqf7bnpNDWONBCkbsxh') os.environ.setdefault('SLACK_BOT_TOKEN_PETUNIA', 'xoxb-256652204482-32JtdYAv5f87VTALSr3FKgBA') os.environ.setdefault('SLACK_BOT_TOKEN_BOB', 'xoxb-260225956352-zGKN4Tfw2iQ8Rn97HwROAIY0') os.environ.setdefault('SLACK_BOT_TOKEN_HOGFORD', 'xoxb-260347880417-cykm48o6jvV2Zu604TTVTG3h') os.environ.setdefault('BOT_ID_PETUNIA', 'U7JK660E6') os.environ.setdefault('BOT_ID_DUDLEY', 'U7JL8RLEQ') os.environ.setdefault('BOT_ID_BOB_HAIR_CUT', 'U7N6MU4AC') os.environ.setdefault('BOT_ID_HOGFORD', 'U7NA7RWC9') os.environ.setdefault('BOT_ID_HAGRID', 'U7UNYBVB3') os.environ.setdefault('BOT_ID_OLLIVANDER', 'U7VFE8PGV') for i in bot_name: p = multiprocessing.Process(target=botexec,args=(i,))
# -*- coding: utf-8 -*- import sys from MsgHandler import * reload(sys) sys.setdefaultencoding("utf-8") if __name__ == '__main__': bot = QQ() bot.login_by_qrcode() bot_handler = MsgHandler(bot) while 1: try: new_msg = bot.check_msg() except socket.timeout, e: logging.warning("check msg timeout, retrying...") continue if new_msg is not None: bot_handler.handle(new_msg)