1.把登录与注册的密码都换成密文形式
import hashlib
# 0 注册功能
def register():
print('注册功能执行中...')
while True:
username = input('请输入用户名:').strip()
with open(r'db.txt', mode='rt', encoding='UTF-8') as f:
for line in f:
if username in line:
user_data = line.strip().split(':')
if user_data:
print('当前用户已存在,请重新输入!')
continue
password = input('请输入密码:').strip()
re_password = input('请确认密码:').strip()
if password == re_password:
m1 = hashlib.md5()
m1.update(password.encode('UTF-8'))
res = m1.hexdigest()
print(res)
with open(r'db.txt', mode='at', encoding='UTF-8') as f:
f.write(f'{username}:{res}\n')
print(f'用户[{username}]注册成功!')
break
else:
print('两次密码不一致,请重新输入!')
# 1 登录功能
def login():
print('登录功能执行中...')
while True:
imp_usr = input('请输入用户名:').strip()
with open(r'db.txt', mode='rt', encoding='UTF-8') as f1:
for line in f1:
if imp_usr in line:
user_data = line.strip().split(':')
if not user_data:
print('当前用户不存在,请重新输入!')
continue
imp_pwd = input('请输入密码:').strip()
with open("db.txt", "r", encoding="utf-8")as f2:
m2 = hashlib.md5()
m2.update(imp_pwd.encode("utf-8"))
res = m2.hexdigest()
for line in f2:
username, password = line.strip().split(":")
if imp_usr == username:
if res == password:
print("登录成功")
else:
print('密码错误,登录失败!')
register()
login()
2.文件完整性校验(考虑大文件)
import hashlib
def upload_file():
with open("upload_file.txt", mode="rt", encoding='UTF-8')as f1 :
l1 = [0, 100, 200]
m = hashlib.md5()
for i in l1:
f1.seek(i,0)
res = f1.read(5)
m.update(res.encode("utf-8"))
res = m.hexdigest()
return res
def download_file():
with open("download_file.txt", mode="rt", encoding='UTF-8')as f2 :
l2 = [0, 100, 200]
m = hashlib.md5()
for i in l2:
f2.seek(i,0)
msg = f2.read(5)
m.update(msg.encode("utf-8"))
res = m.hexdigest()
if res == upload_file():
print("文件完整")
else:
print("文件不完整")
download_file()
3.注册功能改用json实现
import json
import os
def register():
print('注册功能执行中...')
while True:
username = input('请输入用户名:').strip()
with open(r'db.txt', mode='rt', encoding='UTF-8') as f:
for line in f:
if username in line:
user_data = line.strip().split(':')
if user_data:
print('当前用户已存在,请重新输入!')
continue
password = input('请输入密码:').strip()
re_password = input('请确认密码:').strip()
if password == re_password:
with open(r'db.txt', mode='at', encoding='UTF-8') as r_f, \
open(r'db.json.swap', mode='wt', encoding='UTF-8') as w_f:
r_f.seek(0, 0)
if r_f.read() == '':
r_f.write(json.dumps({username: password}))
w_f.close()
os.remove('db.json.swap')
print("注册成功")
return
r_f.seek(0, 0)
load_res = json.loads(r_f.read())
if username in load_res:
print('用户名已存在')
w_f.close()
os.remove('db.json.swap')
return
load_res[username] = password
json.dump(load_res, w_f)
print("注册成功")
os.remove('db.json')
os.rename('db.json.swap', 'db.json')
else:
print('两次密码不一致,请重新输入!')
register()
4.项目的配置文件采用configparser进行解析
[settings]
# 获取项目根目录Novel - 1的根目录
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
# 获取db目录的路径
DB_PATH = os.path.join(BASE_PATH, 'db')
# 获取db.txt的根目录
DB_TXT_PATH = os.path.join(DB_PATH, 'db.txt')
# story_class文件目录路径
STORY_PATH = os.path.join(DB_PATH, 'story_class.txt')
# 小说存放目录
FICTION_DIR = os.path.join(DB_PATH, 'fictions')
# 日志文件的路径
LOG_PATH = os.path.join(BASE_PATH, 'log', 'log.txt')