1.注册sae账号,实名制!实名制!
2.新浪云上的 Python 应用的入口为 index.wsgi:application!,也就是 index.wsgi 这个文件中名为 application 的 callable object,不是用__main__
import sae
from myapp import app
application = sae.create_wsgi_app(app)
上面application为程序的入口,调用的myapp中app
3.接下来就是编写微信验证部分app,代码网上找的
# -*- coding=utf-8 -*-
import time
from flask import Flask,g,request,make_response
import hashlib
import xml.etree.ElementTree as ET
app = Flask(__name__)
app.debug=True
@app.route('/',methods=['GET','POST'])
def wechat_auth():
if request.method == 'GET':
token='weixin' #微信配置所需的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!'
4.在微信测试号上填入url,token验证成功