自己的版本:
''' 1),启动程序,首页面应该显示成如下格式: 欢迎来到博客园首页 1:请登录 2:请注册 3:文章页面 4:日记页面 5:评论页面 6:收藏页面 7:注销 8:退出程序 2),用户输入选项,3~6选项必须在用户登录成功之后,才能访问成功。 3),用户选择登录,用户名密码从register文件中读取验证,三次机会, 没成功则结束整个程序运行,成功之后,可以选择访问3~6项,访问页面之前, 必须要在log文件中打印日志,日志格式为-->用户:xx 在xx年xx月xx日 执行了 %s函数, 访问页面时,页面内容为:欢迎xx用户访问评论(文章,日记,收藏)页面 4),如果用户没有注册,则可以选择注册,注册成功之后,可以自动完成登录,然后进入首页选择。 5),注销用户是指注销用户的登录状态,使其在访问任何页面时,必须重新登录。''' import time hook = False user = '' password = '' def register(): global user,hook flag = True while flag: print('注册中。。。请完成以下流程') user01 = input("请输入用户名:").strip() password01 = input("请输入密码:").strip() password02 = input("请再次输入密码:").strip() if user01 == '' or password01 == '' or password02 == '': print("输入用户名或密码不能为空") continue if password02 != password01: print('两次密码不匹配') continue with open('register',encoding='utf-8',mode='r') as f1: for s in f1: if user01 in s: print('用户已存在,请登陆') flag = False break if flag: with open('register',encoding='utf-8',mode='a+') as f2: # print(user,password01) print(user01,password01,file=f2, flush=True) print('注册成功,自动登陆成功') user = user01 hook = True flag = False def login(): global hook,user flag = True fail_count = 0 if hook == True: print('已经登陆,请勿再登陆') return 0 while flag: print('登陆中。。。请完成以下流程') user02 = input("请输入用户名:").strip() password03 = input("请输入密码:") if user02 == '' or password03 == '': print("输入用户名或密码不能为空,请重新输入") continue # 验证输入的用户名时否在黑名单里 with open('black_list', encoding='utf-8', mode='r') as f3: for s in f3: if user02 in s: print('该用户处于黑名单') flag = False break if flag: with open('register', encoding='utf-8', mode='r') as f4: for s in f4: l1 = s.split() # print(l1) if user02 == l1[0] and password03 == l1[-1]: print('登陆成功') user = user02 flag = False hook = True break elif user02 == l1[0] and password03 != l1[-1]: fail_count += 1 break else: #Python 小技巧:for 循环后面接 else 语句,在for循环完整完成后才执行else;如果中途从break跳出,则连else一起跳出 print("该用户名未注册...") y = input("是否注册(yes/no):").strip() if y == "yes" or y =="y": return register() elif y == "no" or y == "n": return home_main() else: print("输入错误...") if flag and fail_count == 3: with open('black_list', encoding='utf-8', mode='a') as f5: f5.write(user02 + '\n') print('错误超出了3次,该用户已锁定') flag = False elif flag: print('账户密码错误,请重新输入') def auth(f): def inner(*args,**kwargs): while 1: if hook: ret = f(*args,**kwargs) with open('log', encoding='utf-8', mode='a+') as f6: li = time.strftime("%Y-%m-%d %H:%M:%S") print('用户:%s在%s执行了%s函数' %(user,li,f.__name__), file=f6, flush=True) if ret == 0: break else: login() return inner @auth def artcle(*args,**kwargs): print('欢迎%s用户访问文章页面' %user) return 0 @auth def dairy(*args,**kwargs): print('欢迎%s用户访问日记页面' %user) return 0 @auth def comment(*args,**kwargs): print('欢迎%s用户访问评论页面' %user) return 0 @auth def collect(*args,**kwargs): print('欢迎%s用户访问收藏页面' %user) return 0 def loginout(): global hook if hook: hook = False print('注销成功') else: print('已经处于注销状态') def home_main(): msg = ''' 欢迎来到博客园首页 1:请登录 2:请注册 3:文章页面 4:日记页面 5:评论页面 6:收藏页面 7:注销 8:退出程序 请输入序号>>>:''' func_list = ['', login, register, artcle, dairy, comment, collect, loginout, exit] while True: choice = input(msg).strip() if choice.isdigit(): choice = int(choice) if 0 < choice <= 8: func_list[choice]() else: print('请输入正确的操作码!') else: print('请输入正确的操作码!') if __name__ == '__main__': home_main()
引用借鉴的版本
import time status_dic = { 'username': None, #设置一个登录用户 'status': False, #用于后面验证是否登录,登录状态 } def login(*args): #登录模块 with open("login.txt", encoding="utf-8", mode="r") as f: login_list = f.readlines() if args: #判断用户是否登录 status_dic['username'] = args[0] status_dic['status'] = True return True else: count = 0 while count < 3: if status_dic['status'] == False: username = input("请输入登录用户名:").strip() for i in login_list: if username == eval(i)["name"]: #对用户名进行检查,看是否存在,不存在直接走for循环的else判断,让用户进行注册 password = input("请登录用户密码:").strip() if password == eval(i)["password"]: #判断密码输入是否正确 print("login successfully") status_dic['username'] = username status_dic['status'] = True return True else: print("您还有%s次机会" % int(2 - count)) #3次机会验证,都错误会返回登录首页 count += 1 break else: print("检查用户名未注册...") y = input("是否注册(yes/no):").strip() if y == "yes" or y =="y": return register() elif y == "no" or y == "n": return home_page() else: print("输入错误...") else: print("错误太多次啦...") def register(): #注册模块 tag = True f1 = open("login.txt", encoding="utf-8", mode="r") login_list = f1.readlines() print("账号注册".center(50,"#")) while tag: username = input("请输入您注册的用户名:").strip() for i in login_list: if username == eval(i)["name"]: print("用户名已经存在,请重新输入") f1.close() break else: password = input("请输入您注册的密码:").strip() f2 = open("login.txt",encoding="utf-8",mode="a") f2.write("{}\n".format({"name": username,"password": password})) tag = False print("注册成功") f2.close() return login(username, password) def wrapper(func): #登录验证的装饰器 def inner1(): if status_dic['status']: ret = func() return ret else: print("请先进行登录") if login(): ret = func() return ret return inner1 def log(func): #记录日志的装饰器 def inner2(): localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) f = open("access.log",encoding="utf-8",mode="a+") f.write("用户:{},在{}执行了{}函数\n".format(status_dic['username'],localtime,func.__name__)) f.close() ret = func() return ret return inner2 @wrapper #article_page = wrapper(article_page) --> article_page=wrapper(inner2) @log #article_page = log(article_page) article_page = inner2 def article_page(): print("欢迎%s来到文章页面" % status_dic['username']) @wrapper #diary_page = wrapper(diary_page) @log def diary_page(): print("欢迎%s来到日记页面" % status_dic['username']) @wrapper @log def comment_page(): print("欢迎%s来到评论页面" % status_dic['username']) @wrapper @log def collect_page(): print("欢迎%s来到收藏页面" % status_dic['username']) def log_out(): print("%s用户已注销" % status_dic['username']) status_dic['username'] = None status_dic['status'] = False def Quit_exit(): print("退出程序...") return exit() dic = { 1: login, 2: register, 3: article_page, 4: diary_page, 5: comment_page, 6: collect_page, 7: log_out, 8: Quit_exit } def home_page(): while True: str = '''##########欢迎来到博客园首页########### 1: {} 2: {} 3: {} 4: {} 5: {} 6: {} 7: {} 8: {} '''.format('请登录', '请注册', '文章页面', '日记页面', '评论页面', '收藏页面', '注销', '退出程序') print(str) bianhao = input("请输入选择的序列号:").strip() if bianhao.isdigit(): bianhao = int(bianhao) if 0 < bianhao <= 8: dic[bianhao]() else: print("输入编号不在范围内") else: print("输入的序列号只能是数字") if __name__ == '__main__': home_page()