欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)用戶登錄并且輸入錯(cuò)誤三次后鎖定該用戶

 更新時(shí)間:2020年01月20日 11:16:28   作者:何春暉ERP程序員  
這篇文章主要介紹了Python實(shí)現(xiàn)用戶登錄并且輸入錯(cuò)誤三次后鎖定該用戶,文中通過(guò)c#代碼給大家補(bǔ)充介紹了密碼輸入三次錯(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:下面在通過(guò)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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論