python3----练习题(....)

 1 import re
 2 import hashlib
 3 import json
 4 
 5 def get_md5(pwd):     # md5加密
 6     m = hashlib.md5()
 7     m.update(pwd.encode('utf-8'))
 8     return m.hexdigest()
 9 
10 def get_name_md5():    # ‘加盐’加密
11     hash = hashlib.md5('python'.encode('utf-8'))
12     hash.update('admin'.encode('utf-8'))
13     return hash.hexdigest()
14 
15 def write_to_file(content):
16     with open('dic.txt', 'a', encoding='utf-8') as f:
17         f.write(json.dumps(content, ensure_ascii=False) + '
')
18         f.close()
19 
20 
21 def regist():
22     dd = {}
23     print('{0:*^50}'.format('用户注册界面'))
24     name = input("请输入你的用户名(以字母开头):")
25     if re.match(r'^[a-zA-Z].{2,9}', name):
26         pwd = input("请设置密码:")
27         pwd = get_md5(pwd)
28         dd[name] = pwd
29         write_to_file(dd)
30         print(dd)
31     else:
32         print("您输入的用户名格式不正确,请小于10位")
33         regist()
34 
35 
36 def login():
37     print('{0:*^50}'.format('用户登录界面'))
38     name = input('帐号:')
39     with open('dic.txt') as f:
40         for line in iter(f):
41             dic1 = json.loads(line)
42             if name in dic1:
43                 pwd = input('密码:')
44                 pwd = get_md5(pwd)
45                 if dic1[name] == pwd:
46                     print('登录成功')
47                     return True
48         print("帐号或密码错误!")
49         login()
50 
51 def main():
52      login()
53 
54 if __name__ == '__main__':
55     main()
原文地址:https://www.cnblogs.com/jonm/p/8322571.html