2016-10-06 3 views
1

기본 인증을 사용하는 python (3.4) 코드를 작성하고 있습니다. 사용자 이름 & 암호를 텍스트 파일 (abc.txt)에 저장했습니다.
로그인 할 때 코드는 텍스트 파일의 첫 번째 줄만 받아들입니다. &은 나머지 자격 증명을 무시하고 잘못된 자격 증명 오류를 제공합니다.Python 3.4는 텍스트 파일에서 한 줄 이상을 읽지 않습니다.

내 코드 :

with open('abc.txt') as f: 
    credentials = [x.strip().split(':') for x in f.readlines()] 

for username, password in credentials: 

    user_input = input('Please Enter username: ') 

    if user_input != username: 

     sys.exit('Incorrect incorrect username, terminating... \n') 


    user_input = input('Please Enter Password: ') 



    if user_input != password: 

     sys.exit('Incorrect Password, terminating... \n') 



    print ('User is logged in!\n') 

abc.txt : 당신은 첫 번째 라인을 확인하고 있기 때문에 무슨 일이 일어나고

Sil:xyz123 
smith:abc321 
+0

질문에 더 구체적인 제목을 사용하십시오. –

답변

1

. 현재 사용자는 텍스트 파일의 첫 번째 줄과 일치하는 자격 증명 만 입력 할 수 있습니다. 그렇지 않으면 프로그램이 종료됩니다. 사용자 이름과 암호로 사전을 작성한 다음 사용자 이름이 해당 자격증 명 목록을 반복하는 대신 해당 사전에 있는지 확인해야합니다.

with open('abc.txt') as f: 
    credentials = dict([x.strip().split(':') for x in f.readlines()]) # Created a dictionary with username:password items 

username_input = input('Please Enter username: ') 

if username_input not in credentials: # Check if username is in the credentials dictionary 

     sys.exit('Incorrect incorrect username, terminating... \n') 

password_input = input('Please Enter Password: ') 

if password_input != credentials[username_input]: # Check if the password entered matches the password in the dictionary 

     sys.exit('Incorrect Password, terminating... \n') 

print ('User is logged in!\n') 
+0

@SmithPereira 어떤 오류가 있습니까? –

+0

안녕하세요, Farhan, + 대단히 감사합니다. – Von

+0

나의 이전 코멘트에 대해 유감스럽게 생각합니다. – Von

관련 문제