Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息示例

yipeiwu_com7年前Python基础

本文实例讲述了Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息。分享给大家供大家参考,具体如下:

说明:此代码用于接收手机微信端发送的消息

#-*- coding:utf-8 -*-
from flask import Flask,request
from WXBizMsgCrypt import WXBizMsgCrypt
import xml.etree.cElementTree as ET
import sys
app = Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
    sToken = 'Uxxxx'
    sEncodingAESKey = 'U2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    sCorpID = 'wx1xxxxxxxxxxxxx'
    wxcpt=WXBizMsgCrypt(sToken,sEncodingAESKey,sCorpID)
    #获取url验证时微信发送的相关参数
    sVerifyMsgSig=request.args.get('msg_signature')
    sVerifyTimeStamp=request.args.get('timestamp')
    sVerifyNonce=request.args.get('nonce')
    sVerifyEchoStr=request.args.get('echostr')
    #
    sReqMsgSig = sVerifyMsgSig
    sReqTimeStamp = sVerifyTimeStamp
    sReqNonce = sVerifyNonce
    #
    sResqMsgSig = sVerifyMsgSig
    sResqTimeStamp = sVerifyTimeStamp
    sResqNonce = sVerifyNonce
    #验证url
    if request.method == 'GET':
        ret,sEchoStr=wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp,sVerifyNonce,sVerifyEchoStr)
        print type(ret)
        print type(sEchoStr)
        if (ret != 0 ):
            print "ERR: VerifyURL ret:" + ret
            sys.exit(1)
        return sEchoStr
    #接收客户端消息
    if request.method == 'POST':
        #sReqMsgSig = request.form.get('msg_signature')
        #sReqTimeStamp = request.form.get('timestamp')
        #sReqNonce = request.form.get('nonce')
        #赋值url验证请求相同的参数,使用上面注释掉的request.form.get方式获取时,测试有问题
            sReqMsgSig = sVerifyMsgSig
            sReqTimeStamp = sVerifyTimeStamp
            sReqNonce = sVerifyNonce
        sReqData = request.data
        print sReqData
        ret,sMsg=wxcpt.DecryptMsg( sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
        if (ret != 0):
            print "ERR: VerifyURL ret:"
            sys.exit(1)
        #解析发送的内容并打印
        xml_tree = ET.fromstring(sMsg)
        content = xml_tree.find("Content").text
        print content
    #被动响应消息,将微信端发送的消息返回给微信端
    sRespData = '''<xml>
            <ToUserName><![CDATA[mycreate]]></ToUserName>
            <FromUserName><![CDATA[wx177d1233ab4b730b]]></FromUserName>
            <CreateTime>1348831860</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[''' +content +''']]></Content>
            <MsgId>1234567890123456</MsgId>
            <AgentID>1</AgentID>
            </xml>'''
    ret,sEncryptMsg=wxcpt.EncryptMsg(sRespData, sReqNonce, sReqTimeStamp)
    if( ret!=0 ):
        print "ERR: EncryptMsg ret: " + ret
        sys.exit(1)
    return sEncryptMsg
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=6000,debug=True)

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》。

希望本文所述对大家Python程序设计有所帮助。

相关文章

python暴力解压rar加密文件过程详解

第一次使用csdn写文章,写得不好还请见谅。(运行环境:python3.6) 下了一个带密码的压缩包文件,作为一个刚学python的新手,想着能不能用python暴力破解它,于是在网上...

Linux中安装Python的交互式解释器IPython的教程

IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性。特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键...

Python 实现大整数乘法算法的示例代码

我们平时接触的长乘法,按位相乘,是一种时间复杂度为 O(n ^ 2) 的算法。今天,我们来介绍一种时间复杂度为 O (n ^ log 3) 的大整数乘法(log 表示以 2 为底的对数)...

python利用lxml读写xml格式的文件

之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便。 1. 写xml文件 a) 用etree和objectify from lxml import...

python定时器使用示例分享

复制代码 代码如下:class SLTimer(multiprocessing.Process):    #from datetime import dat...