模拟论坛登录
Posted nmucomputer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟论坛登录相关的知识,希望对你有一定的参考价值。
结构
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
以上是关于模拟论坛登录的主要内容,如果未能解决你的问题,请参考以下文章
《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段
求真正有效的可以模拟登录新浪微博的java代码,后续可以用Jsoup进行抓取。急急!!登录成功马上给分!
junit4单元测试--web项目中模拟登录会话,做全流程测试