Python 用户名和密码
我正在尝试创建一个程序,您可以在其中创建、登录和删除帐户。我制作了 2 个列表,一份包含用户名,一份包含密码。该脚本要求输入,如果您说登录,它会说:
if loginchoice=='login':
choice = raw_input("What is your username? ")
choice2 = raw_input("What is your password? ")
if choice in accounts:
option1=1
else:
option1=0
if choice2 in password:
option2=1
else:
option2=0
if option1==1 and option2==1:
print "Welcome to Telemology,", choice
else:
print "The username or password you entered is incorrect. Please try again or register."
如您所见,它仅检查输入是否已在列表中。它不会查看输入是否具有相同的索引。我不能输入“accounts.index(choice)”,因为它将“choice”视为整数。由于字符串的缘故,引号也是如此。它不将它们视为变量。有什么办法解决这个问题吗?
我希望如果我的问题得到解答,两个人不会同时注册并导致索引出现故障。
I am trying to create a program in which you can create, log in, and delete accounts. I have made 2 lists, one with usernames and one with passwords. The script asks for an input and if you say log in, it says:
if loginchoice=='login':
choice = raw_input("What is your username? ")
choice2 = raw_input("What is your password? ")
if choice in accounts:
option1=1
else:
option1=0
if choice2 in password:
option2=1
else:
option2=0
if option1==1 and option2==1:
print "Welcome to Telemology,", choice
else:
print "The username or password you entered is incorrect. Please try again or register."
As you can see, it only checks to see if the inputs are already in the lists. It doesn't see if the inputs have the same index. I can't put "accounts.index(choice)" because it treats 'choice' as an integer. The same goes for quotations because of strings. It doesn't treat them as variables. Is there any way around this?
I hope that if my question gets answered, two people won't register simultaneously and glitch the indices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你想要的是一个映射,或者一本字典。它的密钥是用户名,其内容是该用户名的密码。
当然,在真实的系统中,您会存储加盐和加密的密码。
What you want is a mapping, or a dictionary. Its key would be username, its contents the password for that username.
Of course, in a real system you'd store passwords salted and encrypted.
您应该使用 getpass 模块来确保密码不会回显到屏幕上,并使用 hashlib 模块来确保不存储原始密码。这是一个使用两个模块的类,并且应该适用于 python2 和 python3。如果您想从文件中加载它,您应该将 UserDB.users 属性存储在文件中,您可以使用 json 模块来执行此操作。
You should use the getpass module to ensure that the password is not echoed to the screen and the hashlib module to mean that the raw password isn't stored. Here is a class which uses both modules and should work on python2 and python3. If you want to load it out of a file the you should store the UserDB.users attribute in a file, you could use the json module to do that.
相反,创建一个用户列表,并创建 User 对象,该对象将用户名和密码作为属性。
拥有两个列表迟早会失去同步。
Create one list of users instead, and create User objects, which have username and password as attributes.
Having two lists is going to get desynchronized sooner or later.
既然是
login
,它应该是唯一的,你可以做一个字典:然后像这样检查:
Since it is
login
, it should be unique, and you can make a dictionary:and then check like this:
我看到的一些问题:
我还没有找到不回显密码的便携式方法。 #2是用字典解决的。至于#3,您可能需要使用 hashlib 模块:
替换
sha224
使用您想要的任何哈希算法;我还没有记录哪些仍然适合作为密码。编辑:关于问题#1,如果您正在运行基于 POSIX 的系统(即不在 Windows 提示符下),您可以使用 Python 页面中的一些示例代码来实现 termios 模块获取密码而不将其回显到屏幕:
这不会放置
*
为角色准备;它只是抑制输入。要放置*
,您必须一次处理一个字符的输入并编写更多代码。Some problems I'm seeing:
I haven't found the portable way to not echo passwords. #2 is solved with a dictionary. As for #3, you'll probably want to use the hashlib module:
Replace
sha224
with whatever hash algorithm you want; I haven't kept track of which ones are still good for passwords.EDIT: Regarding problem #1, if you're running a POSIX-based system (i.e. not at a Windows prompt), you can use some example code from Python's page for the termios module to get a password without echoing it to the screen:
This doesn't put
*
's up for the characters; it just suppresses input. To put up*
's, you have to handle input one character at a time and write more code.我有一个非常简单的代码,允许对您的密码进行两次尝试,如果它们不匹配,它将无法工作
i have a very simple code that alows two atemps at your password if they dont match it wont work
或者你可以只使用 SQLite3 db。这将允许您在运行时添加用户和密码。 SQLite3 本身也提供散列。
Or you could just SQLite3 db. That will allow you to add users and password on runtime. SQLite3 also provides hashing itself.
您始终可以使用帐户名及其各自的密码创建字典。
dictionary={'用户名':'密码','其他用户名':'其他密码'}
如果字典[选择] == 选择2:
print '欢迎来到 Telemology,%s' %choice
You can always create a dictionary with the account names and their respective passwords.
dictionary={'username':'password','some other username':'some other password'}
if dictionary[choice] == choice2:
print 'Welcome to Telemology, %s' %choice
您应该将密码的输入更改为整数,如下所示:
然后一切都应该正常。
You should change the input to your password to integer as follows:
and then everything should work.