本文主要是一步一步教大家如何利用python搭建微信公众平台,有兴趣的朋友可以参考一下
使用的工具,python 新浪sae平台,微信的公众平台
你需要先在微信的公众平台与新浪sae平台上各种注册,微信平台注册的时候需要你拍张手持身份证的照片,还有几天的审核期
微信公众平台:http://mp.weixin.qq.com
新浪sae:http://sae.sina.com.cn/
等待微信公众审核通过后,登录公众平台后,点击高级功能。将会看到需要提供一个接入信息:
微信接口配置
那么我们需要一个网址作为接口(这时就需要sae上搭建python的一个应用),token呢,就是相当于我们和微信之间约定的“密码”,这里可以随便填写英文或者数字,但实测输入纯数字有时会有问题,所以还是字符串比较靠谱。
第一步,在sae上搭建python的应用,在下图的应用里选择python应用。
填好二级域名和应用名称等,选择好语言。这里我们使用python开发选择web应用。创建好应用之后,在代码管理中创建一个新的版本。而后我们可以选择编辑代码。能够实现在线编辑,根本用不着配置本地环境,svn等等。当然像这种轻量级的应用在线编辑器就可以了,svn的话还不如在线编辑好用
第二步,编写index.wsgi
因为我们使用的是web.py框架,因为其良好的xml解析。
首先编写config.yaml
name: yangyanxingversion: 1 libraries:- name: webpy version: 0.36 - name: lxml version: 2.3.4 ...
注意严格的缩进,差一个空格你就废了!而且调试的时候很不好发现问题。。。
接着我们继续编写index.wsgi
# coding: utf-8import os import saeimport web from weixininterface import weixininterface urls = ('/weixin','weixininterface') app_root = os.path.dirname(__file__)templates_root = os.path.join(app_root, 'templates')render = web.template.render(templates_root) app = web.application(urls, globals()).wsgifunc() application = sae.create_wsgi_app(app)
简单解释一下,
from weixininterface import weixininterface
这里我们需要再创建一个weixininterface的py文件,你也可以将这个类写在index.wsgi文件中,只是这样看起来会乱乱的
新建一个weixininterface.py文件,注意大小写,写入以下代码
# -*- coding: utf-8 -*-import hashlibimport webimport lxmlimport timeimport osimport urllib2,jsonfrom lxml import etree class weixininterface: def __init__(self): self.app_root = os.path.dirname(__file__) self.templates_root = os.path.join(self.app_root, 'templates') self.render = web.template.render(self.templates_root) def get(self): #获取输入参数 data = web.input() signature=data.signature timestamp=data.timestamp nonce=data.nonce echostr=data.echostr #自己的token token=yangyanxing #这里改写你在微信公众平台里输入的token #字典序排序 list=[token,timestamp,nonce] list.sort() sha1=hashlib.sha1() map(sha1.update,list) hashcode=sha1.hexdigest() #sha1加密算法 #如果是来自微信的请求,则回复echostr if hashcode == signature: return echostr
这里定义了一个get方法,是根据微信公众平台的要求,进行的token验证,因为这里我们定义了templates_root为根目录下的templates,所以还要在根目录下创建一个目录templates的目录
因为微信是将验证信息get发出去的,所以这里使用了get方法来取得值并且返回相应用值
保存全部,现在回到微信的公众平台高级管理界面
微信接口配置
在url里面填写你在新浪sae里应用名称并且加上/weixin,如:http://xxxx.sinaapp.com/weixin token随便输入,只要注意更改weixininterface.py中的token就行了,输入好了以后点击提交,如果没有什么问题的话就会通过验证!
第三步,新建一个简单的自动回复的方法,鹦鹉学舌,就是用户说什么,它也回复什么,没什么用,只是随便玩玩!
在weixininterface.py里继续添加代码
def post(self): str_xml = web.data() #获得post来的数据 xml = etree.fromstring(str_xml)#进行xml解析 content=xml.find(content).text#获得用户所输入的内容 msgtype=xml.find(msgtype).text fromuser=xml.find(fromusername).text touser=xml.find(tousername).text return self.render.reply_text(fromuser,touser,int(time.time()),u我现在还在开发中,还没有什么功能,您刚才说的是:+content)
这个def 是和上一个get同级的,注意缩进
接着我们在templates目录下创建reply_text.xml模板文件,写入以下代码
$def with (touser,fromuser,createtime,content)$createtime
注意这里的touser与fromuser是刚才post的是相反的,因为这里的touser也就是post函数里的fromuser,这里的fromuser也就是post函数里的touser,msgtype是text
全部保存,现在就在用你的个人微信关注一下你创建的公众微信号,然后随便输入些内容,如果没有什么问题,你将会收到一条鹦鹉学舌的回复内容!
以上就是python搭建微信公众平台的全部内容,大家可以根据以上步骤进行搭建。