python tornado微信开发入门代码
本文实例为大家分享了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()
相关推荐
九天银河技术 2020-11-11
行万里 2020-10-26
此处省略三千字 2020-10-22
nongfusanquan0 2020-09-23
夜影风个人空间 2020-09-22
adsadadaddadasda 2020-09-08
85251846 2020-09-14
jbossrobbie 2020-08-16
幸福ITman汪文威 2020-08-15
MySQL源氏boy 2020-08-15
MySQLqueen 2020-08-15
solarspot 2020-07-28
Greatemperor 2020-07-19
冷月醉雪 2020-07-05
manongxiaomei 2020-07-05
javamagicsun 2020-07-04
june0 2020-07-04
yogoma 2020-06-25