def pack_startContest(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.OPEN_CONTEST_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) ## 客户端系统和版本信息 reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造上报语音答题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.open_contest reqBody.wait_time = 3 ## 单位秒 # 对答题请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() Message = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 ContestMessage = struct.pack('!IH', Msg_len, Msg_flag) + Message return ContestMessage
def pack_startRead(self, token, text, max_time): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.READ_SENTENCE_START_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 999999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造上报朗读题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.read_sentence_start self.text = text self.max_record_time = max_time reqBody.text = text reqBody.max_record_time = max_time ## 毫秒 # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() readMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 startMessage = struct.pack('!IH', Msg_len, Msg_flag) + readMessage return startMessage
def pack_VoiceQuery(self, token, level): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.VOICE_READ_QUERY_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101000012 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造查询语音统计请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.voice_read_query reqBody.question_id = self.questionId reqBody.level = level # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() voiceMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 queryMessage = struct.pack('!IH', Msg_len, Msg_flag) + voiceMessage return queryMessage
def pack_rewardEveryone(self, token, min, max): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.REWARD_EVERYONE_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 999999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造上报朗读题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.reward_everyone reqBody.max_reward = max reqBody.min_reward = min reqBody.teacher_id = self.userId # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() rewardMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 packMessage = struct.pack('!IH', Msg_len, Msg_flag) + rewardMessage return packMessage
def pack_rejoin(self, token): reqPack = logical_pb2.RequestPackage() commFrame = reqPack.head_frame commFrame.msg_type = msg_type_pb2.RE_JOIN_REQ commFrame.msg_no = 'wk_test_' + str(random.randint(1, 99999)) # 暂时采用随机数 commFrame.msg_from_user_id = self.userId commFrame.msg_to_user_id = "" commFrame.device_type = 0 commFrame.version = 101000012 # commFrame.timestamp = int(time.time() * 1000) commFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) commFrame.client_info.os_name = "windows" commFrame.client_info.client_version = "wkai2133" commFrame.extended_fields['from'] = 'multiuser_test' # 设置请求包是否压缩标志,0 不压缩,1 压缩 msg_flag = int('0x0000', 16) rejoin_message = logical_pb2.RequestMessage() rejoin_message.token = token joinRoom = rejoin_message.re_join # 对RequestPackage请求数据包进行序列化 reqPack.logical_frame = rejoin_message.SerializeToString() rejoin_pack = reqPack.SerializeToString() # 计算请求封包的长度 msg_len = reqPack.ByteSize() + 2 final_message = struct.pack('!IH', msg_len, msg_flag) + rejoin_pack return final_message
def pack_getLiveconfig(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.GET_LIVE_CONFIG_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101000012 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.get_live_config # 对答题请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() reqMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 getconfigMessage = struct.pack('!IH', Msg_len, Msg_flag) + reqMessage return getconfigMessage
def pack_leaveReq(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.STUDENT_CHAT_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 9999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId # 12976231 老师ID reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造聊天请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.leave_room reqBody.reason = logical_pb2.LeaveReason reqBody.errmsg= "" # 对聊天请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() leaveReqMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 leaveReqMessage = struct.pack('!IH', Msg_len, Msg_flag) + leaveReqMessage return leaveReqMessage
def pack_publishNotice(self, token, content, link): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.NOTICE_PUBLISH_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 999999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.notice_publish reqBody.notice_content = content reqBody.notice_link = link # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() noticeMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 publishMessage = struct.pack('!IH', Msg_len, Msg_flag) + noticeMessage return publishMessage
def pack_voteStop(self, token, question_id): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.VOTE_STOP_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 4 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造答题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.vote_stop reqBody.question_id = question_id reqBody.type = self.type # 对结束答题请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() Message = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 StopMessage = struct.pack('!IH', Msg_len, Msg_flag) + Message return StopMessage
def pack_readQuery(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.READ_SENTENCE_QUERY_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 999999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) # 构造轮询朗读题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.read_sentence_query reqBody.question_id = self.questionId # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() req_msg = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 queryMessage = struct.pack('!IH', Msg_len, Msg_flag) + req_msg return queryMessage
def pack_CoursewareExerciseStop(self, token, courseware_exercise_id): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.COURSEWARE_EXERCISE_STOP_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101000012 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) # 构造查询请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.courseware_exercise_stop_req reqBody.courseware_exercise_id = courseware_exercise_id # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 exerciseStopMessage = struct.pack( '!IH', Msg_len, Msg_flag) + reqPack.SerializeToString() return exerciseStopMessage
def pack_chatOnoff(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.CHAT_CONTROL_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 999999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) # 构造控制聊天请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.chat_control reqBody.operation = self.operation # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() control_message = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 chatOnoff_msg = struct.pack('!IH', Msg_len, Msg_flag) + control_message return chatOnoff_msg
def pack_joinReq(self): reqPack = logical_pb2.RequestPackage() commFrame = reqPack.head_frame commFrame.msg_type = msg_type_pb2.JOIN_ROOM_REQ commFrame.msg_no = 'wk_test_' + str(random.randint(1, 99999)) # 暂时采用随机数 commFrame.msg_from_user_id = self.userId # 12976231 老师ID commFrame.msg_to_user_id = "" commFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 commFrame.version = 101001301 # commFrame.timestamp = int(time.time() * 1000) commFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) ## 客户端系统和版本信息 commFrame.client_info.os_name = platform.system() commFrame.client_info.os_version = platform.win32_ver()[0] commFrame.client_info.client_version = "wkai2133" commFrame.extended_fields['from'] = 'multiuser_test' # 设置请求包是否压缩标志,0 不压缩,1 压缩 msg_flag = int('0x0000', 16) join_Room_message = logical_pb2.RequestMessage() joinRoom = join_Room_message.join_room joinRoom.nickname = self.nickName joinRoom.user_id = self.userId joinRoom.avatar_url = 'https://cdn.17xueba.com//pro/server/image/2018/01/20180122134340183387.png' joinRoom.live_id = self.liveId joinRoom.user_type = self.userType # 可选值 1学生,2助教,3老师 joinRoom.room_index = self.roomIndex joinRoom.timestamp = int(time.time() * 1000) joinRoom.app_id = self.appId # 将所有参数赋值给字典变量,计算签名 para_dict = dict() para_dict['nickname'] = joinRoom.nickname para_dict['user_id'] = joinRoom.user_id para_dict['avatar_url'] = joinRoom.avatar_url para_dict['live_id'] = joinRoom.live_id para_dict['user_type'] = joinRoom.user_type para_dict['room_index'] = joinRoom.room_index para_dict['timestamp'] = joinRoom.timestamp para_dict['app_id'] = joinRoom.app_id joinRoom.sign = addMd5.addSign(para_dict, self.appSecret) # 对RequestPackage请求数据包进行序列化 reqPack.logical_frame = join_Room_message.SerializeToString() joinReqPack = reqPack.SerializeToString() # 计算请求封包的长度 msg_len = reqPack.ByteSize() + 2 joinReq_message = struct.pack('!IH', msg_len, msg_flag) + joinReqPack return joinReq_message
def pack_CoursewareExerciseStart(self, token, courseware_id, courseware_url, questions_count, online_user_count, courseware_questions, page_num, countdown_seconds): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.COURSEWARE_EXERCISE_START_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ## 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101000012 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) # 构造查询请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.courseware_exercise_start_req reqBody.courseware_id = courseware_id reqBody.courseware_url = courseware_url reqBody.questions_count = questions_count reqBody.online_user_count = online_user_count for question in courseware_questions: # 对复合类型(message),调用add方法初始化新实例,再对该实例中的每一个元素进行赋值 new_data = reqBody.courseware_questions.add() for answer in question.right_answer: new_data.right_answer.append(answer) new_data.question_type = question.question_type new_data.submit_type = question.submit_type reqBody.page_num = page_num reqBody.countdown_seconds = countdown_seconds # 对请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 startMessage = struct.pack('!IH', Msg_len, Msg_flag) + reqPack.SerializeToString() return startMessage
def pack_teaChat(self, token): # 构造学生聊天req消息 chatPack = logical_pb2.RequestPackage() chatCommFrame = chatPack.head_frame chatCommFrame.msg_type = msg_type_pb2.CHAT_REQ chatCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 chatCommFrame.msg_from_user_id = self.userId # 12976231 老师ID chatCommFrame.msg_to_user_id = "" chatCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 chatCommFrame.version = 101001301 chatCommFrame.ip = IPConver.ip2int(socket.gethostbyname(socket.gethostname())) chatCommFrame.client_info.os_name = "windows" chatCommFrame.client_info.client_version = "wkai2133" chatCommFrame.extended_fields['from'] = 'multiuser_test' # 构造聊天请求逻辑帧 chat_message = logical_pb2.RequestMessage() chat_message.token = token chat_reqMsg = chat_message.chat #chat_reqMsg.chat_id = random.randint(1, 99999) chat_reqMsg.chat_id = 0 chat_reqMsg.content = self.content # chat_reqMsg.to = self.user_group # chat_reqMsg.user_id = self.userId # chat_reqMsg.nickname = u'老师昵称1' # chat_reqMsg.avatar_url = 'https://cdn.17xueba.com//pro/server/image/2018/01/20180122134340183387.png' # chat_reqMsg.user_type = 3 # chat_reqMsg.device_type = 0 # chat_reqMsg.ip = ip2int(socket.gethostbyname(socket.gethostname())) # chat_reqMsg.adornment_url = 'https://static.17xueba.com/test/server/image/2018/08/20180809180753441799.png' # 对聊天请求数据包进行序列化 chatPack.logical_frame = chat_message.SerializeToString() chat_ReqMessage = chatPack.SerializeToString() chatMsg_flag = int('0x0000', 16) # 计算请求封包的长度 chatMsg_len = chatPack.ByteSize() + 2 chat_ReqMessage = struct.pack('!IH', chatMsg_len, chatMsg_flag) + chat_ReqMessage return chat_ReqMessage
def pack_stopContest(self, token): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.CLOSE_CONTEST_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 0 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) ## 客户端系统和版本信息 reqCommFrame.client_info.os_name = platform.system() reqCommFrame.client_info.os_version = platform.win32_ver()[0] reqCommFrame.client_info.client_version = "1.1.13.1" reqCommFrame.client_info.browser_name = "" reqCommFrame.client_info.browser_version = "" reqCommFrame.client_info.brand_name = "" reqCommFrame.client_info.browser_ua = "" # 构造上报语音答题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.close_contest reqBody.question_id = self.questionId ## 单位秒 # 对答题请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() Message = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 stopMessage = struct.pack('!IH', Msg_len, Msg_flag) + Message return stopMessage
def pack_voteStart(self, token, vote_type): reqPack = logical_pb2.RequestPackage() reqCommFrame = reqPack.head_frame reqCommFrame.msg_type = msg_type_pb2.VOTE_START_REQ reqCommFrame.msg_no = 'wk_tt_' + str(random.randint(1, 99999)) # 采用随机数 reqCommFrame.msg_from_user_id = self.userId reqCommFrame.msg_to_user_id = "" reqCommFrame.device_type = 4 ### 设备类型,0 pc 1 ios 2 android 3 手机网页 4 pc网页 reqCommFrame.version = 101001301 # reqCommFrame.timestamp = int(time.time() * 1000) reqCommFrame.ip = IPConver.ip2int( socket.gethostbyname(socket.gethostname())) reqCommFrame.client_info.os_name = "windows" reqCommFrame.client_info.client_version = "wkai2133" reqCommFrame.extended_fields['from'] = 'multiuser_test' # 构造答题请求逻辑帧 req_message = logical_pb2.RequestMessage() req_message.token = token reqBody = req_message.vote_start if vote_type == logical_pb2.SIGNLE_RIGHT_FOUR_CHOICE: self.type = vote_type reqBody.type = self.type nameList = ['A', 'B', 'C', 'D'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" if option.option_name == 'A': option.is_right = True else: option.is_right = False reqBody.vote_description = "" #reqBody.options_array[0].is_right = True elif vote_type == logical_pb2.SIGNLE_RIGHT_MULTIPLE_CHOICE: self.type = vote_type reqBody.type = self.type nameList = ['A', 'B', 'C', 'D', 'E', 'F'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" if option.option_name == 'B': option.is_right = True else: option.is_right = False reqBody.vote_description = "" elif vote_type == logical_pb2.MULTIPLE_CHOICE: self.type = vote_type reqBody.type = self.type nameList = ['A', 'B', 'C', 'D', 'E', 'F'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" reqBody.options_array[0].is_right = True reqBody.options_array[1].is_right = True reqBody.options_array[2].is_right = True reqBody.vote_description = "" elif vote_type == logical_pb2.NO_RIGHT_CHOICE: self.type = vote_type reqBody.type = self.type nameList = ['A', 'B', 'C', 'D', 'E'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" option.is_right = False reqBody.vote_description = "" elif vote_type == logical_pb2.NO_RIGHT_CHOICE_MULTI: self.type = vote_type reqBody.type = self.type nameList = ['A', 'B', 'C', 'D', 'E', 'F'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" option.is_right = False reqBody.vote_description = "" elif vote_type == logical_pb2.NO_RIGHT_SURVEY: self.type = vote_type reqBody.type = self.type nameList = ['yes', 'no'] for element in nameList: option = reqBody.options_array.add() option.option_name = element option.description = "" option.is_right = False reqBody.vote_description = u"上课能够听懂吗" else: pass # 对答题请求数据包进行序列化 reqPack.logical_frame = req_message.SerializeToString() voteMessage = reqPack.SerializeToString() Msg_flag = int('0x0000', 16) # 计算请求封包的长度 Msg_len = reqPack.ByteSize() + 2 VotestartMessage = struct.pack('!IH', Msg_len, Msg_flag) + voteMessage return VotestartMessage