装饰器应用之登录

# filename = "E:\\pythonStudy\\jingdong.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen:1234\n")
    # print("写入成功!")

# filename = "E:\\pythonStudy\\weixin.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen2:1234\n")
    # print("写入成功!")

login_stats = False
choice=""

content='''*******************************请 选 择 :***********************************
        1:home 
        2:finance
        3:book
******************************************************************************'''

def chose_auth_type(auth_type="jingdong"):
    def login(function_name):
        def inner():
            global login_stats
            if login_stats is False:
                if input_information(auth_type):
                    print("登录成功!ovo")
                    login_stats = True
                    function_name()
                else:
                    print("登录失败!")
                    print()
            else:
                function_name()
        return inner
    return login

@chose_auth_type("jingdong")
def home():
    print("Welcome to home page!")

@chose_auth_type("weixin")
def finance():#金融
    print("Welcome to finance page!")

@chose_auth_type()
def book():
    print("Welcome to book page!")

def input_information(auth_type):
    username=input("username:").strip()
    passwords=input("passwords:").strip()
    if auth_type == "jingdong":
        print("******************京东账号登录中......*************************************")
        return auth_login(username,passwords,auth_type)
    elif auth_type == "weixin":
        print("******************微信账号登录中......************************************")
        return auth_login(username,passwords,auth_type)

    
def auth_login(username,passwords,auth_type):
#get_information()返回来的值赋给user和password
    rest=get_information(auth_type)
    if username != "q" or passwords != "q":
        if username in rest.keys():
            if rest[username] == passwords:
                return True
            else:
                print()
                print('''*********************用户名或密码错误!请重试!*****************************''')
                input_information(auth_type)
        else:
            print()
            print('''*********************用户名或密码错误!请重试!*****************************''')
            input_information(auth_type)
    else:
        return False

def get_information(auth_type):#根据auth_type参数,从不同的文件中读入用户名和密码
    rest={}
    filename = "E:\\pythonStudy\\"+auth_type+".txt"
    with open(filename) as f:
        for line in f:
            (key, value) = line.strip().split(':')
    #将读取到的一行信息通过“:”分割成为一个列表,之后对应赋值给key,value两个变量
            rest[key] = value
        return rest

while(True):
    print(content)
    choice=input(">>>:")
    if choice == "q":
        print("欢迎再来!=*v*=")
        break
    elif choice == "1":
        home()
    elif choice == "2":
        finance()
    elif choice == "3":
        book()
    else :
        print("输入错误!")
        continue




 

展开阅读全文

没有更多推荐了,返回首页