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='''''''' 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') '''+from_user+''' '''+to_user+''' '''+create_time+''' '''+reply+'''