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('请输入用户名:').strip()
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):#装饰器,登录成功直接访问,没有就登录
def inner(*args,**kwargs):
if status_dic['status']:
ret = func(*args,**kwargs)
return ret
else:
print('请先进行登录')
if login():
ret = func(*args,**kwargs)
return ret
return inner
def log_record(func):#用户登录前,记录日志
def inner(*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 inner
@wrapper
@log_record
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]()
else:
print('输入的超出范围,请重新输入:')
else:
print('您输入的选项有非法字符,请重新输入:')