简单的用户登录(文件处理)

 1 import hashlib
 2 #用户注册
 3 def regiest(user,psd):
 4     board = ['中山', '小平', '','政府']
 5     for i in board:  #判断用户名
 6         if i in user: #是否注册涉及敏感字
 7             user=user.replace(user,'*'*len(i))
 8             ret='用户名{}涉及敏感词,请重新填写!'.format(user)
 9             break      #如果确定用户名为敏感字符则直接终止
10 
11     else:        #判断密码
12         if len(psd)==0: #判断密码是否为空
13             ret='error,用户名或密码不能为空'
14         else:
15             # 密码sha256加密
16             def haxi(pwd):
17                 hash= hashlib.sha256()
18                 hash.update(pwd.encode('utf-8'))
19                 m_psd= hash.hexdigest()
20                 return m_psd
21             ha_psd=haxi(psd)      #对密码进行加密操作
22             ret=info_write(user,ha_psd)    #录入用户数据到文件
23     return ret
24 
25 #用户信息写入文件内
26 def info_write(user,psd):
27     with open('abc.txt','a+',encoding='utf-8')as f,open('abc.txt','r+',encoding='utf-8')as f1:
28         if user in f1.read():
29             msg='用户名已被注册,请更换!'
30         else:
31             f.write('{},{}\n'.format(user,psd))
32             msg='Register successful!'
33     return msg
34 
35 flg=True
36 while flg:
37     username=input('请输入用户名—或者按Q退出:').strip()
38     if username.upper() == 'Q': break     #按Q退出
39     password=input('请输入密码:').strip()
40     message= regiest(username,password)
41     print(message)
用户注册

 

 1 import hashlib
 2 #用户注册
 3 def regiest(user,psd):
 4     board = ['中山', '小平', '','政府']
 5     for i in board:  #判断用户名
 6         if i in user: #是否注册涉及敏感字
 7             user=user.replace(user,'*'*len(i))
 8             ret='用户名{}涉及敏感词,请重新填写!'.format(user)
 9             break      #如果确定用户名为敏感字符则直接终止
10 
11     else:        #判断密码
12         if len(psd)==0: #判断密码是否为空
13             ret='error,用户名或密码不能为空'
14         else:
15             ha_psd=haxi(psd)      #调用加密算法
16             ret=info_write(user,ha_psd)    #录入用户数据到文件
17     return ret
18 
19 log=True
20 #用户登录
21 def login(user,psd):
22     while log:
23         if len(user)==0 or len(psd)==0:
24            msg='用户名和密码不能为空!'
25            break
26 
27         else:
28            use_dic=info_read()   #获取后台文件用户信息
29            if user in use_dic and haxi(psd)== use_dic.get(user):
30                msg='用户{}登录成功,欢迎!'.format(user)
31                break
32 
33            if user in use_dic and haxi(psd)!= use_dic.get(user):
34                msg ='密码错误,请输入!'
35                break
36 
37            else:
38                msg='用户名未注册,无效,请注册!'
39                break
40 
41     return msg
42 
43 # 密码sha256加密
44 def haxi(pwd):
45     hash= hashlib.sha256()
46     hash.update(pwd.encode('utf-8'))
47     m_psd= hash.hexdigest()
48     return m_psd
49 
50 #用户信息写入文件内
51 def info_write(user,psd):
52     with open('abc.txt','a+',encoding='utf-8')as f,open('abc.txt','r+',encoding='utf-8')as f1:
53         if user in f1.read():
54             msg='用户名已被注册,请更换!'
55         else:
56             f.write('{},{}\n'.format(user,psd))
57             msg='Register successful!'
58     return msg
59 
60 #用户信息读取操作
61 def info_read():
62     info = {}
63     with open('abc.txt','r+',encoding='utf=8')as f:
64         fr=f.readlines()
65         for i in fr:
66            n_name= i.split(',')[0]         #后台用户名
67            n_psd=i.split(',')[1][0:-1]     #后台密码 要去掉最后的\n
68            info.setdefault(n_name,n_psd)   #把用户名密码加入info字典
69     return info
70 
71 
72 flg=True
73 while flg:
74     selec=input('请输入要进行的操作>>>A【登录】B【注册】').upper()
75     if selec.upper()!='A'and selec.upper()!='B':
76         print('你还是上天和太阳肩并肩吧!')
77         break
78     else:
79         username=input('请输入用户名—或者按Q退出:').strip()
80         if username.upper() == 'Q': break     #按Q退出
81         password=input('请输入密码:').strip()
82         if selec.strip()=='A':
83             state=login(username,password)    #调用登录
84             print(state)
85         else:
86             message= regiest(username,password)  #调用注册
87             print(message)
登录与注册

 

posted @ 2018-07-08 11:58  青红*皂了个白  阅读(184)  评论(0编辑  收藏  举报