题目原先是来自廖雪峰的,在hashlib的那个章节,作业就是模拟注册登录,我改了改,然后增加了一个可以找回密码

首先,我们创建五个文本文件:login.pyregistered.py,Find_password.py,date.txt,find_password_date.txt(在同一个目录下)
login.py主要的,用来执行
registered.py 注册用
Find_password.py 找回密码
date.txt用来存储用户的帐号和密码
find_password_date.txt用来存储用户输入的帐号和邮箱,
我们先写用户注册用的,打开registered.py
内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#! /uer/bin/python3.5
# -*- coding: utf-8 -*-
import hashlib,json,re,getpass
def get_md5(user,password):
md5 = hashlib.md5()
md5.update((user+password+"The*Sale").encode('utf-8'))
return md5.hexdigest()
mail_gesi = ["qq.com","gmail.com","163.com"]
def register():
with open("date.txt",'r') as f:
user_date = json.load(f)
with open("find_password_date.txt",'r') as a:
find_password = json.load(a)
while True:
mail_list = [x for x in find_password.values()] # 获取之前用户输入过的所有邮箱
user = str(input("New User: "))
if user == 'q':
break
password = getpass.getpass("Password: ")
password_chack = [i for i in password if i.isalpha()] # 判断 password 中是否有英文字母
mail = str(input("Mail: "))
save_md5 = get_md5(user,password) # 将用户输入的密码加密
mail_split = mail.split("@")
mail_re = re.findall(r"[^a-z0-9]+",mail_split[0])
if user in user_date:
print("'%s' 已存在,请重新输入!" % user)
continue
elif len(password) < 6 or password_chack == []: # 判断密码是否大于6个和是否带有英文字母
print("密码太弱,请输入6位以上的并且至少有一个英文字母")
elif mail in mail_list: # 判断有没有被其他用户输入过
print("此邮箱已注册!")
elif mail_re != [] or mail_split[-1] not in mail_gesi: # 判断邮箱格式
print("请输入正确的邮箱")
print("输入'q'可退出注册")
else:
user_date[user]=save_md5
find_password[user]=mail
print("'%s' 创建成功!" % user)
with open("date.txt",'w') as s:
json.dump(user_date,s)
with open("find_password_date.txt",'w') as x:
json.dump(find_password,x)
break

Find_password.py的内容是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
import json,getpass
from registered import *
def Find_password():
with open("find_password_date.txt",'r') as f:
mail_date = json.load(f)
with open("date.txt",'r') as d:
user_date = json.load(d)
while True:
user = str(input("User: "))
mail = str(input("Mail: "))
if user in mail_date and mail == mail_date[user]:
while True:
new_password = getpass.getpass("New Password")
enter_password = getpass.getpass("Enter Password: ")
if new_password == enter_password:
get_new_md5 = get_md5(user,new_password)
user_date[user]=get_new_md5
print("密码修改成功!")
with open("date.txt",'w') as x:
json.dump(user_date,x)
break
else:
print("两次输入的密码不相同,请重新输入!")
break
else:
print("用户名或错误!")

最后是登录用的login.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- codint: utf-8 -*-
import json,getpass
from registered import *
from Find_password import *

while True:
print("User Login!")
with open("date.txt","r") as f:
user_date = json.load(f)
user = str(input("User Name: "))
password = getpass.getpass("Password: ")
save_md5 = get_md5(user,password)
if user not in user_date:
print("用户名: '%s' 不存在,请注册!" % user)
print("输入'q'退出,输入'a'继续注册")
sele = str(input("q or a: "))
if sele == "q":
print("已退出!")
break
print("REGISTERED!")
register()
else:
if user in user_date and user_date[user]==save_md5:
print("Login success!")
print("Welcome %s" % user)
break
else:
print("密码或帐号错误,是否需要找回密码?(y/n)")
enter = str(input(">>> "))
if enter == "Y" or enter == "y":
Find_password()
else:
continue

现在就直接运行login.py,是会报错的,因为我们在注册的时候,json会去读取本地文件data.txtfind_password_data.txt,因为刚开始这两个文件里面是还没有内容的,你得先存入一个数据才行,否则会报错!
所以,我们先在data.txt里面写入:

{“root”: “63a9f0ea7bb98050796b649e85481845”}

同样的,在find_password_data.txt写入:

“root”: “[email protected]“}

下面是运行结果:

Login.png

不想把他分成三个文件的话,就全部放在一个.py吧,我把他分开储存是因为,要更改或添加的时候方便~
下面是三个.py文件的总和:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /uer/bin/python3.5
# -*- coding: utf-8 -*-
import hashlib,json,re,getpass
def get_md5(user,password):
md5 = hashlib.md5()
md5.update((user+password+"The*Sale").encode('utf-8'))
return md5.hexdigest()
mail_gesi = ["qq.com","gmail.com","163.com"]
def register():
with open("date.txt",'r') as f:
user_date = json.load(f)
with open("find_password_date.txt",'r') as a:
find_password = json.load(a)
while True:
mail_list = [x for x in find_password.values()] # 获取之前用户输入过的所有邮箱
user = str(input("New User: "))
if user == 'q':
break
password = getpass.getpass("Password: ")
password_chack = [i for i in password if i.isalpha()] # 判断 password 中是否有英文字母
mail = str(input("Mail: "))
save_md5 = get_md5(user,password) # 将用户输入的密码加密
mail_split = mail.split("@")
mail_re = re.findall(r"[^a-z0-9]+",mail_split[0])
if user in user_date:
print("'%s' 已存在,请重新输入!" % user)
continue
elif len(password) < 6 or password_chack == []: # 判断密码是否大于6个和是否带有英文字母
print("密码太弱,请输入6位以上的并且至少有一个英文字母")
elif mail in mail_list: # 判断有没有被其他用户输入过
print("此邮箱已注册!")
elif mail_re != [] or mail_split[-1] not in mail_gesi: # 判断邮箱格式
print("请输入正确的邮箱")
print("输入'q'可退出注册")
else:
user_date[user]=save_md5
find_password[user]=mail
print("'%s' 创建成功!" % user)
with open("date.txt",'w') as s:
json.dump(user_date,s)
with open("find_password_date.txt",'w') as x:
json.dump(find_password,x)
break
def Find_password():
with open("find_password_date.txt",'r') as f:
mail_date = json.load(f)
with open("date.txt",'r') as d:
user_date = json.load(d)
while True:
user = str(input("User: "))
mail = str(input("Mail: "))
if user in mail_date and mail == mail_date[user]:
while True:
new_password = getpass.getpass("New Password")
enter_password = getpass.getpass("Enter Password: ")
if new_password == enter_password:
get_new_md5 = get_md5(user,new_password)
user_date[user]=get_new_md5
print("密码修改成功!")
with open("date.txt",'w') as x:
json.dump(user_date,x)
break
else:
print("两次输入的密码不相同,请重新输入!")
break
else:
print("用户名或错误!")
while True:
print("User Login!")
with open("date.txt","r") as f:
user_date = json.load(f)
user = str(input("User Name: "))
password = getpass.getpass("Password: ")
save_md5 = get_md5(user,password)
if user not in user_date:
print("用户名: '%s' 不存在,请注册!" % user)
print("输入'q'退出,输入'a'继续注册")
sele = str(input("q or a: "))
if sele == "q":
print("已退出!")
break
print("REGISTERED!")
register()
else:
if user in user_date and user_date[user]==save_md5:
print("Login success!")
print("Welcome %s" % user)
break
else:
print("密码或帐号错误,是否需要找回密码?(y/n)")
enter = str(input(">>> "))
if enter == "Y" or enter == "y":
Find_password()
else:
continue

结果是一样的