亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: python,flask,SAE(新浪云),搭建開發(fā)微信公眾賬號 [打印本頁]

作者: zhaozy78    時間: 2015-07-07 10:02
標(biāo)題: python,flask,SAE(新浪云),搭建開發(fā)微信公眾賬號
將我們的服務(wù)器放在新浪云上,搭建微信公眾賬號,下面的代碼將實現(xiàn)獲取微信token,實現(xiàn)最簡單的消息對話(用戶說什么,我們回復(fù)什么)。
因為網(wǎng)上有的代碼有錯,所以將這個傳上來供大家借鑒
注意,代碼中的空格不能多,會出錯,要嚴(yán)格縮進(jìn)對齊,然后使用下面的代碼一定是無錯的。
下面文件依次為:
第一個代碼文件是index.wsgi 文件 ,它是啟動服務(wù)的文件
第二個代碼文件是config.yaml文件 , 配置文件
第三個代碼文件是meishidaren.py 文件,用于由微信開發(fā)模式獲取url 和 token,同時可以與用戶實現(xiàn)簡單通信

[Python]代碼
  1. import sae
  2. from meishidaren import app
  3. application = sae.create_wsgi_app(app)
復(fù)制代碼
[Python]代碼
  1. name: gourmetmaster
  2. version: 1
復(fù)制代碼
meishidaren.py
  1. import time
  2. from flask import Flask,g,request,make_response
  3. import hashlib
  4. import xml.etree.ElementTree as ET
  5. app = Flask(__name__)
  6. app.debug=True
  7. @app.route('/',methods=['GET','POST'])
  8. def wechat_auth():
  9.     if request.method == 'GET':
  10.         token='liusicong'
  11.         data = request.args
  12.         signature = data.get('signature','')
  13.         timestamp = data.get('timestamp','')
  14.         nonce = data.get('nonce','')
  15.         echostr = data.get('echostr','')
  16.         s = [timestamp,nonce,token]
  17.         s.sort()
  18.         s = ''.join(s)
  19.         if (hashlib.sha1(s).hexdigest() == signature):
  20.             return make_response(echostr)
  21.     else:
  22.         rec = request.stream.read()
  23.         xml_rec = ET.fromstring(rec)
  24.         tou = xml_rec.find('ToUserName').text
  25.         fromu = xml_rec.find('FromUserName').text
  26.         content = xml_rec.find('Content').text
  27.         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>"
  28.         response = make_response(xml_rep % (fromu,tou,str(int(time.time())), content))
  29.         response.content_type='application/xml'
  30.         return response
復(fù)制代碼





歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2