博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sae flask 微信公众平台开发
阅读量:5308 次
发布时间:2019-06-14

本文共 2863 字,大约阅读时间需要 9 分钟。

index.wsgi启动服务文件

import saefrom evilxr import appapplication = sae.create_wsgi_app(app)

 evilxr.py

# -*- coding: utf-8 -*-import timeimport MySQLdbimport hashlibfrom flask import Flask, g, request, make_response, render_template,\                url_forimport xml.etree.ElementTree as ETfrom sae.const import (MYSQL_HOST, MYSQL_HOST_S,   MYSQL_PORT, MYSQL_USER, MYSQL_PASS, MYSQL_DB)app = Flask(__name__)app.debug = True@app.before_requestdef before_request():     g.db = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS,                          MYSQL_DB, port=int(MYSQL_PORT))@app.teardown_requestdef teardown_request(exception):    if hasattr(g, 'db'):        g.db.close()@app.route('/',methods=['GET','POST'])def wechat_auth():    if request.method == 'GET':        token='weixin123'        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()#获得post来的数据        xml_rec = ET.fromstring(rec)        from_user=xml_rec.findtext(".//FromUserName")        to_user=xml_rec.findtext(".//ToUserName")        create_time=xml_rec.findtext(".//CreateTime")        msg_type=xml_rec.findtext(".//MsgType")        content = xml_rec.findtext(".//Content")        Event=xml_rec.findtext(".//Event")        if msg_type=="text":            if content == "baidu":                reply = 'http://www.baidu.com/'            elif content == "1":                c = g.db.cursor()                c.execute('select * from user where uid=1')                msgs = list(c.fetchall())                msgs.reverse()                for row in msgs:                	reply = str(row)        elif msg_type=="image":            reply="图片消息"        elif msg_type=="voice":            reply="语音消息"        elif msg_type=="video":            reply="视频消息"        elif msg_type=="location":            reply="地理消息"        elif msg_type=="link":            reply="链接消息"        elif msg_type=="event" and Event=="subscribe":            reply="关注消息"        else:            reply="未知类型"        texttpl='''''
'''+from_user+'''
'''+to_user+'''
'''+create_time+'''
'''+reply+'''
''' return texttpl response = make_response(texttpl % (from_user,to_user,create_time, reply)) response.content_type='application/xml' return response@app.route('/malice')def malice_index(): ''' check index ''' return render_template('123.html')

 

转载于:https://www.cnblogs.com/evilxr/p/4117722.html

你可能感兴趣的文章
springmvc与Ajax交互
查看>>
图片 滚动切换效果(三)
查看>>
response.setHeader()的用法
查看>>
日期格式化
查看>>
java基础入门-建立能够多client链接的ServerSocket
查看>>
刨根问底Objective-C Runtime(4)- 成员变量与属性
查看>>
hdu4734 数位dp + 小技巧
查看>>
mouseover和mouseenter的区别
查看>>
索引字段的宽度降下来?
查看>>
js 唤起APP
查看>>
专车降价滴滴快车使命终结?
查看>>
Java for LeetCode 098 Validate Binary Search Tree
查看>>
Java for LeetCode 108 Convert Sorted Array to Binary Search Tree
查看>>
改变UITextField placeHolder 字体 颜色
查看>>
关于asp下gridview的一些问题
查看>>
《微信朋友圈,这么玩才赚钱》读书笔记-刘焱飞
查看>>
Factorial(hdu 1124)
查看>>
eclipse控制台中文乱码解决方法
查看>>
ASP.NET加载应用程序域
查看>>
StackExchange.Redis 管道 批量 高性能插入数据
查看>>