37,719
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/env python
# -*- 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
print("signature=%s timestanmp=%s nonce=%s" %(signature, timestamp, nonce))
token = "wxtoken"
# 字典序排序
list = [token, timestamp, nonce]
list.sort()
sha1 = hashlib.sha1()
map(sha1.update, list)
hashcode = sha1.hexdigest()
print("hashcode=%s signature=%s" %(hashcode, signature))
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):
print("get ok")
self.write(echostr)
else:
self.write('fail')
print("get fa
il")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(80)
tornado.ioloop.IOLoop.instance().start()