【问题标题】:Python Pickled PasswordsPython腌制密码
【发布时间】:2015-02-24 10:55:15
【问题描述】:

这是我使用pickle的python密码系统。我知道这很糟糕,但这是我第一次吃泡菜。

import pickle
import os

userlist = {'user1':'userpass1', 'user2':'userpass2'}

users = open ("users.pkl", 'wb')

pickle.dump (userlist, users)

username = input ("Enter your username: ")
password = input ("Enter your password: ")

if (username in userlist) and (password == userlist[username]):
    print ("Access Granted")
else:
   newaccount = input ("User not found. Shall I create a new account? ")
    if newaccount == "yes":
       username = input ("Please enter your username: ")
       password = input ("Please enter yout password: ")
       userlist.update({username:password})
       pickle.dump (userlist, users)
       users.close()

我的问题是,每当我去添加一个新帐户时,使用这部分:

 newaccount = input ("User not found. Shall I create a new account? ")
    if newaccount == "yes":
        username = input ("Please enter your username: ")
        password = input ("Please enter yout password: ")
        userlist.update({username:password})
        pickle.dump (userlist, users)
        users.close()

它似乎添加了它(看起来它在使用记事本的泡菜文件中)但是,我重新启动 python 文件,它没有看到它。

我认为这与这部分有关:

userlist = {'user1':'userpass1', 'user2':'userpass2'}

users = open ("users.pkl", 'wb')

pickle.dump (userlist, users)

感谢任何帮助! :D

【问题讨论】:

  • 密码属于一个非常特殊的类别。即使是简单的事情也应该以不同的方式使用密码,例如,不要以纯文本形式存储它们,不要在源代码中对密码进行硬编码。
  • 我知道,但这是给学校的 - 没必要。

标签: python dictionary pickle


【解决方案1】:

每次运行程序时都用w覆盖:

users = open ("users.pkl", 'wb')

如果您想获取之前腌制的项目,您需要查看文件是否已经存在,pickle.load 获取之前腌制的项目,然后在代码末尾dump

类似于以下内容:

from tempfile import NamedTemporaryFile

try:
    # see if we have run  this before
    with  open ("users.pkl", 'rb') as users:
       users_dict = pickle.load(users)
except IOError:
    # if not set to defualt
    users_dict = {'user1':'userpass1', 'user2':'userpass2'}


username = input ("Enter your username: ")
password = input ("Enter your password: ")

if users_dict.get(username) == password: # unless a password can be None we can use get
    print ("Access Granted")
else:
    newaccount = input("User not found. Shall I create a new account? ")
    if newaccount == "yes":
       username = input("Please enter your username: ")
       password = input ("Please enter yout password: ")
       users_dict[username] = password # just use key = value

with NamedTemporaryFile("wb",dir=os.path.dirname("users.pkl"),delete=False) as f: # in case we get exception use temp file
    pickle.dump (users_dict, f)
os.replace(f.name,"users.pkl") # update original

【讨论】:

  • 您无法写入使用rb 模式打开的文件。
  • 您可能也应该对users 文件使用with-statement(我不知道您是否可以在Windows 上打开已经打开的文件)。写入一个临时文件,最后使用os.replace()——否则如果pickle.dump期间发生错误,密码数据库将丢失。如果该程序的多个实例可能同时运行或the power is lost suddenly
  • @J.F.Sebastian,是的,我以为我已经关闭了原始文件,但无论如何使用 with 可能会更好。
  • 我的意思是tempfile.NamedTemporaryFile(注意:设置dir很重要)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2011-05-04
相关资源
最近更新 更多