利用装饰器模拟博客园登陆

Posted duyu123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用装饰器模拟博客园登陆相关的知识,希望对你有一定的参考价值。

import os

# 登录状态
status_dic = 
    username: None,
    status: False

flag = True

# 用户名,密码 以字典形式打开
def user_pwd():
    with open("user", encoding="utf-8") as f:
        dic = 
        for i in f:
            li = i.strip().split("|")
            dic[li[0].strip()] = li[1]
        return dic

# 登录函数
def login():
    u_p = user_pwd()
    count = 0
    while count < 3:
        username = input("请输入姓名: ").strip()
        password = input("请输入密码: ").strip()
        if username in u_p and password == u_p[username]:
            status_dic["username"] = username
            status_dic[status] = True         # 状态改为True
            print("登陆成功")
            return True
        else:
            print("账号或密码错误,请重新登录")
        count += 1

# 注册函数
def register():
    while 1:
        dict1 = user_pwd()
        username = input("请输入新账号: ").strip()
        password = input("请输新入密码: ").strip()
        if username in dict1:                # 判断用户是否在文件中存在
            print("用户名已存在,请重新输入")
            continue
        elif not username.isalnum():        # 判断用户是否是字母或数字组成
            print("账号含有非法字符,请重新输入")
            continue
        elif 5 < len(password) < 15:         # 限制密码在6-14位之间
            with open("user", encoding="utf-8", mode="a") as f1:
                f1.write(username+"|"+password+"\n")
                print("恭喜!注册成功")
                return True
        else:
            print("密码不合规范,请重新输入")

# 装饰器
def auth(f):            # 限制3-7选项,没有登陆不能访问
    def inner(*args, **kwargs):
        if status_dic["status"]:
            ret = f(*args, **kwargs)
            return ret
        else:
            print("-------登录页面-------")
            login()
            ret = f(*args, **kwargs)
            return ret
    return inner

@auth
def article():
    print(f欢迎status_dic["username"]访问文章页面)
    print("""
    1.写入文件
    2.导入md文件
    """)
    while 1:
        choice = input("请输入要选择的操作:").strip()
        if choice == "1":
            file_name = input("输入文件名:").strip()
            content = input("请输入内容").strip()
            with open(rarticle/+file_name, encoding="utf-8", mode="a") as f1:
                f1.write(content)
                print("写入文章成功")
                break
        if choice == "2":
            with open("函数的进阶.md", encoding="utf-8") as f2,                    open(r"article/"+"函数的进阶.text", encoding="utf-8", mode="w") as f3:
                f3.seek(0)
                for i in f2:
                    f3.write(i)
                print("导入文件成功")
                break
        else:
            print("您输入的不合规范")

@auth
def diary():
    print(f欢迎status_dic["username"]访问日记页面)

@auth
def comment():
    print(f欢迎status_dic["username"]访问评论页面)
    file = os.listdir(os.path.dirname(__file__)+/article)  # 把article文件夹里的文件,变成列表形式
    for index, obj in enumerate(file, 1):                    # 循环列表
        print(index, obj)
    while 1:
        choice = input("请输入要评论的文章的序号:").strip()
        if choice.isdigit():
            choice = int(choice)
            if 0 < choice <= len(file):                          # 限制用户输入序号的范围
                path = os.path.join(os.path.dirname(__file__)+/article, file[choice-1])   # 拼接路径,找到该文件
                with open(path, encoding="utf-8", mode="r+") as f1:
                    ret = f1.read()
                    print(ret)
                    content = input("请输入要评论的内容")
                    sensitive = ["苍老师", "东京热", "武藤兰", "波多野结衣"]         # 此列表为敏感字符
                    for i in range(len(sensitive)):                          # 敏感字符替换等长度*
                        if sensitive[i] in content:
                            content = content.replace(sensitive[i], * * len(sensitive[i]))
                    if "-"*20 not in ret:                             # 判断如果不在这里加上"评论区"三个字
                        f1.write("评论区:" + "\n" + "-"*20 + "\n" + fstatus_dic["username"]:\ncontent + "\n")
                        print("评论成功")
                        return True
                    else:                                  # 否则直接写入评论
                        f1.write(fstatus_dic["username"]:\ncontent + "\n")
                        print("评论成功")
                        return True
            else:
                print("请选择正确的文章序号")
        else:
            print("输入内容含有非法字符")


@auth
def enshrine():
    print(f欢迎status_dic["username"]访问收藏页面)

# 注销
def login_out():
    status_dic[username] = None
    status_dic[status] = False
    print(注销成功)

# 退出程序
def exit_program():
    global flag
    flag = False
    return flag

def run():
    choice_dict = 
        1: login,
        2: register,
        3: article,
        4: diary,
        5: comment,
        6: enshrine,
        7: login_out,
        8: exit_program,
    

    while flag:
        print(‘‘‘
        欢迎来到博客园首页
        1:请登录
        2:请注册
        3:文章页面
        4:日记页面
        5:评论页面
        6:收藏页面
        7:注销
        8:退出程序‘‘‘)
        choice = input(请输入您选择的序号:).strip()
        if choice.isdigit():
            choice = int(choice)
            if 0 < choice <= len(choice_dict):
                choice_dict[choice]()
            else:
                print("您输入的超出范围")
        else:
            print("输入不合规范,重新输入")

if __name__ == __main__:
    run()

 

以上是关于利用装饰器模拟博客园登陆的主要内容,如果未能解决你的问题,请参考以下文章

装饰器的初识

装饰器的初识

Go语言之装饰器

~~函数进阶:装饰器~~

装饰器实现session登陆 验证功能

python,不用装饰器实现登陆器小程序