![](/img/trans.png)
[英]how to login into the database with username, password using python code?
[英]How to add a maximum login attempt to a python script where the username and password is from the database?
我從 inte.net 上找到了這個,並試圖將它應用到我的學校項目中。 我嘗試使用 while 循環,但未能使其工作。
def logs():
mycursor = mydb.cursor()
sql = "SELECT * FROM login_data WHERE BINARY username = '%s' OR BINARY email_address = '%s' AND BINARY password = '%s'" % (
username.get(), username.get(), password.get())
mycursor.execute(sql)
if mycursor.fetchone():
subprocess.Popen([sys.executable, "Manager.py"])
exit()
else:
messagebox.showinfo("Warning", "Wrong username or password")
您可以聲明一個全局變量 fail_count。 (在這個函數之外)在else部分每次加1。 確保為每個 session 重置 fail_count = 0
fail_count = 0
def logs():
mycursor = mydb.cursor()
if fail_count < 3:
sql = "SELECT * FROM login_data WHERE BINARY username = '%s' OR BINARY email_address = '%s' AND BINARY password = '%s'" % (
username.get(), username.get(), password.get())
mycursor.execute(sql)
if mycursor.fetchone():
subprocess.Popen([sys.executable, "Manager.py"])
exit()
else:
fail_count = fail_count + 1
messagebox.showinfo("Warning", "Wrong username or password")
如果您想從頭開始構建它,這是我的好習慣:
假設你新建了一張表LoginAttemp,列為ID、USERNAME、ATTEMP。
評估每次登錄 ATTEMPT,其中 USERNAME 有效且密碼無效,將 ATTEMP 編號加一。
在每次登錄 ATTEMPT 時創建 ATTEMPT 檢查器。 當數據庫中的ATTEMPT超過你的MAX時,讓它異常登錄。
你應該為 RESET 登錄 ATTEMPT 創造條件 如果你想使用可用的工具,這里有一些很好的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.