码迷,mamicode.com
首页 > 微信 > 详细

python3 flask 微信公众号消息接收测试代码

时间:2020-12-29 11:45:23      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:methods   time()   mat   hash   处理   dex   请求方式   audio   参数   

code

# -*- coding:utf-8 -*-
from flask import Flask
from flask import request
import hashlib
import time
import re
import xml.etree.ElementTree as ET

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World!"

@app.route("/wechat", methods=["GET","POST"])
def weixin():
    if request.method == "GET":     # 判断请求方式是GET请求
        my_signature = request.args.get(signature)     # 获取携带的signature参数
        my_timestamp = request.args.get(timestamp)     # 获取携带的timestamp参数
        my_nonce = request.args.get(nonce)        # 获取携带的nonce参数
        my_echostr = request.args.get(echostr)         # 获取携带的echostr参数
        # my_token = request.args.get(token)
        print(my_signature)
        print(my_timestamp)
        print(my_nonce)
        print(my_echostr)
        # print(my_token)
        token = 123456     # 一定要跟刚刚填写的token一致

        # 进行字典排序
        data = [token,my_timestamp ,my_nonce ]
        data.sort()
        # 拼接成字符串,进行hash加密时需为字符串
        data = ‘‘.join(data)
        #创建一个hash对象
        s = hashlib.sha1()
        #对创建的hash对象更新需要加密的字符串
        s.update(data.encode("utf-8"))
        #加密处理
        mysignature = s.hexdigest()

        print("handle/GET func: mysignature, my_signature: ", mysignature, my_signature)

        # 加密后的字符串可与signature对比,标识该请求来源于微信
        if my_signature == mysignature:
            return my_echostr
        else:
            return ""
    else:
            # 解析xml
            xml = ET.fromstring(request.data)
            toUser = xml.find(ToUserName).text
            fromUser = xml.find(FromUserName).text
            msgType = xml.find("MsgType").text
            createTime = xml.find("CreateTime")
            # 判断类型并回复
            if msgType == "text":
                content = xml.find(Content).text
                content=content+"已收到"
                return reply_text(fromUser, toUser, content)
            else:
                return reply_text(fromUser, toUser, "我只懂文字")

def reply_text(to_user, from_user, content):
    """
    以文本类型的方式回复请求
    """
    return """
    <xml>
        <ToUserName><![CDATA[{}]]></ToUserName>
        <FromUserName><![CDATA[{}]]></FromUserName>
        <CreateTime>{}</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[{}]]></Content>
    </xml>
    """.format(to_user, from_user, int(time.time() * 1000), content)

if __name__ == "__main__":
    app.run(host=0.0.0.0, port=879)

 

 

 

 

 

 

 

 

 

 

 

python3 flask 微信公众号消息接收测试代码

标签:methods   time()   mat   hash   处理   dex   请求方式   audio   参数   

原文地址:https://www.cnblogs.com/sea-stream/p/14181920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!