Python usernames and passwords

I am trying to create a program in which you can create, register and delete accounts. I made 2 lists, one with usernames and one with passwords. the script asks for input, and if you say that it is logged 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 if the entries are already in the lists. He does not see if the inputs have the same index. I cannot put "accounts.index (selection)" because it treats the "selection" as a whole. The same goes for quotes due to lines. He does not consider them as variables. Is there any way around this?

I hope that if my question gets an answer, two people will not be registered at the same time and index failures.

+3
9

, , . , - .

users = {} # this is the mapping
users["joe"] = "123" # it currently contains username "joe" with password "123"
...
username = raw_input("What is your username? ")
password = raw_input("What is your password? ")
if username in users.keys():
  expected_password = users[username]
  if expected_password == password:
    print "Welcome to Telemology,", username
  else:
    print "Didn't you forget your password,", username
else:
  print "Unknown user"

, .

+2

getpass, , , hashlib , . , python2 python3. , UserDB.users , json .

import getpass
import hashlib
import random
import sys
class UserDB():
    def __init__(self, users=dict(), unprompt='Username:', pwprompt='Password:'):
        self.users=dict(users)
        self.unprompt=unprompt
        self.pwprompt=pwprompt
    def adduser(self):
        if sys.version_info.major==3:
            name=input(self.unprompt)
        elif sys.version_info.major==2:
            name=raw_input(self.unprompt)
        passwd=getpass.getpass(self.pwprompt).encode('utf-8')
        salt=bytes(random.randint(30, 95) for i in range(10))
        passwd=hashlib.pbkdf2_hmac('sha512', passwd, salt, 10*10)
        self.users[name]=[salt, passwd]
    def deluser(self, name):
        del self.users[name]
    def __str__(self):
        return str(self.users)
    def __repr__(self):
        return 'UserDB(users=%s, unprompt=%s, pwprompt=%s)' % (self.users,
                                                           ascii(self.unprompt),
                                                           ascii(self.pwprompt))
    def login(self):
        if sys.version_info.major==3:
            name=input(self.unprompt)
        elif sys.version_info.major==2:
            name=raw_input(self.unprompt)
        if name not in self.users:
            return False
        passwd=getpass.getpass(self.pwprompt).encode('utf-8')
        salt=self.users[name][0]
        passwd=hashlib.pbkdf2_hmac('sha512', passwd, salt, 10*10)
        return self.users[name][1]==passwd
+2

login, , :

users = {'username':'password', ...}

:

choice = raw_input("What is your username? ")
choice2 = raw_input("What is your password? ")
if (choice in users) and (choice2 == users[choice]):
   # valid user
else:
   # login or password incorrect.
+1

, .

, .

+1

, :

  • , .
  • , .
  • .

-. # 2 . № 3, , , hashlib:

from hashlib import sha224

PASSWORD_HASHES = {}

def set_password(account, raw_password):
    PASSWORD_HASHES[account] = sha224(raw_password).digest()

def authenticate(account, raw_password):
    if account not in PASSWORD_HASHES:
        return False
    return PASSWORD_HASHES[account] == sha224(raw_password).digest()

sha224 , ; , .

: № 1, POSIX- (.. Windows), Python termios, :

def getpass(prompt="Password: "):
    import termios, sys
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~termios.ECHO          # lflags
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        passwd = raw_input(prompt)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)
    return passwd

* ; . *, .

+1

, , .

    users = {}
users["bob"] = "bob" 

username = raw_input("What is your username? ")
password = raw_input("What is your password? ")
if username in users.keys():
  expected_password = users[username]
  if expected_password == password:
    print "Welcome to Telemology,", username
  else:
    print "Didn't you forget your password,", username

password = raw_input("What is your password? ")
if username in users.keys():
  expected_password = users["bob"]
  if expected_password == password:
    print "Welcome to Telemology,", username
    #*here
  else:
    print "Didn't you forget your password,", username
else:
  print "Unknown user"
#continue your code for wrong here else*
+1

SQLite3 db. . SQLite3 .

0

You can always create a dictionary with account names and their corresponding passwords. dictionary={'username':'password','some other username':'some other password'} if dictionary[choice] == choice2: print 'Welcome to Telemology, %s' %choice

0
source

You must change the password entry to an integer as follows:

password = raw_input("What is your password? ") --->this is your code
password = int(input("what is your password?")) --->modified code

and then everything should work.

0
source

Source: https://habr.com/ru/post/1781127/


All Articles