#__author: Administrator
#date: 2018/5/21
login_status_jingdong = False
login_status_weixin = False
def start():
list={1:'home',2:'finance',3:'book'}
list1={1:home,2:finance,3:book}
for i in list:
print(i,list[i])
select_num =input('请输入访问网页的编号[退出:q]>>')
if select_num=='q':
return exit()
elif select_num.isdigit():
key=int(select_num)
list1[key]()
else:
print('请输入有效值!')
def login(auth_type=''):
def outter(a):
def inner():
global login_status_jingdong#获取全局变量来实现该变量的赋值修改
global login_status_weixin
if auth_type=='jingdong':
if login_status_jingdong is False:
with open('京东账号', 'r', encoding='utf8') as f1: # 打开存有用户和密码的文件只读模式
d1 = {}
for i in f1.readlines():
[key, valus] = i.split('=') # 通过分隔符将字符串转为列表,并赋值给变量key和valus
d1[key] = valus.strip() # 向字典中添加元素,这步就实现了字符串转为列表的功能
user = input('请输入用户名>>'.strip())
password = input('请输入密码>>'.strip())
if user == d1['user'] and password == d1['password']:
login_status_jingdong= True
a()
else:
print('账号或密码错误')
f1.close()
else:
a()
elif auth_type=='weixin':
if login_status_weixin is False:
with open('微信账号', 'r', encoding='utf8') as f2: # 打开存有用户和密码的文件只读模式
d2 = {}
for i in f2.readlines():
[key, valus] = i.split('=') # 通过分隔符将字符串转为列表,并赋值给变量key和valus
d2[key] = valus.strip() # 向字典中添加元素,这步就实现了字符串转为列表的功能
user = input('请输入用户名>>'.strip())
password = input('请输入密码>>'.strip())
if user == d2['user'] and password == d2['password']:
login_status_weixin = True
a()
else:
print('Sorry')
f2.close()
else:
a()
return inner
return outter
@login('jingdong')
def home():
print('welcome to home page')
@login('weixin')
def finance():
print('welcome to finance page')
@login('jingdong')
def book():
print('welcome to book page')
while True:
start()
python装饰器之登录练习
最新推荐文章于 2022-04-29 10:54:03 发布