www.zhblog.net

Python Flask 微信验证

@app.route('/check', methods=['GET', 'POST'])
def check():
if request.method == 'GET':
"""微信Token验证"""
signature = request.args.get('signature')
timestamp = request.args.get('timestamp')
nonce = request.args.get('nonce')
echostr = request.args.get('echostr')
token = 'zhanheng'

items = [token, timestamp, nonce]
items.sort()
items_str = ''.join(items)

sha1 = hashlib.sha1(items_str.encode('utf-8'))
hashcode = sha1.hexdigest()
if hashcode == signature:
return echostr
else:
return ""

if request.method == 'POST':
"""接收普通文本信息"""
data = request.data
root = ET.fromstring(data)
xml_dict = {child.tag:child.text for child in root}
xml = """<xml>
<ToUserName><![CDATA[{FromUserName}]]></ToUserName>
<FromUserName><![CDATA[{ToUserName}]]></FromUserName>
<CreateTime>{CreateTime}</CreateTime>
<MsgType><![CDATA[{MsgType}]]></MsgType>
<Content><![CDATA[{Content}]]></Content>
</xml>"""
return xml.format(**xml_dict)

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情