| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import tornado.web import hashlib class WechatHandler(tornado.web.RequestHandler):     def check(self):         #微信公众平台设置         token = "renjie"         #微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。         signature = self.get_argument("signature", None)         #时间戳         timestamp = self.get_argument("timestamp", None)         #随机数         nonce = self.get_argument("nonce", None)         #随机字符串         echostr = self.get_argument("echostr", None)         if signature and timestamp and nonce:             #将token、timestamp、nonce三个参数进行字典序排序             param = [token, timestamp, nonce]             param.sort()             #将三个参数字符串拼接成一个字符串进行sha1加密             sha = hashlib.sha1("%s%s%s" % tuple(param)).hexdigest()             #开发者获得加密后的字符串可与signature对比,标识该请求来源于微信             if sha == signature:                 if echostr:                     #请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。                     return echostr                 else:                     return True         return False     def get(self):         self.write(str(self.check())) | 
