python+web.py 搭建微信公众平台(1)
2017-10-11 09:47
525 查看
公众号平台指导网址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5
配置python开发环境就不再细说。
windows xp x86用户可参考:http://blog.csdn.net/qinmin5527/article/details/50902099
安装web.py环境
web.py是一个轻量级的python web框架,官网地址:http://webpy.org/
linux下安装:(这里我用的是树莓派),windows用户参见文章开头的地址。
安装python-lxml:
微信使用XML格式文件进行数据交换,需要使用lxml来解析
首先放几个地址:
开发者文档 : http://mp.weixin.qq.com/wiki/home/index.html
接口测试号申请:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
接口调试工具:http://mp.weixin.qq.com/debug/
一开始平台还没有搭好的时候可以申请一个测试号,权限基本都有,方便调试。
建议开始之前浏览一下开发者文档中的接入指南。
微信要求的服务器配置如下图。可以看出,有两个必填项:URL,Token。其中URL就不必说了,服务器的地址(这里我申请了花生壳二级域名,放链接:http://hsk.oray.com/)。Token相当于你与微信约定的密码,一会微信会使用这个Token去连接你的服务器。随便填一串字符串就可以(要记下来)。
然后上代码:
(下面import的模块有些没有用到,不过没什么影响,自己判断吧)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
然后在你的py文件同级目录下新建一个templates文件夹,在文件夹中新建一个xml文件:reply_text.xml,写入以下代码:
2
3
4
5
6
7
8
OK,保存,使用命令行运行你的py文件,返回一个网址:http://0.0.0.0:8080/
【未完待续】
配置python开发环境就不再细说。
windows xp x86用户可参考:http://blog.csdn.net/qinmin5527/article/details/50902099
1.web环境配置
安装web.py环境 web.py是一个轻量级的python web框架,官网地址:http://webpy.org/
linux下安装:(这里我用的是树莓派),windows用户参见文章开头的地址。
sudo apt-get install python-webpy1
安装python-lxml:
微信使用XML格式文件进行数据交换,需要使用lxml来解析
sudo apt-get install python-lxml1
2.微信公众平台开发
首先放几个地址: 开发者文档 : http://mp.weixin.qq.com/wiki/home/index.html
接口测试号申请:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
接口调试工具:http://mp.weixin.qq.com/debug/
一开始平台还没有搭好的时候可以申请一个测试号,权限基本都有,方便调试。
建议开始之前浏览一下开发者文档中的接入指南。
微信要求的服务器配置如下图。可以看出,有两个必填项:URL,Token。其中URL就不必说了,服务器的地址(这里我申请了花生壳二级域名,放链接:http://hsk.oray.com/)。Token相当于你与微信约定的密码,一会微信会使用这个Token去连接你的服务器。随便填一串字符串就可以(要记下来)。
然后上代码:
(下面import的模块有些没有用到,不过没什么影响,自己判断吧)
# -*- coding: utf-8 -*- ''' 【功能】 1.响应微信发送的Token验证 2.接收用户文本信息 【更新日期】2016.03.17 ''' import os import web #web.py import time import datetime import hashlib #hash加密算法 from lxml import etree #xml解析 import requests #http请求 import json import codecs #===================微信公众账号信息================================ #把微信开发页面中的相关信息填进来,字符串格式 my_appid = '******************' my_secret = '*****************************' #========匹配URL的正则表达式,url/将被WeixinInterface类处理=========== urls = ( '/','WeixinInterface' ) #===================微信权限交互==================================== def _check_hash(data): ''' 响应微信发送的GET请求(Hash校验) :param data: 接收到的数据 :return: True or False,是否通过验证 ''' signature=data.signature #加密签名 timestamp=data.timestamp #时间戳 nonce=data.nonce #随机数 #自己的token token="qinminraspberrypi" #这里改写你在微信公众平台里输入的token #字典序排序 list=[token,timestamp,nonce] list.sort() #拼接成一个字符串进行sha1加密,加密后与signature进行对比 sha1=hashlib.sha1() map(sha1.update,list) hashcode=sha1.hexdigest() #sha1加密算法 #如果是来自微信的请求,则回复echostr if hashcode == signature: return True return False #=====================微信+HTTP server=============================== 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 _reply_text(self, toUser, fromUser, msg): ''' 回复文本消息 :param fromUser: :param toUser: :param msg:要发送到的消息,文本格式 :return: ''' return self.render.reply_text(toUser, fromUser, int(time.time()),msg + '\n\ntime: ' + now_time)#加入时间戳 def _recv_text(self, fromUser, toUser, xml): ''' 接收到文本消息,自动回复 :param fromUser: :param toUser: :param xml:收到的xml文件 :return: ''' #提取xml中Content文本信息 content = xml.find('Content').text reply_msg = content return self._reply_text(fromUser, toUser, u'你刚才说的是:' + reply_msg ) def GET(self): #get,从指定的资源请求数据 #获取输入参数 data = web.input() if _check_hash(data): return data.echostr #微信发来的随机字符串,若验证通过,则返回echostr def POST(self): #post,向指定的资源提交数据 str_xml = web.data() #获得post来的数据 xml = etree.fromstring(str_xml)#进行XML解析 msgType=xml.find("MsgType").text #消息类型,包括text/event/image/voice/location/link等 fromUser=xml.find("FromUserName").text toUser=xml.find("ToUserName").text #对不同类型的消息分别处理: if msgType == 'text': return self._recv_text(fromUser, toUser, xml) #=====================启动app======================================== if __name__ == "__main__": app = web.application(urls,globals()) app.run()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
然后在你的py文件同级目录下新建一个templates文件夹,在文件夹中新建一个xml文件:reply_text.xml,写入以下代码:
$def with (toUser,fromUser,createTime,content) <xml> <ToUserName><![CDATA[$toUser]]></ToUserName> <FromUserName><![CDATA[$fromUser]]></FromUserName> <CreateTime>$createTime</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[$content]]></Content> </xml>1
2
3
4
5
6
7
8
OK,保存,使用命令行运行你的py文件,返回一个网址:http://0.0.0.0:8080/
【未完待续】
相关文章推荐
- 树莓派上搭建基于Python+web.py+fastcgi+lighttpd的网站
- Linux下搭建基于Nginx+FastCGI+Flup+Webpy+Cheetah的Python Web环境
- 在GAE(Google App Engine)上搭建python2.7的web.py程序
- Python抓取优酷视频(下):使用web.py搭建网站框架
- linux下搭建nginx+uwsgi+web.py的python web服务器
- 在windows下搭建python环境,安装python、pip、virtualen、web.py
- Windows下Python Web开发环境搭建 (Notepad++ & Web.py)
- webpy+python2.7+linux环境搭建
- centos7简单搭建nginx+uwsgi+web.py的Python web环境(记录)
- 搭建Apache+web.py+mod_wsgi python服务器
- Python web 框架webpy 学习体验-人生苦短我用Python为鬼叔代言
- nginx python spawn-fcgi Flup webpy搭建python的web环境
- 用python快速搭建WEB服务器
- python SimpleHTTPServer 快速搭建Web服务器
- 在apache上部署web.py项目(python2.7+web.py+mod_wsgi+apache2.2)
- Vagrant Virtual Box 搭建Python Web 开发环境
- 【Python】windows下Eclipse中安装集成webpy框架
- Python进阶(三十六)-Web框架Django项目搭建全过程
- IIS+Python+CGI Web服务器搭建