def change_wxxnr_redis_data(wxbot_id, xnr_data={}): d = r.get(wxbot_id) data = {} if d: data = eval(d) for key, value in xnr_data.items(): data[key] = value #如果存在key则更改数据为value,不存在key则增加数据value return r.set(wxbot_id, data)
def my_login_callback(self, **kwargs): d = r.get(self.wxbot_id) if d: data = eval(d) data['status'] = 'logined' r.set(self.wxbot_id, data) else: r.set(self.wxbot_id, {'status': 'logined'})
def send_command_without_recv(command): wxbot_id = command['wxbot_id'] wxbot_port = eval(r.get(wxbot_id))['wxbot_port'] #查询redis得到该wxbot_id对应的wxbot_port try: client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((LOCALHOST_IP, wxbot_port)) client.send(json.dumps(command)) result = 1 except Exception,e: print e result = 0
def start_bot(wx_id, wxbot_id, wxbot_port, submitter=None, mail=None, access_id=None, remark=None, init_groups_list='', create_flag=0): #在logout完善之前,在登录之前先手动把status数据更改成logout,并执行logout change_wxxnr_redis_data(wxbot_id, xnr_data={'status': 'logout','qr_path':'', 'wx_id':wx_id, 'wxbot_port':wxbot_port}) if submitter != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'submitter':submitter}) if mail != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'mail':mail}) if access_id != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'access_id':access_id}) if remark != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'remark':remark}) if create_flag: change_wxxnr_redis_data(wxbot_id, xnr_data={'create_flag':create_flag}) logout_result = xnr_logout(wxbot_id) if logout_result: print u'登录前登出成功' else: print u'登录前登出失败' #login wxxnr_login_path = os.path.join(os.getcwd(), WX_LOGIN_PATH) if init_groups_list: base_str = 'python '+ wxxnr_login_path + ' -i '+ wxbot_id + ' -p ' + str(wxbot_port) + ' -g ' + init_groups_list else: base_str = 'python '+ wxxnr_login_path + ' -i '+ wxbot_id + ' -p ' + str(wxbot_port) p_str1 = base_str + ' >> wxxnr_login'+ str(wxbot_port) + '.txt' command_str = base_str p_str2 = 'pgrep -f ' + '"' + command_str + '"' process_ids = subprocess.Popen(p_str2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) process_id_list = process_ids.stdout.readlines() for process_id in process_id_list: process_id = process_id.strip() kill_str = 'kill -9 ' + process_id print 'kill_str::',kill_str p2 = subprocess.Popen(kill_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p2 = subprocess.Popen(p_str1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) #检测登陆状态,返回登陆所需二维码路径或者返回缓存登录成功的标志:loginedwithcache while True: d = r.get(wxbot_id) if d: try: qr_path = eval(d)['qr_path'] # print 'qr_path', qr_path #使用缓存登陆时,qr_path对应的二维码文件不存在 if qr_path == 'loginedwithcache': return qr_path if ('.png' in qr_path) and (os.path.isfile(qr_path)): #发送二维码图片至邮箱 #send_qrcode2mail(wxbot_id, qr_path) return qr_path except Exception,e: print e return 0
def login_wx_xnr(wxbot_id): try: wx_id = eval(r.get(wxbot_id))['wx_id'] check_result = check_wx_xnr(wx_id) if check_result: wxbot_id = check_result['wxbot_id'] wxbot_port = check_result['wxbot_port'] groups_list = check_result['groups_list'] qr_path = start_bot(wx_id=wx_id, wxbot_id=wxbot_id, wxbot_port=wxbot_port, init_groups_list=groups_list) return qr_path except Exception,e: print e return 0
def check_status(wxbot_id): d = r.get(wxbot_id) if d: try: status = eval(d)['status'] if status == 'listening': #判断监听群组消息的socket端口是否在开着 command = {'opt': 'checkstatus', 'wxbot_id': wxbot_id} if not send_command_without_recv(command): status = 'logout' change_wxxnr_redis_data(wxbot_id, {'status': status}) return status except Exception, e: print e
def save_bot_info(self): while True: d = r.get(self.wxbot_id) if d: try: wx_id = eval(d)['wx_id'] wxbot_port = eval(d)['wxbot_port'] submitter = eval(d)['submitter'] mail = eval(d)['mail'] access_id = eval(d)['access_id'] remark = eval(d)['remark'] break except Exception, e: print e
def load_wxxnr_redis_data(wxbot_id, items=[]): d = r.get(wxbot_id) data = {} results = {} if d: data = eval(d) if items: for item in items: if item in data: results[item] = data[item] else: return 0 return results else: return data else: return data
def set_default_groups(self): try: d = r.get(self.wxbot_id) if d: data = eval(d) create_flag = data['create_flag'] if create_flag: group_list = [] groups = self.groups(update=True) for group in groups: #load members details group.update_group(members_details=True) group_list.append(group.puid) if self.set_groups(group_list): if self.change_wxxnr_redis_data({'create_flag': 0}): return 1 else: return 1 except Exception, e: print e
def send_command(command): wxbot_id = command['wxbot_id'] wxbot_port = eval(r.get(wxbot_id))['wxbot_port'] #查询redis得到该wxbot_id对应的wxbot_port try: client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((LOCALHOST_IP, wxbot_port)) client.send(json.dumps(command)) buf = [] while True: d = client.recv(8192) if d: buf.append(d) else: break data = ''.join(buf) if data: return json.loads(data) else: return None except Exception,e: print e
def xnr_logout(wxbot_id): #无论监听群消息的端口开着与否都要保证执行完logout后是关闭状态 command = {'opt': 'logout', 'wxbot_id': wxbot_id} result = send_command_without_recv(command) start_time = time.time() if not result: #说明端口没有打开,只需要更改状态就行了 if change_wxxnr_redis_data(wxbot_id, xnr_data={'status': 'logout'}): return 1 while True: end_time = time.time() d = r.get(wxbot_id) if d: status = eval(d)['status'] if (status == 'logout') and result: #再执行一次,让server端执行server.close()的代码 result = send_command_without_recv(command) if result: return 1 break if int(end_time - start_time) > 8: break return 0
def start_bot(wx_id, wxbot_id, wxbot_port, submitter=None, mail=None, access_id=None, remark=None, init_groups_list='', create_flag=0): #在logout完善之前,在登录之前先手动把status数据更改成logout,并执行logout change_wxxnr_redis_data(wxbot_id, xnr_data={ 'status': 'logout', 'qr_path': '', 'wx_id': wx_id, 'wxbot_port': wxbot_port }) #测试一下端口是否可用,不可用的话就更换端口 if IsOpen(LOCALHOST_IP, wxbot_port): #端口可用 print 'wxbot_port ok', wxbot_port pass else: wxbot_port = find_port(get_all_ports()) change_wxxnr_redis_data(wxbot_id, xnr_data={'wxbot_port': wxbot_port}) data = {'wxbot_port': wxbot_port} es.update(index=wx_xnr_index_name, doc_type=wx_xnr_index_type, body={'doc': data}, id=wxbot_id) print 'new wxbot_port:', wxbot_port if submitter != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'submitter': submitter}) if mail != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'mail': mail}) if access_id != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'access_id': access_id}) if remark != None: change_wxxnr_redis_data(wxbot_id, xnr_data={'remark': remark}) if create_flag: change_wxxnr_redis_data(wxbot_id, xnr_data={'create_flag': create_flag}) logout_result = xnr_logout(wxbot_id) if logout_result: print u'登录前登出成功' else: print u'登录前登出失败' #login path2 = os.path.dirname(path1) wxxnr_login_path = os.path.join(path2, WX_LOGIN_PATH) #print 'wxxnr_login_path: ', wxxnr_login_path if init_groups_list: base_str = 'python ' + wxxnr_login_path + ' -i ' + wxbot_id + ' -p ' + str( wxbot_port) + ' -g ' + init_groups_list else: base_str = 'python ' + wxxnr_login_path + ' -i ' + wxbot_id + ' -p ' + str( wxbot_port) p_str1 = base_str + ' >> wxxnr_login' + str(wxbot_port) + '.txt' #p_str1 = base_str command_str = base_str p_str2 = 'pgrep -f ' + '"' + command_str + '"' process_ids = subprocess.Popen(p_str2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) process_id_list = process_ids.stdout.readlines() for process_id in process_id_list: process_id = process_id.strip() kill_str = 'kill -9 ' + process_id p2 = subprocess.Popen(kill_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p2 = subprocess.Popen(p_str1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) ''' #如果确定没错误,就注释掉这个 for line in p2.stdout.readlines(): print 'line: ', line ''' #检测登陆状态,返回登陆所需二维码路径或者返回缓存登录成功的标志:loginedwithcache while True: d = r.get(wxbot_id) if d: try: qr_path = eval(d)['qr_path'] # if qr_path: # print 'qr_path', qr_path #使用缓存登陆时,qr_path对应的二维码文件不存在 if qr_path == 'loginedwithcache': return qr_path if ('.png' in qr_path) and (os.path.isfile(qr_path)): #发送二维码图片至邮箱 print 'start send mail ...' if send_qrcode2mail(wxbot_id, qr_path): print 'send mail SUCCESS' else: print 'send mail FAIL' return qr_path except Exception, e: print e return 0