python註解(裝飾器)的用法

帶參數的要用三層def,第一層寫註解裏的參數,第二層寫函數,第三層寫具體邏輯。cookie

若是是不帶參數的註解,直接使用後兩層便可session

def needPermission(permissionstr):
    def decorator(func):
        def hasPermission(self,*args, **kw):
            sessionid = self.get_cookie("session_id")
            verification = self.get_cookie("verification")
            # print(sessionid)
            # print(verification)
            islogin, content = AuthSdk().isLogin(sessionid, verification)
            permissionList = []
            if islogin:
                permissionList = content["permissions"]
            else:
                self.set_status(401)
                self.write(R().errorCodeMsg(401, "認證不經過,請確認是否登陸"))
                return
            # print('%s %s %s():' % (self, permissionstr, func.__name__))
            if permissionstr not in permissionList:
                self.set_status(403)
                self.write(R().errorCodeMsg(403, "沒有訪問權限"))
                return
            else:
                return func(self, *args, **kw)
        return hasPermission
    return decorator
相關文章
相關標籤/搜索