• 模拟论坛登录


    结构

     starts.py

    # Author:Winter Liu is coming!
    # 启动文件
    import os
    import sys
    import json
    
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))
    sys.path.append(BASE_PATH)
    
    from conf import settings
    from core import src
    
    # 创建用户登录信息文件
    with open(settings.STATUS_PATH, "w") as fp:
        json.dump({"status": False, "username": "", "userpwd": ""}, fp)
    
    # 监测用户信息文件是否存在,不存在则使用json模式创建一个
    if not os.path.exists(settings.USER_PATH):
        with open(settings.USER_PATH, "w") as fp:
            json.dump({}, fp)
    
    # 只能本模块启动
    if __name__ == '__main__':
        src.start()

    settings.py

    import os
    
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))
    STATUS_PATH = BASE_PATH + "/" + "db" + "/" + "status"
    USER_PATH = BASE_PATH+"/"+"db"+"/"+"user_data"

    src.py

    # Author:Winter Liu is coming!
    # 主逻辑
    import json
    from lib import common
    from conf import settings
    
    
    def start():
        while True:
            print("""请输入数字1-8:
            1. #### 登录
            2. #### 注册账号
            3. #### 文章
            4. #### 评论
            5. #### 日记
            6. #### 收藏
            7. #### 注销
            8. #### 退出""")
            choice = input(">>>").strip()
            if choice == "1":
                login()
            elif choice == "2":
                register()
            elif choice == '3':
                article()
            elif choice == '4':
                comment()
            elif choice == '5':
                diary()
            elif choice == "6":
                favorite()
            elif choice == "7":
                log_out()
            elif choice == '8':
                break
    
    
    @common.auth
    def login():
        pass
    
    
    def register():
        with open(settings.USER_PATH, "r") as f:
            data = json.load(f)
        while True:
            print("开始注册,用户名由数字和字母组成,密码长度为6—14位!")
            username = input("请输入用户名:").strip()
            if not username.isalnum():
                continue
            if username in data:
                print("用户名已存在!")
                continue
            userpwd = input("请输入密码:").strip()
            if len(userpwd) < 4 or 14 < len(userpwd):
                continue
            print("用户加入前:", data)
            data[username] = userpwd
            print("用户加入后:", data)
            # 注意,重写用户文件
            with open(settings.USER_PATH, "w") as f:
                json.dump(data, f)
            break
        f.close()
    
        print("注册完成")
    
    
    @common.auth
    def article():
        print("欢迎访问文章页面!")
    
    @common.auth
    def comment():
        print("欢迎访问评论页面!")
    
    @common.auth
    def diary():
        print("欢迎访问日记页面!")
    
    @common.auth
    def favorite():
        print("欢迎访问收藏页面!")
    
    def log_out():
        with open(settings.STATUS_PATH, "w") as fp:
            json.dump({"status": False, "username": "", "userpwd": ""}, fp)
        print("退出登录!")

    common.py

    # Author:Winter Liu is coming!
    import json
    from conf import settings
    
    
    # 装饰器,登录和登录监测,通过文件检测登录状态和用户名正确状况
    def auth(f):
        def inner(*arg, **kwargs):
            with open(settings.STATUS_PATH, "r") as fps:
                status_dict = json.load(fps)
            if status_dict["status"]:
                print("已登录")
                ret = f(*arg, **kwargs)
                return ret
            else:
                if user_login():
                    ret = f(*arg, **kwargs)
                    return ret
                else:
                    print("用户名或密码错误三次!")
    
        return inner
    
    
    # 登录
    def user_login():
        with open(settings.USER_PATH, "r") as fp:
            user_data = json.load(fp)
        for i in range(3):  # 三次输入限制
            username = input("请输入用户名:").strip()
            userpwd = input("请输入密码:").strip()
            if username in user_data and user_data[username] == userpwd:
                print("登录成功")
                status_dict = {"status":True}
                status_dict["username"] = username
                status_dict["userpwd"] = userpwd
                with open(settings.STATUS_PATH, "w") as fp:
                    json.dump(status_dict, fp)
                return True
            else:
                print("用户名或者密码错误!")
        else:
            return False
  • 相关阅读:
    滚动新闻的实现
    使用DLC(Dynamic Loader Compatibility)函数提取iPhone当前运营商信号的强度
    iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
    如何在Typora中使用流程图
    连接池的使用场景
    Mybatisplus入门教程(一)
    mybatis第一篇
    Mybatis需要注意的细节
    测试代码着色插件
    (转)企业Elearning的产业链与业务模式
  • 原文地址:https://www.cnblogs.com/nmucomputer/p/12752581.html
Copyright © 2020-2023  润新知