Python實(shí)現(xiàn)用戶登錄并且輸入錯(cuò)誤三次后鎖定該用戶
實(shí)現(xiàn)用戶登錄并且輸入錯(cuò)誤三次后鎖定該用戶
我的測(cè)試環(huán)境,win7,python3.5.1
提示輸入用戶名,和密碼
判斷是否被鎖定
判斷用戶名和密碼是否匹配
輸入錯(cuò)誤三次,賬號(hào)被鎖定
思路

代碼塊
name = 'alex' #正確的用戶名
passwd = '123456' #正確的密碼
lock_usr = [] #鎖定賬號(hào)列表
for i in range(0,3):
usr_name = input("用戶名:")
usr_passwd = input("密碼:")
if usr_name == name and usr_passwd == passwd:
print("玩命加載中...")
break
elif name != usr_name or passwd != usr_passwd:
if i < 2:
print("用戶名密碼錯(cuò)誤,請(qǐng)重新輸入!")
else:
lock_usr.append(usr_name) #將輸入錯(cuò)誤三次的的賬號(hào)添加到鎖定列表中
print("對(duì)不起!機(jī)會(huì)只有三次,您的賬號(hào)密碼被鎖定")
elif usr_name in lock_usr:
print("該賬號(hào)已鎖定,請(qǐng)解鎖后登陸")
PS:下面在通過c#實(shí)現(xiàn)密碼輸入三次錯(cuò)誤后鎖定用戶功能
#region 密碼輸入三次錯(cuò)誤后鎖定用戶功能
#region 增加錯(cuò)誤次數(shù)的方法
///<summary>
///增加錯(cuò)誤次數(shù)的方法
///</summary>
private void IncErrorTime()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand updateCmd = conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set errortime=errortime+1 whereUsername=@username";
updateCmd.Parameters.Add(new SqlParameter("username", cmbuserName.Text));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
#endregion
#region 錯(cuò)誤次數(shù)清0
///<summary>
///錯(cuò)誤次數(shù)清0
///</summary>
private void NotErrorTime()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand updateCmd = conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set errortime=0 whereUsername=@username";
updateCmd.Parameters.Add(new SqlParameter("username", cmbuserName.Text));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
#endregion
#region 密碼錯(cuò)誤3次,記錄當(dāng)前時(shí)間加30分鐘
///<summary>
///密碼錯(cuò)誤3次,記錄當(dāng)前時(shí)間加30分鐘
///</summary>
private void IncLoginTime()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand updateCmd = conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set logintime=@logintime whereUsername=@username";
DateTime logintime = DateTime.Now.AddMinutes(30);
updateCmd.Parameters.Add(new SqlParameter("username", cmbuserName.Text));
updateCmd.Parameters.Add(new SqlParameter("logintime", logintime.ToString()));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
#endregion
#region 按鈕事件判斷用戶登錄3次失效后鎖定用戶30分鐘
private void BtnClike()
{
string username = cmbuserName.Text;
string password = txtPwd.Text;
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Admin whereusername=@username";
cmd.Parameters.Add(new SqlParameter("username", username));
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
//用戶存在
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
DateTime logintime = reader.GetDateTime(reader.GetOrdinal("logintime"));
//判斷當(dāng)前時(shí)間是是服務(wù)器允許登錄時(shí)間
if (logintime > DateTime.Now)
{
MessageBox.Show("一定時(shí)間內(nèi)禁止登錄");
return;
}
//如果密碼正確
if (dbpassword == txtPwd.Text)
{
NotErrorTime();
MessageBox.Show("登錄成功!");
}
//如果密碼錯(cuò)誤
else
{
int errortime = reader.GetInt32(reader.GetOrdinal("errortime"));
if (errortime >= 2)
{
MessageBox.Show("密碼錯(cuò)誤次數(shù)太多!");
IncLoginTime();
NotErrorTime();
return;
}
MessageBox.Show("密碼錯(cuò)誤!");
IncErrorTime();//密碼錯(cuò)誤,次數(shù)加1
}
}
else//用戶名不存在
{
MessageBox.Show("用戶名不存在!");
return;
}
}
}
}
}
#endregion
#endregion
總結(jié)
以上所述是小編給大家介紹的Python實(shí)現(xiàn)用戶登錄并且輸入錯(cuò)誤三次后鎖定該用戶,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- python 用戶交互輸入input的4種用法詳解
- python中使用input()函數(shù)獲取用戶輸入值方式
- python根據(jù)用戶需求輸入想爬取的內(nèi)容及頁(yè)數(shù)爬取圖片方法詳解
- Python 用戶輸入和while循環(huán)的操作
- 學(xué)習(xí)python 的while循環(huán)嵌套
- Python?實(shí)現(xiàn)循環(huán)最快方式(for、while?等速度對(duì)比)
- Python?不設(shè)計(jì)?do-while?循環(huán)結(jié)構(gòu)的理由
- Python中用戶輸入與while循環(huán)詳情
相關(guān)文章
最新Listary?v5.00.2843注冊(cè)碼?親測(cè)可用
listary是?windows?下一款可以快速搜索所有程序、文件,并且可以快速啟動(dòng)程序和打開相應(yīng)文件的優(yōu)秀的搜索軟件,絕對(duì)比系統(tǒng)自帶搜索速度快很多,而且查詢功能豐富,本文給大家分享Listary?v5.00.2843注冊(cè)碼,感興趣的朋友一起看看吧2022-07-07
flask+layui+echarts實(shí)現(xiàn)前端動(dòng)態(tài)圖展示數(shù)據(jù)效果
這篇文章主要介紹了flask+layui+echarts實(shí)現(xiàn)前端動(dòng)態(tài)圖展示數(shù)據(jù)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
百度HI QQ和MSN 阿里旺旺貿(mào)易通MSN在線客服在線聊天代碼
有時(shí)候業(yè)務(wù)需要,需要讓客戶更方便的與我們溝通,就可以參考下面的代碼。2010-04-04
ArcGIS Pro 按照字段進(jìn)行融合或拆分的操作步驟
ArcGIS Pro 是 Esri 提供的功能全面的專業(yè)桌面 GIS 應(yīng)用程序,這篇文章主要介紹了ArcGIS Pro 按照字段進(jìn)行融合或拆分,需要的朋友可以參考下2024-02-02
用asp與php實(shí)現(xiàn)百度ping服務(wù)的代碼
分別用asp與php實(shí)現(xiàn)百度ping服務(wù)的代碼,需要的朋友可以參考下2012-02-02
kali-linux?202202?安裝w3af命令行版的詳細(xì)過程
這篇文章主要介紹了kali-linux?202202?安裝w3af命令行版,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

