微信公众号服务器配置Python+Tornado后端

先看博主配置完的效果
微信公众号服务器配置Python+Tornado后端_第1张图片

直接看代码

import tornado.web
import hashlib

class TBWeChatBasisConfigHandler(tornado.web.RequestHandler):
    

    def __init__(self, application, request, **kwargs):
        super(TBWeChatBasisConfigHandler, self).__init__(application, request, **kwargs)
        self.WECHAT_TOKEN = "你在微信后台配置的令牌Token"
        

    def get(self):
        signature = self.get_argument("signature")
        timestamp = self.get_argument("timestamp")
        nonce = self.get_argument("nonce")
        echostr = self.get_argument("echostr")
        if signature is None or len(signature) <= 0 \
            or timestamp is None or len(timestamp) <= 0 \
            or nonce is None or len(nonce) <= 0 \
            or echostr is None or len(echostr) <= 0:
            self.respond_empty()
            return

        tmp_arr = [self.WECHAT_TOKEN, timestamp, nonce]
        tmp_arr.sort()
        tmp_str = "".join(tmp_arr)
        real_signature = self.sha1(tmp_str)
        if signature == real_signature:
            self.write(echostr)
        else:
            self.write("error")
        self.finish()


    def respond_empty(self):
        self.write("Invalid Parameters")
        self.finish()
        
        
    def sha1(self, unencrypted_str):
        return hashlib.sha1(unencrypted_str.encode("utf-8")).hexdigest()

官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432

如果有疑问,可以留言,或加微信:Victor_Queue

微信公众号服务器配置Python+Tornado后端_第2张图片

你可能感兴趣的:(微信公众号,微信JSSDK,linux+server,nginx,微信,公众号,服务器配置)