程序要求:

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 分),然后进入首页选择。
5),注销用户是指注销用户的登录状态,使其在访问任何页面时,必须重新登录。
6),退出程序为结束整个程序运行。

 



1 import time
2
3
4 status_dic = {
5 'username': None, #设置一个登录用户
6 'status': False, #用于后面验证是否登录,登录状态
7 }
8
9
10
11 def login(*args): #登录模块
12 with open("login.txt", encoding="utf-8", mode="r") as f:
13 login_list = f.readlines()
14 if args: #判断用户是否登录
15 status_dic['username'] = args[0]
16 status_dic['status'] = True
17 return True
18 else:
19 count = 0
20 while count < 3:
21 if status_dic['status'] == False:
22 username = input("请输入登录用户名:").strip()
23 for i in login_list:
24 if username == eval(i)["name"]: #对用户名进行检查,看是否存在,不存在直接走for循环的else判断,让用户进行注册
25
26 password = input("请登录用户密码:").strip()
27 if password == eval(i)["password"]: #判断密码输入是否正确
28
29 print("login successfully")
30 status_dic['username'] = username
31 status_dic['status'] = True
32 return True
33 else:
34 print("您还有%s次机会" % int(2 - count)) #3次机会验证,都错误会返回登录首页
35 count += 1
36 break
37
38 else:
39 print("检查用户名未注册...")
40 y = input("是否注册(yes/no):").strip()
41 if y == "yes" or y =="y":
42 return register()
43
44 elif y == "no" or y == "n":
45 return home_page()
46 else:
47 print("输入错误...")
48 else:
49 print("错误太多次啦...")
50
51
52 def register(): #注册模块
53 tag = True
54 f1 = open("login.txt", encoding="utf-8", mode="r")
55 login_list = f1.readlines()
56 print("账号注册".center(50,"#"))
57 while tag:
58 username = input("请输入您注册的用户名:").strip()
59 for i in login_list:
60 if username == eval(i)["name"]:
61 print("用户名已经存在,请重新输入")
62 f1.close()
63 break
64 else:
65 password = input("请输入您注册的密码:").strip()
66 f2 = open("login.txt",encoding="utf-8",mode="a")
67 f2.write("{}\n".format({"name": username,"password": password}))
68 tag = False
69 print("注册成功")
70 f2.close()
71 return login(username, password)
72
73
74 def wrapper(func): #登录验证的装饰器
75 def inner1():
76 if status_dic['status']:
77 ret = func()
78 return ret
79 else:
80 print("请先进行登录")
81 if login():
82 ret = func()
83 return ret
84 return inner1
85
86
87 def log(func): #记录日志的装饰器
88 def inner2():
89 localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
90 f = open("access.log",encoding="utf-8",mode="a+")
91 f.write("用户:{},在{}执行了{}函数\n".format(status_dic['username'],localtime,func.__name__))
92 f.close()
93 ret = func()
94 return ret
95 return inner2
96
97
98 @wrapper #article_page = wrapper(article_page) --> article_page=wrapper(inner2)
99 @log #article_page = log(article_page) article_page = inner2
100 def article_page():
101 print("欢迎%s来到文章页面" % status_dic['username'])
102
103 @wrapper #diary_page = wrapper(diary_page)
104 @log
105 def diary_page():
106 print("欢迎%s来到日记页面" % status_dic['username'])
107
108
109 @wrapper
110 @log
111 def comment_page():
112 print("欢迎%s来到评论页面" % status_dic['username'])
113
114 @wrapper
115 @log
116 def collect_page():
117 print("欢迎%s来到收藏页面" % status_dic['username'])
118
119
120 def log_out():
121 print("%s用户已注销" % status_dic['username'])
122 status_dic['username'] = None
123 status_dic['status'] = False
124
125
126 def Quit_exit():
127 print("退出程序...")
128 return exit()
129
130 dic = {
131 1: login,
132 2: register,
133 3: article_page,
134 4: diary_page,
135 5: comment_page,
136 6: collect_page,
137 7: log_out,
138 8: Quit_exit
139 }
140
141
142
143 def home_page():
144 while True:
145 str = '''##########欢迎来到博客园首页###########
146 1: {}
147 2: {}
148 3: {}
149 4: {}
150 5: {}
151 6: {}
152 7: {}
153 8: {}
154 '''.format('请登录', '请注册', '文章页面', '日记页面', '评论页面', '收藏页面', '注销', '退出程序')
155 print(str)
156 bianhao = input("请输入选择的序列号:").strip()
157 if bianhao.isdigit():
158 bianhao = int(bianhao)
159 if 0 < bianhao <= 8:
160 dic[bianhao]()
161 else:
162 print("输入编号不在范围内")
163 else:
164 print("输入的序列号只能是数字")
165
166 if __name__ == '__main__':
167 home_page()