zoukankan      html  css  js  c++  java
  • python-实现登录接口

    简介:1.在本地创建两个文件,一个文件的内容是存储用户名和密码,另一个的内容是存储锁定的用户

             2.登录次数限制为三次,当密码错误三次后,该账号锁定,无法再次登录

    #coding=utf-8
    
    user_info_list = []
    #打开两个文件
    with open('userinfo','rb') as userinfo_file,
        open('locketuser','rb') as locketuser_file:
            all_userinfo_file = userinfo_file.read()                        #获取文件中的内容
            all_userinfo_file_list = all_userinfo_file.splitlines()         #把文件中的内容按行分割,放入列表中
            for user_info_str in all_userinfo_file_list:                    #获取列表中的每个元素
                user_info_tuple = tuple(user_info_str.split())              #把每个元素保存为一个数组
                user_info_list.append(user_info_tuple)                      #把每个数组添加到一个列表中
            user_info_dict = dict(user_info_list)                           #把列表转为一个字典
    
    
            all_locketuser_file = locketuser_file.read()
            user_locket_info_list = all_locketuser_file.splitlines()
    with open('locketuser','ab') as locketuser_file:
        while True:
            user_login_flag = False                                         #定义一个是否登录状态的,初始值未false
            username_input=raw_input('请输入用户名:')
            if username_input in user_locket_info_list:                     #先判断是否在黑名单中
                print ("您已被加入黑名单,请重新输入。。。")
            elif username_input in user_info_dict:                          #判断用户名是否在字典中
                count = 3
                while True:
                    pass_word = raw_input("请输入密码:")
                    if pass_word == user_info_dict.get(username_input):     #通过字典,直接判断用户名和密码是否匹配
                        user_login_flag = True 
                        print ("{0}登录成功,欢迎。。".format(username_input))
                        break                                               #登录成功,跳出内层while循环
                    else:
                        count -= 1                                          #用户名和密码未匹配成功,可尝试登录次数-1
                        if count == 0:                                      #可尝试登录次数为0,则退出
                            locketuser_file.write(username_input)
                            exit("{0}已被锁定".format(username_input))
                        print ("{0}密码错误,还剩{1}次机会。。".format(username_input,count))  #可尝试登录次数不为0,可继续尝试登录,给出剩余次数提示
                if user_login_flag:                                         #如果登录成功,则跳出外层while循环
                    break
            else:
                print ("33[31m{0}33[0m,此用户名不存在,请重新输入".format(username_input))
    

      

  • 相关阅读:
    Java中Runnable和Thread的区别
    Callable,Runnable比较及用法
    如何实现视差滚动效果的网页?
    【175】Easy CHM的使用
    【174】C#添加非默认字体
    【173】双显示器随便切换位置
    【172】outlook邮箱设置
    【171】IDL读取HDF文件
    怎样实现二级联动
    Java 23种设计模式详尽分析与实例解析之二--结构型模式
  • 原文地址:https://www.cnblogs.com/nizhihong/p/6550022.html
Copyright © 2011-2022 走看看