python学习-密钥认证

hcwei 2020年06月10日 261次浏览
import getpass
import json

_name=input("name:")
with open("lock.txt", 'r',encoding="utf-8") as f:
  for line in f:
     if _name in line:
      print("you are locked")
      exit()

def loadpassword():
  with open("test.json", encoding='utf-8') as f:
    setting = json.load(f)
    PASS = setting[_name]
    return PASS
passwords = loadpassword()

last_time = 3
for i in range(3):
  print("倒数"+str(last_time)+"次")
#  password=int(input("password:"))
  password=int(getpass.getpass("password:"))
  if password==passwords:
      print("welcome")
      break
  else:
      print("password wrong!!!check it")
      last_time = last_time-1
      if last_time == 1:
          print("请注意这是最后一次,如果输入错误账户将被锁定!!!")
  if i==2:
      print("locked")
      with open("lock.txt", "a",encoding="utf-8") as f:
          f.write('\n'+_name)
          print("加载入文件完成...")