Python实战之用类的静态方法实现登录验证
#!usr/bin/env Python3 # -*-coding:utf-8-*- __author__="William" #define a class,just to learn to use class. class Login(object): #those variable just use to be a flag. count = 3 Flag = 1 wrong_user_flag = 1 wrong_user = '' #define a staticmethod function,just like a normal function to login @staticmethod def login(): while Login.count > 0: if Login.wrong_user_flag == 0: username = Login.wrong_user else: username = input("Please input your username:>>>") Login.wrong_user = username password = input("Pleasw input your password:>>>") #call verify function Login.verify(username,password) Login.count -= 1 else: if Login.Flag != 1: #add the wrong password user to black user list. with open("black_user.txt",'a') as fp: fp.write("\n") fp.write(username) fp.write("\n") #define a staticmethod to verify the user info. @staticmethod def verify(user_name,password): #with to use to open a file. with open("black_user.txt", 'r') as fp: data = ''.join(fp.readlines()).split('\n') if user_name in data: print("Your are locked") Login.count = 0 else: if user_name == "William" and password =="123": print("Success,welcome to login") Login.Flag = 1 Login.count = 0 else: print("Wrong username or password") Login.Flag = 0 Login.wrong_user_flag = 0 if __name__=='__main__': #To make a instance. u1 = Login() #to call login u1.login()
posted on 2017-06-23 10:32 William126 阅读(226) 评论(0) 编辑 收藏 举报