鱼C论坛

 找回密码
 立即注册
查看: 1222|回复: 2

求助数据储存优化及代码优化(用户登录模块)

[复制链接]
发表于 2020-7-3 21:07:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
PS:该程序有一默认锁定的用户(即所读取的文件均不为空)
源代码如下:
  1. import easygui as g

  2. def count_users():
  3.     count = len(dict_members)
  4.     return count

  5. def secure_question(read_write):
  6.     if read_write == 'read':
  7.         with open('securequestions.pkl','rb') as read_file:
  8.             (questions,answer) = pickle.load(read_file)
  9.             return (questions,answer)
  10.         
  11.     if read_write == 'write':
  12.         (list_question,list_answer) = secure_question('read')
  13.         msg = '请设置账号密保问题'
  14.         title = '账号安全设置'
  15.         choices = ('密保问题','答案')
  16.         while True:
  17.             (str_question,str_answer) = g.multenterbox(msg,title,choices)
  18.             if len(str_question) and len(str_answer):
  19.                 break
  20.             else:
  21.                 msg = '输入不能为空,请重新输入'
  22.         list_question.append(str_question)
  23.         list_answer.append(str_answer)
  24.         question_data = [list_question,list_answer]
  25.         with open('securequestions.pkl','wb') as write_file:
  26.             pickle.dump(question_data,write_file)
  27.         g.msgbox('密保问题设置成功','安全通知')
  28.         
  29.         
  30.    
  31. def read_user_ID(file_name):
  32.     pickle_file = open(file_name,'rb')
  33.     dict_user = pickle.load(pickle_file)
  34.     pickle_file.close()
  35.     return dict_user

  36. def read_password():
  37.     with open('password.pkl','rb') as passwd_file:
  38.         passwd = pickle.load(passwd_file)
  39.         return passwd

  40. def write_password(password):
  41.     passwd = read_password()
  42.     passwd.append(password)
  43.         
  44.     with open('password.pkl','wb') as passwd_file:
  45.         pickle.dump(passwd,passwd_file)
  46.         
  47.         

  48. def write_file(name,user_ID):
  49.     dict_members[name] = user_ID
  50.     pickle_file = open('data.pkl','wb')
  51.     pickle.dump(dict_members,pickle_file)
  52.     pickle_file.close()


  53. def changepasswd(name):
  54.     (question,answer) = secure_question('read')
  55.     flag = dict_members[name]
  56.     msg = question[flag]
  57.     correct_answer = answer[flag]
  58.     title = '修改密码窗口'
  59.     while True:
  60.         enter_answer = g.enterbox(msg,title)
  61.         if enter_answer == correct_answer:
  62.             msg = '请输入新密码'
  63.             new_password = g.passwordbox(msg,title)
  64.             password_list = read_password()
  65.             password_list[flag] = new_password
  66.             with open('password.pkl','wb') as passwd_file:
  67.                 pickle.dump(password_list,passwd_file)
  68.             break
  69.         else:
  70.             msg = question[flag]
  71.             msg = '输入错误,请重新输入'+'\n'+msg

  72. def find_password():
  73.     msg = '请输入您的用户名'
  74.     title = '找回密码'
  75.     name = g.enterbox(msg,title)
  76.     if name in dict_members:
  77.         changepasswd(name)
  78.     else: g.msgbox('您输入的用户名不存在','提示')
  79.         
  80.    
  81.    
  82. def main_menu():
  83.     title = '主界面'
  84.     choices = ('修改密码','注销','退出')
  85.     while True:
  86.         option = g.buttonbox('',title,choices)
  87.         if option == '修改密码':
  88.             changepasswd(current_user)
  89.         if option == '注销':
  90.             return False
  91.         if option == '退出':
  92.             return True

  93. def frozen_users(read_write,name):
  94.     if read_write == 'read':
  95.         with open('frozen.pkl','rb') as frozend_file:
  96.             frozen_user = pickle.load(frozend_file)
  97.             return frozen_user

  98.     if  read_write == 'write':
  99.         Name = name
  100.         frozen_list = frozen_users('read',Name)
  101.         frozen_list.append(name)
  102.         with open('frozen.pkl','wb') as frozen_file:
  103.             pickle.dump(frozen_list,frozen_file)


  104. def frozen_off(name):
  105.     user_ID = dict_members[name]
  106.     (questions,answer) = secure_question('read')
  107.     msg = questions[user_ID]
  108.     title = '账号安全窗口'
  109.     n = 3
  110.     while n:
  111.         answer_enter = g.enterbox(msg,title)
  112.         if answer_enter == answer[user_ID]:
  113.             frozen_list = frozen_users('read',name)
  114.             frozen_list.remove(name)
  115.             with open('frozen.pkl','wb') as frozen_file:
  116.                 pickle.dump(frozen_list,frozen_file)
  117.             break
  118.         else:
  119.             n -= 1
  120.             g.msgbox('输入错误,你还有%d次机会'%n,title)
  121.             

  122. def SignIn():
  123.     msg = '请输入用户名和密码'
  124.     title = '用户注册窗口'
  125.     user_info = []
  126.     user_info = g.multpasswordbox(msg,title,('用户名','密码'))
  127.     (name,password) = user_info
  128.     while name in dict_members:
  129.         g.msgbox('用户名已存在请重新输入!')
  130.         SignIn()
  131.     else:
  132.         user_ID = count_users()
  133.         dict_members[name] = user_ID
  134.         
  135.         write_password(password)
  136.         write_file(name,user_ID)
  137.         secure_question('write')
  138.         return { name:user_ID }

  139. def login(msg,title):
  140.     while 1:
  141.         user_info = []
  142.         user_info = g.multpasswordbox(msg,title,('用户名','密码'))
  143.         (name,password) = user_info

  144.         if name not in dict_members:
  145.             g.msgbox('用户名不存在请重新输入!')
  146.             login(msg,title)
  147.         else:
  148.             break
  149.     return user_info


  150. def LogIn():
  151.    
  152.     msg = '请输入用户名和密码'
  153.     title = '用户登录窗口'
  154.     (Name,Password) = login(msg,title)
  155.     if Name in frozen_users('read',Name):
  156.         g.msgbox('该用户已冻结,请前往解冻')
  157.         frozen_off(Name)
  158.     else:
  159.          global current_user
  160.          current_user = Name
  161.          user_ID = dict_members[Name]
  162.          passwd = read_password()
  163.          n = 3
  164.          while (n-1):
  165.             if Password == passwd[user_ID]:
  166.                
  167.                 # (暂时不用) g.msgbox('欢迎进入XXOO系统,请点击右上角的X结束程序',ok_button = '退出')
  168.                 return True
  169.                 break
  170.             else:
  171.                 n -= 1
  172.                 msg = '密码输入错误,你还有%d次机会' % n
  173.                 (Name,Password) = login(msg,title)
  174.          else:
  175.             g.msgbox('已输错三次密码,账号已锁定')
  176.             frozen_users('write',Name)
  177.             frozen_off(Name)
  178.         
  179.    

  180. import pickle

  181. dict_members = read_user_ID('data.pkl')
  182. cmd = True
  183. while cmd:
  184.     title = '请选择操作对象'
  185.     choices = ('注册','登录','忘记密码','退出')
  186.     temp = g.buttonbox('',title,choices)

  187.     if temp == '注册':
  188.         newmember = SignIn()
  189.         dict_members.update(newmember)
  190.         g.msgbox('注册成功,赶紧登陆试试吧~')
  191.         
  192.     elif temp == '登录' :
  193.         while LogIn():
  194.             cmd = main_menu()
  195.             if cmd:
  196.                 break

  197.     elif temp == '忘记密码':
  198.         find_password()

  199.     elif temp == '退出' :
  200.         break
  201.         
  202. g.msgbox('欢迎使用本程序',ok_button = '再见')
  203.             
  204.         


  205.         
  206.         

  207.         
  208.    
复制代码








小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-3 21:16:28 | 显示全部楼层


先提个最基本的优化,你运行程序前先检测有无data.pkl文件,若无就新建,否则读取
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-4 18:28:54 | 显示全部楼层
Twilight6 发表于 2020-7-3 21:16
先提个最基本的优化,你运行程序前先检测有无data.pkl文件,若无就新建,否则读取

谢谢,会加进去的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 05:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表