zoukankan      html  css  js  c++  java
  • 用户登陆--判断输入密码错误3次后冻结该账号

    '''
    先创建user、lock文件,user存储账号密码,用于登录时判断,lock文件为账号错误3次后,冻结
    user内容格式为:
    {'name':'zhangsan','passwd':'123'}
    {'name':'lisi','passwd':'123'}
    {'name':'wangwu','passwd':'123'}
    '''

    import os
    '''此方法暂时未用到try/except'''

    def lock(_name):
    '''判断一个用户是否冻结'''
    if os.path.getsize("lock"):
    with open("lock","r") as f:
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["name"] == _name and line["num"] == 3:
    return 0
    else:
    return -1
    else:
    return -1

    def clear_lock():
    '''当程序退出时,删除非冻结的账号信息'''
    list = []
    with open("lock","r+") as f:
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["num"] == 3:
    list.append(line)
    f.truncate(0)
    with open("lock","w") as f1:
    for i in list:
    f1.write(str(i) + ' ')

    def login(_name,_passwd):
    '''用户登陆'''
    with open("user","r") as f:
    flag = 0
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["name"] == _name:
    if line["name"] == _name and line["passwd"] == _passwd:
    return 0
    else:
    return 1
    return -1

    def write_lock(_name):
    '''将输入错误的账号写入lock文件'''
    #文件不为空
    if os.path.getsize("lock"):
    list = []
    with open("lock","r+") as f:
    flag = 0
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    #判断账号是否存在
    if line["name"] == _name:
    line["num"] += 1
    list.append(line)
    else:
    dict2 = {}
    dict2["name"] = _name
    dict2["num"] = 1
    list.append(dict2)
    with open("lock","w") as f1:
    for i in list:
    f1.write(str(i) + ' ')


    #空文件直接写入
    else:
    list1 = []
    dict1 = {}
    dict1["name"] = _name
    dict1["num"] = 1
    list1.append(str(dict1))
    with open("lock","w") as f2:
    for j in list1:
    f2.write(str(j) + ' ')

    def main():
    name = input("用户名:")
    res = lock(name)
    if res == 0:
    print("%s已被冻结,请联系管理员"%name)
    clear_lock()
    exit()
    passwd = input("密码:")
    res1 = login(name,passwd)
    if res1 == 0:
    print("欢迎%s登陆51CTO"%name)
    clear_lock()
    exit()
    elif res1 == 1:
    print("密码错误")
    write_lock(name)
    else:
    print("账号不存在")

    res2 = lock(name)
    if res2 == 0:
    print("%s已被冻结,请联系管理员" %name)
    clear_lock()
    exit()

    while True:
    main()



  • 相关阅读:
    jvm 垃圾回收
    shell编写一键启动
    jvm内存结构
    java 线程监控
    linux 操作系统级别监控 nmon命令
    linux 操作系统级别监控 vmstat/dstat 命令
    linux 操作系统级别监控 df 命令
    linux 操作系统级别监控 iostat 命令
    linux 操作系统级别监控 free命令
    linux 操作系统级别监控 TOP命令
  • 原文地址:https://www.cnblogs.com/hqd2008/p/7637588.html
Copyright © 2011-2022 走看看