简体   繁体   English

使用 python 创建登录系统

[英]creating a login system with python

so i am new to python and trying to create a simple login system which registers users and login but i am having trouble in password and username validation following is the code (note that i have divided my code in different modules because i am thinking of implementing it in future:所以我是 python 的新手,并试图创建一个简单的登录系统来注册用户和登录,但我在密码和用户名验证方面遇到了问题,下面是代码(请注意,我已经将我的代码划分为不同的模块,因为我正在考虑实现将来:

register module
import re
def register():
    with open('username.txt', mode='a')as user_file:
        username = input('Enter Username : ')
        user_file.write(f"{username}\n")
    with open('password.txt', mode='a')as pass_file:
        password = input('Enter Password: ')
        pattern= re.compile(r'[a-zA-Z0-9@#!$%^&*]{8,}')
        if password==pattern.fullmatch(password):
            pass_file.write(f'{password}\n')
        else:
            print("your password should be atleast 8 characters long!")
login module
def login() :
    l_user = input('Username: ')
    l_pass = input('Password: ')
    with open('username.txt', mode='r')as user_file:
        for users in user_file:
            validate_u = user_file.readlines()
    with open('password.txt', mode='r')as pass_file:
        for passwords in pass_file:
            validate_p = pass_file.readlines()
    if l_user==validate_u and l_pass==validate_p:
        print('hello')
    else:
        print('login failed')
finally main module
import Enigma_Register
import Enigma_login

print('1-Login\n2-Register')
choice = int(input("enter choice: "))
if choice == 1:
    Enigma_login.login()

elif choice == 2:
    Enigma_Register.register()
    Enigma_login.login()
else:
    print('Invalid Choice!')

For your login function, replace the last four lines with:对于您的登录 function,将最后四行替换为:

if l_user+"\n" in validate_u: #ReadLines() adds newlines
   user_num = validate_u.index(l_user+"\n")
   if l_pass+"\n" == validate_p[user_num]:
      print("Password correct!")
   else:
      print("Password incorrect!")
else:
   print("Cannot find your username!")

also, the for users in user_file and for passwords in pass_file are unnecessary: just put the readlines() method on its own, without a loop.此外,user_file 中的for users in user_filefor passwords in pass_file是不必要的:只需将readlines()方法放在自己的位置上,没有循环。

Additionally, replace the if password==pattern.fullmatch with if pattern.match(password) .此外,将if password==pattern.fullmatch替换为if pattern.match(password)

I'd also suggest moving the file writes until after you've validated the password, otherwise "ghost usernames" with no passwords could appear, easily causing problems;我还建议您验证密码之前将文件写入移动,否则可能会出现没有密码的“幽灵用户名”,很容易导致问题; or you could replace all of the code in your register() function after password = input("Enter password: ") with或者您可以在password = input("Enter password: ")之后替换register() function 中的所有代码

pattern = re.compile( [YOUR REGEX HERE] )
while not pattern.match(password):
  print("Your password is invalid.")
pass_file.write(f'{password}\n')
user_file.write(f'{username}\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM