python tornado微信开发入门代码
本文实例为大家分享了pythontornado微信开发的具体代码,供大家参考,具体内容如下
#微信入门代码
#!/usr/bin/envpython2.7
#-*-coding:utf-8-*-
importtornado.ioloop
importtornado.web
importhashlib
importxml.etree.ElementTreeasET
importtime
defcheck_signature(signature,timestamp,nonce):
#微信公众平台里输入的token
token="linden"
#字典序排序
list=[token,timestamp,nonce]
list.sort()
sha1=hashlib.sha1()
map(sha1.update,list)
hashcode=sha1.hexdigest()
returnhashcode==signature
classMainHandler(tornado.web.RequestHandler):
defget(self):
signature=self.get_argument('signature')
timestamp=self.get_argument('timestamp')
nonce=self.get_argument('nonce')
echostr=self.get_argument('echostr')
ifcheck_signature(signature,timestamp,nonce):
self.write(echostr)
else:
self.write('fail')
defpost(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="""
%s
%s
"""
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。