微信公众号开发 flask后台的快速部署

来源:互联网 发布:淘宝产品视频 编辑:程序博客网 时间:2024/04/29 17:39

公众号快速部署。

首先需要以下资源: 服务器url (这里用的是新浪SAE ,不会部署的教程 链接在此  http://blog.csdn.net/qq_34963461/article/details/52936665)

                             微信公众测试号: 去微信公众平台申请测试号,注意 测试号不需要审核。


首先,需要写  flask 后台代码,以下以一个简单的后台做为示范。

将如下代码部署到 新浪SAE服务器。注意:这里代码里面的token 值,这个值可填任意值。但是要记住,一会微信要用。

# -*- coding=utf-8 -*-import timefrom flask import Flask,g,request,make_responseimport hashlibimport xml.etree.ElementTree as ETapp = Flask(__name__)app.debug=True@app.route('/',methods=['GET','POST'])def wechat_auth():    if request.method == 'GET':        token='your token' #微信配置所需的token        data = request.args        signature = data.get('signature','')        timestamp = data.get('timestamp','')        nonce = data.get('nonce','')        echostr = data.get('echostr','')        s = [timestamp,nonce,token]        s.sort()        s = ''.join(s)        if (hashlib.sha1(s).hexdigest() == signature):            return make_response(echostr)    else:        rec = request.stream.read()        xml_rec = ET.fromstring(rec)        tou = xml_rec.find('ToUserName').text        fromu = xml_rec.find('FromUserName').text        content = xml_rec.find('Content').text        xml_rep = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>"        response = make_response(xml_rep % (fromu,tou,str(int(time.time())), content))        response.content_type='application/xml'        return response    return 'Hello weixin!'if __name__ == '__main__':    app.run()

注意  其中的

response = make_response(xml_rep % (fromu,tou,str(int(time.time())), content))# 是将xml_rep 的值按顺序返回给微信

这个是返回值。 所以如果要做什么微信自动回复 就将内容添到content中,然后微信前端就会回复给用户,如果需要继续深入关于微信返回值的问题    可以看这篇   https://www.oschina.net/code/snippet_1768500_37529


部署完获得后台网址就可以进行下一步:开始填写微信接口。  其他的都随意填,关键的两个值是下图的箭头所指。

填好后会出现配置成功的标志。 这时就能在微信上使用这个公众号了。  本文所实现的功能是你向公众号发布一条信息,公众号返回同样的信息给你 







0 0
原创粉丝点击