在SAE上用python搭建微信公众平台

首先编写config.yaml:
---
name: pyweixin
version: 1
libraries:
- name: lxml
  version: "2.3.4"
...

继续编写index.wsgi:

#coding: utf-8
import os
import hashlib

import sae
#需要的第三方库
import web
from lxml import etree

#TOKEN 到微信公众平台自己设置
config={
	"WEIXIN": 'weixin'}
        
urls = (
    '/weixin', 'weixin'
)

app_root = os.path.dirname(__file__)

class weixin:        
    #GET方法,主要用来注册url
    def GET(self):
        data = web.input()
        #以下是微信公众平台请求的参数
        signature = data.signature
        timestamp = data.timestamp
        nonce = data.nonce
        echostr = data.echostr

        #自己定义的 TOKEN
        token = 'input your taken'
        
        #对微信发送的请求,做验证
        tmplist = [ token, timestamp, nonce ]
        #tmplist.sort()
        tmplist.sort()
        tmpstr = ''.join( tmplist )
        hashstr = hashlib.sha1( tmpstr ).hexdigest()

        #如果相等,返回验证信息
        if hashstr == signature:
            return echostr
        
        #如果不相等,返回错误,并打印调试信息
        print signature,timestamp,nonce
        print tmpstr,hashstr
        return 'Error' + echostr


    def POST(self):
        #接收微信的请求内容
        data = web.data()
        #解析XML内容
        root = etree.fromstring( data )
        child = list( root )
        recv = {}
        for i in child:
            recv[i.tag] = i.text

        #print data
        #print recv
        
        #测试demo 所以接收到啥内容,就原样返回
        textTpl = """<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            <FuncFlag>0</FuncFlag>
            </xml>"""
        echostr = textTpl % (recv['FromUserName'], recv['ToUserName'],recv['CreateTime'],recv['MsgType'],recv['Content'])
        return echostr

app = web.application(urls, globals()).wsgifunc()

application = sae.create_wsgi_app(app)

url中输入:http://appname.sinaapp.com/weixin

taken: input your taken

经过测试可以通过!

参考:SAE搭建微信平台


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值