利用装饰器模拟网页登录机制

#!/usr/bin/env python3

"""
This is a module to implement the login function.
There are three login modes for the module.
You can accept qq, local and guest login.
Different login methods can access different pages,
 after the same page login you can not login again
 """

import user

qq_login_authentication = False  # Record qq login status
loc_login_authentication = False  # Record local login status

def login(arg = 'tourists'):  # arg: decorator parameters to identify login methods
    def login_auth(fun):
        def inter():
            if arg == 'qq':
                global qq_login_authentication
                if not qq_login_authentication: #Query the login status and log in if not logged in
                    for i in range(3):
                        account = input("qq_count:")
                        password = input("qq_password:")
                        if (user.qq_account == account) & (user.qq_password == password):
                            qq_login_authentication = True  #Login successfully, change login status
                            fun()
                            break
                        else:
                            print('erro account,please input again!')
                else:
                    fun()
            elif arg == 'loc':
                global loc_login_authentication
                if not loc_login_authentication:  # Query the login status and log in if not logged in
                    for i in range(3):
                        account = input("loc_count:")
                        password = input("loc_password:")
                        if (user.loc_account == account) & (user.loc_password == password):
                            loc_login_authentication = True  #Login successfully, change login status
                            fun()
                            break
                        else:
                            print('error account,please input again!')
                else:
                    fun()
            elif arg == 'tourists':
                fun()
            else:
                print('error os!')
        return inter
    return login_auth

@login()
def home():
    """This is home page! No login required"""
    print('this is home page!')

@login('qq')
def book():
    """This is book page! Need qq login"""
    print('this is book page!')

@login('loc')
def device():
    """This is book page! Need local login"""
    print('this is device page!')

if __name__  == '__main__':
    home()
    device()
    device()
    book()
    book()
阅读更多
个人分类: python
上一篇python中整形和浮点型的缓存问题
想对作者说点什么? 我来说一句

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

关闭
关闭