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()



  • 相关阅读:
    Mongodb 与 MySQL对比
    MongoDB的真正性能-实战百万用户
    走进科学之揭开神秘的"零拷贝"!
    对于 Netty ByteBuf 的零拷贝(Zero Copy) 的理解
    <Netty>(入门篇)TIP黏包/拆包问题原因及换行的初步解决之道
    MSSQL复制功能实现与Oracle数据库同步
    SQLServer与Oracle的数据同步(触发器trigger)
    ORACLE和SQL SERVER的数据同步常用方法
    MS SQL SERVER: msdb.dbo.MSdatatype_mappings & msdb.dbo.sysdatatypemappings
    FORM 错误:此责任无可用函数。 更改责任或与您的系统管理员联系。
  • 原文地址:https://www.cnblogs.com/hqd2008/p/7637588.html
Copyright © 2011-2022 走看看