import time
status_dic = {
'username': None,
'status': False,
}
flag1 = True
def login(*args,**kwargs):
i = 0
while i < 3:
if args:
status_dic['username'] = args[0]
status_dic['status'] = True
return True
else:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
with open('register',encoding='utf-8') as f1:
for line in f1:
line_list = line.strip().split()
if username == line_list[0] and password == line_list[1]:
print('登录成功')
status_dic['username'] = username
status_dic['status'] = True
return True
else:
print('输入不正确,请重新输入,还剩%s机会' % (2-i))
if i == 2: return Quit()
i += 1
def register(*args, **kwargs):
flag = True
while flag:
username = input('请输入要注册的用户名:')
f1 = open('register',encoding='utf-8')
for i in f1:
if username in i:
print('用户名重复,请重新输入')
f1.close()
break
else:
f1.close()
password = input('请输入要注册的密码:').strip()
f2 = open('register', encoding='utf-8', mode='a')
f2.write('
{} {}'.format(username, password))
f2.close()
print('恭喜你,注册成功,已经自动为您登录,现在跳转到首页...')
return login(username,password)
def wrapper(func): #func=inner2
def inner1(*args,**kwargs):
if status_dic['status']:
ret = func(*args,**kwargs)
return ret
else:
print('请先进行登录')
if login():
ret = func(*args, **kwargs)
return ret
return inner1
def log_record(func): #func=article
def inner2(*args,**kwargs):
struct_time = time.localtime()
time_now = time.strftime("%Y-%m-%d %H:%M:%S", struct_time)
with open('log_func','a',encoding='utf-8') as f1:
f1.write('用户:%s 在%s 执行了 %s函数
'%(status_dic['username'],time_now,func.__name__))
ret = func(*args,**kwargs)
return ret
return inner2
@wrapper # article = wrapper(article) artical=wrapper(inner2) 外面的artical=inner1
@log_record # artical = log_record(article) 外面的artical=inner2
def article():
print('欢迎%s访问文章页面' % status_dic['username'])
@wrapper
@log_record
def diary():
print('欢迎%s访问日记页面' % status_dic['username'])
@wrapper
@log_record
def comment():
print('欢迎%s访问评论页面' % status_dic['username'])
@wrapper
@log_record
def enshrine():
print('欢迎%s访问收藏页面' % status_dic['username'])
def login_out():
status_dic['username'] = None
status_dic['status'] = False
print('注销成功')
def Quit():
global flag1
flag1 = False
return flag1
choice_dict = {
1: login,
2: register,
3: article,
4: diary,
5: comment,
6: enshrine,
7: login_out,
8: Quit,
}
while flag1:
print('欢迎来到博客园首页
1:请登录
2:请注册
3:文章页面
4:日记页面
5:评论页面
6:收藏页面
7:注销
8:退出程序')
choice = input('请输入您选择的序号:').strip()
if choice.isdigit():
choice = int(choice)
if 0 < choice <= len(choice_dict):
choice_dict[choice]() #artical() inner1() login() register()
else:
print('您输入的超出范围,请重新输入')
else:
print('您输入的选项有非法字符,请重新输入。')