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