python 驗證功能的裝飾器示例

2022-03-31 02:34:58 字數 2492 閱讀 4044

user_list=[,,

]current_dict=

defauth(auth_type):

defauth_func(func):

print("

認證型別

", auth_type)

if auth_type == '

filedb':

if current_dict['

username

'] and current_dict['

login

']:#

判斷使用者的登陸狀態

res = func(*args, **kwargs)

return

res username=input("

使用者名稱").strip()#

strip()函式去除字串兩邊的空格

password=input('密碼'

).strip()

for item in

user_list:

if username==item['

name

'] and password==item['

pwd'

]: current_dict[

'username

']=username#

改變使用者的登陸狀態,改變後就不需要每次都進行輸入

current_dict['

login

']=true

res=func(*args,**kwargs)

return

res

else

:

print("

使用者名稱或密碼錯誤")

elif auth_type=="

ldap":

print("

hahhaha")

return

return

auth_func

@auth(auth_type="

filedb

")#auth_func=auth(auth_type="filedb")--->@auth_func 附加了乙個auth_type---->index=auth_func(index)

defindex():

print("

歡迎來到京東")

@auth(auth_type=''

)def

home(name):

print("

我的個人中心%s

"%(name))

@auth(auth_type='

filedb')

defshop_car(name):

print("

%s我的購物車

"%(name))

print("

after---->

",current_dict)

index()

print('

after---->

',current_dict)

home(

"alex")

shop_car(

"產品經理")

#dict_user=##

def auth_func(func):##

if dict_user['username'] and dict_user['login']:

#res = func(*args, **kwargs)

#return res

#username=input("使用者名稱").strip()#strip()函式去除字串兩邊的空格

#password=input('密碼').strip()

#if username=="sb" and password=="123":

#dict_user['username']=username

#dict_user['login']=true

#res=func(*args,**kwargs)

#return res

#else:

#print("使用者名稱或密碼錯誤")##

####

###@auth_func

#def index():

#print("歡迎來到京東")##

@auth_func

#def home(name):

#print("我的個人中心%s"%(name))##

@auth_func

#def shop_car(name):

#print("%s我的購物車"%(name))##

index()

#home("alex")

#shop_car("產品經理")##

#a="123"

#b='1'

#v=b.join(a)##b join a

#print(v)

Python 帶引數的使用者驗證功能裝飾器

user list 初始狀態,用來儲存登陸的使用者,client dic 新增新功能 def auth auth type filedb def auth func func print auth type if auth type fildb 引數檢查,判斷是否有使用者登入,如果有,不用驗證,直接...

Python 裝飾器模擬使用者登陸驗證功能

user list 初始狀態,用來儲存登陸的使用者,client dic 新增新功能 def auth func func 引數檢查,判斷是否有使用者登入,如果有,不用驗證,直接執行函式的功能 if client dic username and client dic login res func ...

Python 裝飾器模擬使用者登陸驗證功能

def auth func func 引數檢查,判斷是否有使用者登入,如果有,不用驗證,直接執行函式的功能 if client dic username and client dic login res func args,kwargs return res 輸入使用者名稱和密碼 username ...