sae flask 微信公众平台开发

index.wsgi启动服务文件

import sae

from evilxr import app

application = sae.create_wsgi_app(app)

 evilxr.py

# -*- coding: utf-8 -*-



import time

import MySQLdb

import hashlib

from flask import Flask, g, request, make_response, render_template,\

                url_for



import xml.etree.ElementTree as ET



from sae.const import (MYSQL_HOST, MYSQL_HOST_S,

   MYSQL_PORT, MYSQL_USER, MYSQL_PASS, MYSQL_DB

)



app = Flask(__name__)

app.debug = True



@app.before_request

def before_request():

     g.db = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS,

                          MYSQL_DB, port=int(MYSQL_PORT))



@app.teardown_request

def 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='''''<xml>

        <ToUserName>'''+from_user+'''</ToUserName>

        <FromUserName>'''+to_user+'''</FromUserName>

        <CreateTime>'''+create_time+'''</CreateTime>

        <MsgType><![CDATA[text]]></MsgType>

        <Content>'''+reply+'''</Content>

        </xml>'''

        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')

 

你可能感兴趣的:(微信公众平台)