作业20
1、把登录与注册的密码都换成密文形式
def select(user):
'''
此函数用于获取某个用户的所有信息
:param user: 要获取信息的用户
:return: 列表[user_name,password,balance] or None
'''
with open(settings.DB_USER_PATH, "r", encoding="utf-8") as f:
for line in f:
user_dic = json.loads(line)
if user in user_dic:
return user_dic
def encrypt(info):
import hashlib
m = hashlib.md5()
salt = "嘟嘟喂嘟嘟"
m.update(salt.encode("utf-8"))
m.update(info.encode("utf-8"))
res = m.hexdigest()
return res
def add_data(data):
'''
为user_info.txt添加数据
:param data: 添加的数据
:return:
'''
with open(settings.DB_USER_PATH, "a", encoding="utf-8") as f:
json.dump(data,f)
f.write("
")
def reg_user_check(user_name):
'''
用于查看注册的用户名是否存在
:return: None ----->用户名存在,不能注册
:return: 1------>用户名不存在,可以注册
'''
if not db_handler.select(user_name):
return 1
def register():
print("注册功能执行中,输入q退出")
while 1:
reg_user = input("请输入要注册的用户名:").strip()
if reg_user.upper() == "Q":
break
if not user_interface.reg_user_check(reg_user):
# 返回结果为None则不能注册
print("用户名已存在,请重新输入")
continue
reg_password1 = input("请输入密码:")
reg_password2 = input("请确认密码:")
if reg_password1 == reg_password2:
password = db_handler.encrypt(reg_password1)
db_handler.add_data({reg_user: {"password": password, "balance": 0, "limit": 15000}})
print("注册成功!")
return
else:
print("两次密码不一致,请重新输入!")
continue
def login():
while 1:
login_user = input("请输入用户名:[输入q退出]")
if login_user.upper() == "Q":
break
login_password = input("请输入密码")
if not user_interface.login_check(login_user, login_password):
print("用户名或密码错误")
continue
print("登录成功")
return login_user
2、文件完整性校验(考虑大文件)
import os
import hashlib
check1 = "c30290999e50e12bf4a80f38ffebab2b"
check2 = "c5627a1572d99fc58393c5d13c468819"
check3 = "cde90a7b2b88e13eeb1086789149158a"
check4 = "793a2ef2c686db87556bfca872bdcff3"
list = [check1,check2,check3,check4]
def check(big_file):
size = os.path.getsize(big_file)
global check1, check2, check3, check4
with open(big_file, "rb") as f:
for i in range(1, 5):
pos = size*i // 5
f.seek(pos, 0)
check_point = f.read(100)
m = hashlib.md5()
m.update(check_point)
res = m.hexdigest()
print(res)
if res != list[i-1]:
return 0
else:
return 1
print(check(r'D: oolpythonpython代码练习day23ig_file.txt'))
3、注册功能改用json实现
def select(user):
'''
此函数用于获取某个用户的所有信息
:param user: 要获取信息的用户
:return: 列表[user_name,password,balance] or None
'''
with open(settings.DB_USER_PATH, "r", encoding="utf-8") as f:
for line in f:
user_dic = json.loads(line)
if user in user_dic:
return user_dic
def encrypt(info):
import hashlib
m = hashlib.md5()
salt = "嘟嘟喂嘟嘟"
m.update(salt.encode("utf-8"))
m.update(info.encode("utf-8"))
res = m.hexdigest()
return res
def add_data(data):
'''
为user_info.txt添加数据
:param data: 添加的数据
:return:
'''
with open(settings.DB_USER_PATH, "a", encoding="utf-8") as f:
json.dump(data,f)
f.write("
")
def reg_user_check(user_name):
'''
用于查看注册的用户名是否存在
:return: None ----->用户名存在,不能注册
:return: 1------>用户名不存在,可以注册
'''
if not db_handler.select(user_name):
return 1
def register():
print("注册功能执行中,输入q退出")
while 1:
reg_user = input("请输入要注册的用户名:").strip()
if reg_user.upper() == "Q":
break
if not user_interface.reg_user_check(reg_user):
# 返回结果为None则不能注册
print("用户名已存在,请重新输入")
continue
reg_password1 = input("请输入密码:")
reg_password2 = input("请确认密码:")
if reg_password1 == reg_password2:
password = db_handler.encrypt(reg_password1)
db_handler.add_data({reg_user: {"password": password, "balance": 0, "limit": 15000}})
print("注册成功!")
return
else:
print("两次密码不一致,请重新输入!")
continue
4、项目的配置文件采用configparser进行解析
import configparser
config=configparser.ConfigParser()
config.read('a.cfg')
config.get(["section1","path"])