解析微信服务器发送过来的数据并保存类中 :param data: HTTP Request 的 Body 数据 :param msg_signature: EncodingAESKey 的 msg_signature :param timestamp: EncodingAESKey 用时间戳 :param nonce: EncodingAESKey 用随机数 :raises ParseError: 解析微信服务器数据错误, 数据不合法
(self, data, msg_signature=None, timestamp=None, nonce=None)
| 132 | return signature |
| 133 | |
| 134 | def parse_data(self, data, msg_signature=None, timestamp=None, nonce=None): |
| 135 | """ |
| 136 | 解析微信服务器发送过来的数据并保存类中 |
| 137 | :param data: HTTP Request 的 Body 数据 |
| 138 | :param msg_signature: EncodingAESKey 的 msg_signature |
| 139 | :param timestamp: EncodingAESKey 用时间戳 |
| 140 | :param nonce: EncodingAESKey 用随机数 |
| 141 | :raises ParseError: 解析微信服务器数据错误, 数据不合法 |
| 142 | """ |
| 143 | result = {} |
| 144 | if isinstance(data, six.text_type): # unicode to str(PY2), str to bytes(PY3) |
| 145 | data = data.encode('utf-8') |
| 146 | |
| 147 | if self.conf.encrypt_mode == 'safe': |
| 148 | if not (msg_signature and timestamp and nonce): |
| 149 | raise ParseError('must provide msg_signature/timestamp/nonce in safe encrypt mode') |
| 150 | |
| 151 | data = self.conf.crypto.decrypt_message( |
| 152 | msg=data, |
| 153 | msg_signature=msg_signature, |
| 154 | timestamp=timestamp, |
| 155 | nonce=nonce, |
| 156 | ) |
| 157 | try: |
| 158 | xml = XMLStore(xmlstring=data) |
| 159 | except Exception: |
| 160 | raise ParseError() |
| 161 | |
| 162 | result = xml.xml2dict |
| 163 | result['raw'] = data |
| 164 | result['type'] = result.pop('MsgType').lower() |
| 165 | |
| 166 | message_type = MESSAGE_TYPES.get(result['type'], UnknownMessage) |
| 167 | self.__message = message_type(result) |
| 168 | self.__is_parse = True |
| 169 | |
| 170 | @property |
| 171 | def message(self): |