作业4.3

    3、编写注册功能,用户输入用户名、性别、年龄、密码。。。还需要输入一个随机验证码,若用户在60秒内输入验证码错误
    则产生新的验证码要求用户重新输入,直至输入正确,则将用户输入的信息以json的形式存入文件
    ###随机验证####
import random
import time
import json

def make_code(n):
    res = ''
    for i in range(n):
        s1 =  str(random.randint(0,9))
        s2 = chr(random.randint(65,90))
        res += random.choice([s1,s2])
    return res

def user_input():
    name = input('enter your name: ').strip()
    pwd = input('enter your pwd: ').strip()
    age = input('enter your age: ').strip()
    sex = input('enter your sex: ').strip()
    dic={'name':name,'pwd':pwd,'age':age,'sex':sex}
    return dic

def writing(x):
    with open('admin.json','w',encoding='utf-8') as f:
        json.dump(x,f)
def run():
    res = user_input()
    code = make_code(4)
    print(code)
    start_time = time.time()
    while True:
        user_code = input('please enter code: ').strip()
        if user_code == code:
            writing(res)
            return
        else:
            print('code error')
            end_time = time.time()
            inp_time = end_time - start_time
            if inp_time < 5:
                continue
            else:
                code = make_code(4)
                print(code)
                start_time = time.time()
随机验证码
    2、编写认证功能装饰器,同一用户输错三次密码则锁定5分钟,5分钟内无法再次登录
        ####认证装饰器#####

import json
import time

count = 0
name = input('Enter your username: ').strip()
pwd = input('Enter your password: ').strip()

def auth(name,pwd):
    def auth1(func):
        def wrapper(*args,**kwargs):
            ####################################
            with open('%s.json' %name,'r',encoding='utf-8') as f:
                res = json.load(f)
            if name != res['name']:
                print('name illegal')
                exit()
            #####这里可以单独写一个用户名验证模块#############
            else:
                now_time = time.time()
                if res['time'] == 0 or now_time - res['time'] > 300:
                    while count < 3:
                        if pwd == '123':
                            print('login successful')
                            res = func(*args,**kwargs)
                            return res
                        else:
                            print('pwd illegal')
                            count += 1
                            if count == 3:
                                ############################################
                                with open('%s.json' %name,'r+',encoding='utf-8') as f:
                                    clock_time = time.time()
                                    res['time'] = clock_time
                                    json.dump(res,f)
                                print('pwd illega,locking five minute')
                                ############这里可以单独写一个文件修改模块#############
                                return
                            else:
                                continue
                else:
                    print('user locking,please waiting')
        return wrapper
    return auth1
认证装饰器

 

posted @ 2018-04-09 19:05  木夂口  阅读(117)  评论(0编辑  收藏  举报
levels of contents