这篇文章主要介绍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()
以上是“python中tornado微信开发的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。