zoukankan      html  css  js  c++  java
  • Python小程序之用户登陆接口

    编写登陆接口

    • 输入用户名密码
    • 认证成功后显示欢迎信息
    • 输错三次后锁定

    程序逻辑图:

    代码:

     1 #!/usr/bin/env python
     2 #_*_ coding:UTF-8 _*_
     3 #__author__ = lee sir
     4 
     5 import sys
     6 
     7 userinfo = r'D:Python_Scriptsday1userinfo.txt'
     8 userlock = r'D:Python_Scriptsday1userlock.txt'
     9 
    10 
    11 
    12 #用户输入用户名和密码
    13 def user_input():
    14     name = input("username:")
    15     passwd = input("password:")
    16     return name,passwd
    17 
    18 #判断用户是否是已注册用户
    19 def judge_register(user):
    20     with open(userinfo) as fd:
    21         for info in fd.readlines():
    22             username,password = info.split()
    23             if user == username:
    24                 return True
    25         else:
    26             return False
    27 
    28 #注册用户
    29 def register_user(user,password):
    30     fd = open(userinfo,'a+')
    31     fd.write('%s %s
    ' % (user,password))
    32     sys.exit('user register Success!')
    33 
    34 
    35 #判断用户名密码正确性
    36 def judge_password(user,passwd):
    37     with open(userinfo) as fd:
    38         for info in fd.readlines():
    39             username,password = info.split()
    40             if user == username and passwd == password:
    41                 return True
    42         else:
    43             return False
    44 
    45 
    46 #测试用户是否被锁定
    47 def test_user(user):
    48     with open(userlock) as fd:
    49         for info in fd.readlines():
    50             if user == info.strip('
    '):
    51                 return True
    52         else:
    53             return False
    54 
    55 #锁定用户
    56 def lockuser(user):
    57     fd = open(userlock,'a+')
    58     fd.write('%s
    ' % user )
    59     fd.close()
    60     sys.exit("Your Account is locking !!")
    61 
    62 #主函数
    63 def main():
    64     count = 0
    65     while count < 3:
    66         username,password = user_input()    #获取用户输入的用户名和密码
    67         judge_register_result = judge_register(username)
    68         if judge_register_result:   #启动登陆流程
    69             test_user_result = test_user(username)     #判断用户是否被锁定
    70             if test_user_result:
    71                 sys.exit('Sorry,Your Account is locked !!')
    72             else:   #验证用户名密码是否正确
    73                 judge_password_result = judge_password(username,password)
    74                 if judge_password_result:
    75                     sys.exit('Hello,Welcome login!!')
    76                 else:
    77                     if count < 2:
    78                         print('Username or Password is Wrong,You have %s chance' % (2-count))
    79                 count += 1
    80         else:   #启动注册流程
    81             user_choice = input('Are you want to register ? [y/n]')
    82             if user_choice == 'y' or user_choice == 'Y':
    83                 register_user(username,password)
    84                 sys.exit('register successful,Please relogin !')
    85             elif user_choice == 'n' or user_choice == 'N':
    86                 sys.exit('Bye Bye')
    87             else:
    88                 sys.exit('Input Error,Bye Bye!')
    89     else:    #三次失败 锁定帐户
    90         lockuser(username)
    91 
    92 if __name__ == '__main__':
    93     main()
  • 相关阅读:
    python设计模式之简单工厂
    python设计模式之观察者模式
    python设计模式之模版方法设计模式
    python设计模式之策略模式
    python编程之数据描述器
    python设计模式之单例模式
    .NET 开源工作流: Slickflow流程引擎高级开发(七)--消息队列(RabbitMQ)的集成使用
    .NET 开源工作流: Slickflow流程引擎快速入门之四:--SlickGraph 流程模型编码建模工具使用手册
    .NET 开源工作流: Slickflow流程引擎快速入门之三: 简单或分支流程代码编写示例
    .NET 开源工作流: Slickflow流程引擎快速入门之二: 简单并行分支流程代码编写示例
  • 原文地址:https://www.cnblogs.com/dachenzi/p/6509716.html
Copyright © 2011-2022 走看看