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

yipeiwu_com6年前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实现log日志的示例代码

源代码: # coding=utf-8 import logging import os import time LEVELS={'debug':logging.DEBUG,\...

Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解

Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解

本文实例讲述了Python PyAutoGUI模块控制鼠标和键盘实现自动化任务。分享给大家供大家参考,具体如下: PyAutoGUI是用Python写的一个模块,使用它可以控制鼠标和键盘...

idea创建springMVC框架和配置小文件的教程图解

idea创建springMVC框架和配置小文件的教程图解

这个框架主要还是思想,之后,,,还是创建项目好了, 1.新建一个项目 新建一个maven,并且选择webapp类型。 2.点击next选项 这里面的两个选项可以随便填,但是Artif...

深入浅析Python 中 is 语法带来的误解

起步 Python 的成功一个原因是它的可读性,代码清晰易懂,更容易被人类所理解,但有时可读性会产生误解。 假如要判断一个变量是不是 17,那可以: if x is 17: x 是 17...

python中时间、日期、时间戳的转换的实现方法

1.简介 在编写代码时,往往涉及时间、日期、时间戳的相互转换。 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字...