def login(self, verifyCode=None): '''登录''' #发送登录请求 packet = '' #包头 packet += Coder.trim('00 00 00 08 02 00 00 00 04 00') packet += Coder.num2hexstr(len(self.qqHexstr) / 2 + 4, 4) packet += self.qqHexstr #TEA加密的包体 packet += self.packSendLoginMessage(verifyCode) #总包长 packet = Coder.num2hexstr(len(packet) / 2 + 4, 4) + packet #发送请求 self.socket.sendall(Coder.hexstr2str(packet)) #接收请求 ret = self.socket.recv() pack = HexPacket(Coder.str2hexstr(ret)) #返回包头 pack.shr(4) pack.shr(8) pack.shr(2 + len(self.qqHexstr) / 2) #返回包体 self.unpackRecvLoginMessage(pack.remain()) if self.alive: #登录成功 threading.Thread(target=self.startHeart).start() #心跳 return True elif self.verify: #需要验证码 pass else: return False
def login(self, verifyCode=None): '''登录''' #发送登录请求 packet = '' #包头 packet += Coder.trim('00 00 00 08 02 00 00 00 04 00') packet += Coder.num2hexstr(len(self.qqHexstr)/2+4, 4) packet += self.qqHexstr #TEA加密的包体 packet += self.packSendLoginMessage(verifyCode) #总包长 packet = Coder.num2hexstr(len(packet)/2+4, 4) + packet #发送请求 self.socket.sendall(Coder.hexstr2str(packet)) #接收请求 ret = self.socket.recv() pack = HexPacket(Coder.str2hexstr(ret)) #返回包头 pack.shr(4) pack.shr(8) pack.shr(2 + len(self.qqHexstr)/2) #返回包体 self.unpackRecvLoginMessage(pack.remain()) if self.alive: #登录成功 threading.Thread(target=self.startHeart).start() #心跳 return True elif self.verify: #需要验证码 pass else: return False
def unpackRecvLoginMessage(data): data = TEA.detea_hexstr(data, '00'*16) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4))-4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4))-4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4))-4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4))-4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 unpackRecvLoginSucceedMessage(pack.remain())
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) print 'undata:', data pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname print 'qqkey', self.qqkey self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True print self.verifyPicHexstr code = self.window.SetVerification(self.verifyPicHexstr) print code self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True threading.Thread(target=Img.showFromHexstr, args=(self.verifyPicHexstr, )).start() code = raw_input(u'请输入验证码:') self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4))-4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4))-4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4))-4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4))-4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True threading.Thread(target=Img.showFromHexstr, args=(self.verifyPicHexstr, )).start() code = raw_input(u'请输入验证码:') self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def Fun_recv(self): list = [] ret = self.socket.recv() if ret: if ret == '0': return try: retdata = Coder.str2hexstr(ret) while retdata: n = retdata[:8] long = Coder.hexstr2num(n) * 2 list.append(retdata[:long]) retdata = retdata[long:] for item in list: qq = self.qqHexstr + '(.*)' data1 = re.findall(qq, item)[0] data = TEA.detea_hexstr(data1, self.qqkey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) cmd = Coder.hexstr2str( pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd print '##################', cmd, '#####################' pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) self.unpack(cmd, body) except: traceback.print_exc() else: print '返回包为空'