码迷,mamicode.com
首页 > 其他好文 > 详细

作业4.3

时间:2018-04-09 21:08:43      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:war   error   def   年龄   auth   turn   min   str   分享   

技术分享图片
    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
认证装饰器

 

作业4.3

标签:war   error   def   年龄   auth   turn   min   str   分享   

原文地址:https://www.cnblogs.com/guodengjian/p/8762136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!