python tornado微信开发入门代码
作者:LindenTao 时间:2023-11-01 01:04:59
本文实例为大家分享了python tornado微信开发的具体代码,供大家参考,具体内容如下
#微信入门代码
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import hashlib
import xml.etree.ElementTree as ET
import time
def check_signature(signature, timestamp, nonce):
# 微信公众平台里输入的token
token="linden"
#字典序排序
list = [token,timestamp,nonce]
list.sort()
sha1=hashlib.sha1()
map(sha1.update,list)
hashcode=sha1.hexdigest()
return hashcode == signature
class MainHandler(tornado.web.RequestHandler):
def get(self):
signature = self.get_argument('signature')
timestamp = self.get_argument('timestamp')
nonce = self.get_argument('nonce')
echostr = self.get_argument('echostr')
if check_signature(signature, timestamp, nonce):
self.write(echostr)
else:
self.write('fail')
def post(self):
body = self.request.body
data = ET.fromstring(body)
toUser = data.find('ToUserName').text
fromUser = data.find('FromUserName').text
createTime = int(time.time())
msgType = data.find('MsgType').text
content = data.find('Content').text
msgId= data.find("MsgId").text
# from与to在返回的时候要交换
textTpl = """<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<MsgId>%s</MsgId>
</xml>"""
out = textTpl % (fromUser, toUser, createTime, msgType, content, msgId)
self.write(out)
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(80)
tornado.ioloop.IOLoop.instance().start()
来源:https://blog.csdn.net/u011845833/article/details/51104211
标签:python,tornado,微信


猜你喜欢
ExtJS 开发总结
2009-04-28 13:05:00

基于python的七种经典排序算法(推荐)
2023-06-16 18:21:43

Python数据结构之栈详解
2021-01-07 01:12:36

SQL Server 分页查询通用存储过程(只做分页查询用)
2024-01-12 20:10:11
python实现引用其他路径包里面的模块
2023-02-21 21:57:53

javascript与jsp发送请求到servlet的几种方式实例
2023-06-15 15:59:30
pandas dataframe添加表格框线输出的方法
2021-11-28 01:34:41

Python对字符串实现去重操作的方法示例
2021-12-12 11:46:37

深入理解mysql的自连接和join关联
2024-01-21 11:40:01

pandas学习之df.set_index的具体使用
2021-11-12 07:35:20
Python数据可视化绘图实例详解
2022-06-11 15:51:27

ThinkPHP中pathinfo的访问模式、路径访问模式及URL重写总结
2024-05-05 09:16:33
Python中的基本数据类型讲解
2021-04-29 07:07:24
解读数据库的嵌套查询的性能问题
2024-01-20 17:00:06

Python操作Mysql实例代码教程在线版(查询手册)
2024-01-24 17:07:44

python实现上传文件到linux指定目录的方法
2023-02-05 03:03:56

Python全栈之进程和守护进程
2021-11-25 18:05:44

Python json模块dumps、loads操作示例
2023-01-03 20:16:29
python爬虫开发之PyQuery模块详细使用方法与实例全解
2021-05-28 20:54:29
python自动化实现登录获取图片验证码功能
2022-09-21 13:23:59
