微信文档里面只有个php例子,本人基于tornado写了个python例子:

#!/usr/bin/env python
# encoding:utf-8
import hashlib
from common.BaseHandler import MyHandler


class WeixinInterface(MyHandler):
    """
    微信公众平台服务器配置,验证接口
    """

    def get(self):
        signature = self.get_argument('signature', '')
        timestamp = self.get_argument('timestamp', '')
        nonce = self.get_argument('nonce', '')
        echostr = self.get_argument('echostr', '')
        # 自定义token,只要跟服务器设置那里,填写的一样就可以,不是指的微信的access_token
        wx_access_token = "sdasdas"
        list = [wx_access_token, timestamp, nonce]
        list.sort()
        list2 = ''.join(list)
        sha1 = hashlib.sha1()
        sha1.update(list2.encode('utf-8'))
        hashcode = sha1.hexdigest()
        if hashcode == signature:
            self.write(echostr)  # 返回echostr
            self.finish()


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐