Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise ResponseEmpty(params.messages.no_output)
if "rt" not in parsed_base["route-table"]:
raise ResponseEmpty(params.messages.no_output)
parsed = parsed_base["route-table"]
validated = JuniperRoute(**parsed)
serialized = validated.serialize().export_dict()
if i == 0:
data.update(serialized)
else:
data["routes"].extend(serialized["routes"])
except xmltodict.expat.ExpatError as err:
log.critical(str(err))
raise ParsingError("Error parsing response data")
except KeyError as err:
log.critical(f"'{str(err)}' was not found in the response")
raise ParsingError("Error parsing response data")
except ValidationError as err:
log.critical(str(err))
raise ParsingError(validation_error_message(*err.errors()))
return data
def deserialize_reply(xml, update_time=False):
"""
反序列化被动回复
:param xml: 待反序列化的xml
:param update_time: 是否用当前时间替换xml中的时间
:raises ValueError: 不能辨识的reply xml
:rtype: wechatpy.replies.BaseReply
"""
if not xml:
return EmptyReply()
try:
reply_dict = xmltodict.parse(xml)["xml"]
msg_type = reply_dict["MsgType"]
except (xmltodict.expat.ExpatError, KeyError):
raise ValueError("bad reply xml")
if msg_type not in REPLY_TYPES:
raise ValueError("unknown reply type")
cls = REPLY_TYPES[msg_type]
kwargs = dict()
for attr, field in cls._fields.items():
if field.name in reply_dict:
str_value = reply_dict[field.name]
kwargs[attr] = field.from_xml(str_value)
if update_time:
kwargs["time"] = time.time()
return cls(**kwargs)
def post_xml(url, data, cert=None):
"""Post data is xml."""
result = requests.post(url, data=data.encode(), cert=cert)
result.encoding = 'utf-8'
try:
result = dict(parse(result.text).get('xml'))
except expat.ExpatError:
result = result.text
return result