• 用Python编写登录接口


    登陆接口要求:

    1. 输入用户名和密码
    2. 认证成功后显示欢迎信息
    3. 输错三次后锁定

    代码:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 # Author:James Tao
     4 
     5 import pickle
     6 import os
     7 
     8 #注册
     9 register={'tz':'1234','t':'123','tza':'12345abc'}
    10 locked_account=[]
    11 
    12 #也可由用户输入注册账号
    13 #register_username=input('newusername:')
    14 #register_passord=input('newpassword:')
    15 #print('注册成功')
    16 #register[register_username]=register_passord
    17 
    18 #用pickle把已注册的账号字典序列化写到文件中
    19 w_register=open('register.txt','wb')
    20 pickle.dump(register,w_register)
    21 w_register.close()
    22 
    23 #登陆
    24 count=0
    25 while count<3:
    26     print('请登录')
    27     username=input('username:')
    28     password=input('password:')
    29 
    30     #若被锁定账号文件不为空,读出已锁定的账号
    31     if os.path.getsize('lockedaccount.txt') > 0:
    32 
    33         #用pickle把被锁定账号从文件中反序列化读出来
    34         r_lockedaccount=open('lockedaccount.txt', 'rb')
    35         locked_account=pickle.load(r_lockedaccount)
    36         r_lockedaccount.close()
    37 
    38     #判断输入的账号是否已被锁定,若账号已被锁定则直接跳出所有循环
    39     judge_of_locked=False
    40     for locked_name in locked_account:
    41         if username==locked_name:
    42             print('The account has been locked')
    43             judge_of_locked = True
    44             break
    45     if judge_of_locked:
    46         break
    47 
    48     #用pickle把已注册的账号字典反序列化读出来
    49     r_register=open('register.txt','rb')
    50     login=pickle.load(r_register)
    51     r_register.close()
    52 
    53     #判断输入的账号是否存在密码是否正确
    54     if username in login and password==login[username]:
    55         print('Welcome')
    56         break
    57     else:
    58         print('Non-existed username or password error')
    59     count+=1
    60 
    61 #若账号和密码输错三次账号会被锁定
    62 else:
    63     print('You have tried too many times,The account has been locked')
    64 
    65     #插入被锁定账号的用户名
    66     locked_account.append(username)
    67 
    68     #用pickle把被锁定账号的列表序列化写到文件中
    69     w_lockedaccount = open('lockedaccount.txt', 'wb')
    70     pickle.dump(locked_account,w_lockedaccount)
    71     w_lockedaccount.close()
  • 相关阅读:
    前端到后台ThinkPHP开发整站(4)
    前端到后台ThinkPHP开发整站(2)
    字典树模版
    KMP模版
    EXKMP模版
    Manacher模版
    AC自动机练习题1:地图匹配
    AC自动机模版
    spring.net之aop加单例模式编写无try catch程序
    父类与子类之间赋值转换
  • 原文地址:https://www.cnblogs.com/BIT-taozhen/p/9709345.html
Copyright © 2020-2023  润新知