[英]How to Dump, Load and Read info from pickle
我正在创建一些代码,询问用户一些问题并将其数据存储在列表中。 我希望能够保存该列表并存储它,以便当他们回来时,可以从上次停止的地方开始接听。 这是一个简单的程序,因此我并不想使其变得非常复杂。 我可以用泡菜来做到这一点吗?
import pickle
username = raw_input("Choose a username \n")
password = raw_input("Choose a password \n")
username_password = {}
def save_progress(save):
if 'yes' in save:
username_password[username] = password
with open('filename.pickle', 'wb') as handle:
pickle.dump(like_people, handle) #like_people is a list with data
pickle.dump(username_password, handle)
return "Progress saved"
elif 'no' in save:
print "Alright we won't save your progress"
else:
print "I don't know what happened"
print save_progress(raw_input("Would you like to save your progress \n").lower())
如果我想创建一个功能
def load_progress(load):
if 'yes' in load:
#have them input their username
#have them input their password
#How would I check to see if that username + password matched what they originally
#inputted and if so, load their progress
elif 'no' in load:
return "Alright we won't be loading"
else:
print "I don't know what is happening"
我什至可以用泡菜来做到这一点吗? 如果我需要编辑或添加任何内容,请告诉我,我会的。
就泡菜而言,答案是:
with open('filename.pickle', 'rb') as infile:
loaded_people = pickle.load(infile)
loaded_user = pickle.load(infile)
#have them input their username
#have them input their password
if username_password.get(username) == password:
print 'OK, loaded'
like_people = loaded_data
username_password = loaded_user
else:
raise Exception('nice try but no cigar')
但是,从安全性的角度来看,保存这样的密码始于可疑。
如果用户可以读取文件,则可以看到自己的用户名和密码,并在运行程序时键入。 由于用户(尽管被告知不这样做)通常将相同的密码用于不同的事物,因此冒风险泄漏密码,即使您实际上并不关心所保护事物的安全性也是如此。
解决方法与泡菜文件,查找密码哈希和/或使用HMAC签名无关。 如果您还想让可以读取文件但不知道密码的人对数据本身保密,则需要加密。
另外,如果您根本不需要任何安全性,只需一个名称与此存储的数据相关联,然后完全删除“密码”即可。 如果不是真正的登录名,那毫无用处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.