1 新浪SAE應用創建
登陸新浪SAE后點擊控制臺->運應用SAE->創建新應用
創建新應用.png
上圖中二級域名和應用名稱的兩個 yourappname 自己填
沒有云豆的話沖個10塊錢可以玩很久
2 程序編寫
共有兩個文件index.wsgi和mainapp.py
2.1 index.wsgi
import sae
from mainapp import app
application = sae.create_wsgi_app(app)
2.1 mainapp.py
注意文件中自己填的一串字母
# -*- coding:utf8 -*-
import hashlib
import time
from flask import Flask, request, make_response
import xml.etree.ElementTree as ET
app = Flask(__name__)
app.debug = True
@app.route("/otherpage/")
def otherpage():
return "otherpage!"
@app.route("/")
def hello():
return "main page!"
@app.route('/wechat_api/', methods = ['GET', 'POST'] )
def wechat_auth(): #處理微信請求的處理函數,get方法用于認證,post方法取得微信轉發的數據
if request.method == 'GET':
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):
response = make_response(echostr)
response.headers['content-type'] = 'text'
return response
3 上傳代碼并測試
3.1 上傳代碼
在SAE代碼管理中創建一個版本,將index.wsgi和mainapp.py打包到一個.zip文件中上傳且兩個文件位于代碼包根目錄(本人不會git和svn)
上傳并測試.jpg
3.2 測試應用是否運行
點擊3.1節圖中的鏈接下的紅色url顯示在mainapp.py hello()中定義返回的"main page!"則表示app已經開始運行
正確的測試結果.png
4 微信配置
注意服務器地址為你自己的地址,Token為你在mainapp.py wechat_auth()中填的那串字符串
微信配置.png