python裝飾器之登入練習(老男孩)

2021-09-12 17:18:55 字數 1841 閱讀 5304

jdlogin_status = false

wxlogin_status = false

key = 0

def logger(auth_type):

def login(f):

global jdlogin_status #獲取全域性變數來實現該變數的賦值修改

global wxlogin_status

if jdlogin_status == false:

username = input("username:").strip()

passwd = input("passwd:").strip()

a = open(auth_type, 'r', encoding='utf8') #這裡的auth_type 也是呼叫的檔案的檔名

at = a.read()

a.close()

dic = eval(at)

if username in dic and passwd == dic[username]:#判斷使用者名稱和密碼是否在檔案之中

f()jdlogin_status =true

else:

print('賬號密碼錯誤')

elif wxlogin_status == false:

username = input("username:").strip()

passwd = input("passwd:").strip()

a = open(auth_type, 'r', encoding='utf8')

at = a.read()

a.close()

dic = eval(at)

if username in dic and passwd == dic[username]:

f()wxlogin_status = true

else:

print('賬號密碼錯誤')

elif jdlogin_status == true:

f()elif wxlogin_status == true:

f()else:

pass

return login

def judge(key):

if key == 1:

@logger("jingdong")

def home():

print('welcome to home\n')

elif key == 2:

@logger("weixin")

def finance():

print('welcome to finance\n')

elif key == 3:

@logger("jingdong")

def book():

print('welcome to book\n')

def start():

global key

list =

for i in list:

print(i, list[i])

select_num = input('請輸入訪問網頁的編號[退出:q]>>')

if select_num == 'q':

exit()

elif select_num.isdigit() and select_num in list:

key = int(select_num)

else:

print('請輸入有效值!')

return

judge(key)

while 1:

start()

檔案 jingdong 為

檔案 weixin 為

Python之裝飾器(綜合練習)

裝飾器 案例1 建立裝飾器,要求如下 1.建立add log裝飾器,被裝飾的函式列印日誌資訊 import time import functools 定義裝飾器 def add log fun 保留被裝飾函式的函式名和幫助資訊文件 functools.wraps fun def inter arg...

py之裝飾器練習(使用者登入)

root admin redhat id id vip 多個裝飾器的應用場景 會採用多個裝飾器先驗證是否登陸成功 再驗證許可權是否足夠 import functools import inspect def is login fun functools.wraps fun if args 0 in ...

Python之Flask登入認證 裝飾器

from flask import flask,render template,request,response,redirect,session,url for usrs 2 3 4 redirect to 重定向到另乙個頁面 subdomain admin 子網域名稱 請求相關的全部在reque...