惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
F
Fortinet All Blogs
I
InfoQ
Jina AI
Jina AI
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
B
Blog RSS Feed
C
Check Point Blog
Y
Y Combinator Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
Engineering at Meta
Engineering at Meta
G
Google Developers Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
flask搭建微信公众号服务端|OhYee 博客
2018-04-09 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

微信公众号会向接口处发送不同的数据
GET数据是验证信息,你需要计算传来的数据,判断是否符合要求,如果符合,返回传来的echostr字段
POST数据是数据处理,用户输入等内容以xml的格式post到同一个地址,你需要处理xml数据,根据字段进行相应操作。其中,返回时同样是xml格式的信息,并且from和to部分要交换位置。

对应代码如下,其中userInput是根据不同输入进行不同处理的函数

@app.route('/api/weixin/', methods=['GET', 'POST'])
def apiweixin():
    if request.method == 'GET':
        token = 'ohyee'
        query = request.args
        signature = query.get('signature', '')
        timestamp = query.get('timestamp', '')
        nonce = query.get('nonce', '')
        echostr = query.get('echostr', '')
        s = [timestamp, nonce, token]
        s.sort()
        s = ''.join(s)
        if (hashlib.sha1(s).hexdigest() == signature):
            return make_response(echostr)
        else:
            return make_response("error")
    else:
        from xml.etree import ElementTree
        xml = ElementTree.fromstring(request.data)
        ToUserName = xml.find("ToUserName").text
        FromUserName = xml.find("FromUserName").text
        Content = xml.find("Content").text
        reply = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>"
        print("%s->%s %s" % (FromUserName, ToUserName, Content))
        Content = userInput(Content)
        response = make_response(
            reply % (FromUserName, ToUserName, str(int(time.time())), Content))
        response.content_type = 'application/xml'
        return response