def unpack(buf): if len(buf) < 16: #包头不完整 return (UNPACK_CONTINUE, b'') else: #解析包头 header = buf[:16] (len_ack, cmd_id_ack, seq_id_ack) = struct.unpack('>I4xII', header) logger.info('收到新的封包,长度:{},cmd_id:{},seq_id:0x{:x}'.format( len_ack, cmd_id_ack, seq_id_ack)) #包长合法性验证 if len(buf) < len_ack: #包体不完整 return (UNPACK_CONTINUE, b'') else: if CMDID_PUSH_ACK == cmd_id_ack and PUSH_SEQ == seq_id_ack: #推送通知:服务器有新消息 #尝试获取sync key sync_key = Util.get_sync_key() if sync_key: #sync key存在 interface.new_sync() #使用newsync同步消息 longlink.send( pack(CMDID_REPORT_KV_REQ, business.sync_done_req2buf())) #通知服务器消息已接收 else: #sync key不存在 interface.new_init() #使用短链接newinit初始化sync key else: cmd_id = cmd_id_ack - 1000000000 if CMDID_NOOP_REQ == cmd_id: #心跳响应 logger.info('心跳返回:{}'.format(Util.b2hex(buf))) pass elif CMDID_MANUALAUTH_REQ == cmd_id: #登录响应 if business.login_buf2Resp(buf[16:len_ack], login_aes_key): raise RuntimeError('登录失败!') #登录失败 return (UNPACK_OK, buf[len_ack:]) return (UNPACK_OK, b'')
def Login(name, password): #组包 (senddata, login_aes_key) = business.login_req2buf(name, password) #发包 ret_bytes = Util.mmPost('/cgi-bin/micromsg-bin/manualauth', senddata) logger.debug('返回数据:' + str(ret_bytes)) #解包 return business.login_buf2Resp(ret_bytes, login_aes_key)