# 1、把登录与注册的密码都换成密文形式 import hashlib def register(): while 1: in_name = input('请输入注册账号:').strip() in_pwd = input('请输入注册密码:').strip() with open(r'aaa.txt', mode='rt', encoding='utf-8')as f: for check_info in f: if in_name == check_info.strip().split(':')[0]: print('账号已存在,请重新输入') break else: break dongles = hashlib.md5(in_pwd.encode('utf-8')) in_pwd_hash = dongles.hexdigest() in_info = in_name + ':' + in_pwd_hash + '\n' with open(r'aaa.txt', mode='at', encoding='utf-8')as f: f.write(in_info) print('注册成功,请妥善保管以下注册信息:') print('您的账号:{}'.format(in_name)) print('您的密码:{}'.format(in_pwd)) def login(): tag = 1 while tag: login_name = input('请输入登录账号:') login_pwd = input('请输入登录密码:') dongles = hashlib.md5(login_pwd.encode('utf-8')) login_pwd_hash = dongles.hexdigest() with open(r'aaa.txt', mode='rt', encoding='utf-8')as f: for check_info in f: check_info_list = check_info.strip().split(':') if login_name == check_info_list[0] and login_pwd_hash == check_info_list[1]: print('用户 {} 登录成功'.format(login_name)) tag = 0 break else: print('账号或密码错误,请重新输入')
# 2、文件完整性校验(考虑大文件) import hashlib def md5_check(in_content_path, check_content_hash): with open(in_content_path, mode='rb')as f: all_len = len(f.read()) dongles = hashlib.md5() f.seek(all_len // 4, 0) dongles.update(f.read()) f.seek(all_len // 3, 0) dongles.update(f.read()) f.seek(all_len // 2, 0) dongles.update(f.read()) in_content_hash = dongles.hexdigest() if in_content_hash == check_content_hash: return True else: return False with open(r'bbb.jpg', mode='rb')as f: all_len = len(f.read()) dongles = hashlib.md5() f.seek(all_len // 4, 0) dongles.update(f.read()) f.seek(all_len // 3, 0) dongles.update(f.read()) f.seek(all_len // 2, 0) dongles.update(f.read()) check_content_hash = dongles.hexdigest() res = md5_check(r'bbb - 副本.jpg', check_content_hash) print(res)
# 3、注册功能改用json实现 import json def register(): while 1: in_name = input('请输入注册账号:').strip() in_pwd = input('请输入注册密码:').strip() with open(r'ccc.txt', mode='rt', encoding='utf-8')as f: for check_info in f: check_info = json.loads(check_info) if in_name == check_info.strip().split(':')[0]: print('账号已存在,请重新输入') break else: break in_info = in_name + ':' + in_pwd in_info = json.dumps(in_info) with open(r'ccc.txt', mode='at', encoding='utf-8')as f: f.write(in_info) f.write('\n') print('注册成功,请妥善保管以下注册信息:') print('您的账号:{}'.format(in_name)) print('您的密码:{}'.format(in_pwd))
# 4、项目的配置文件采用configparser进行解析 import configparser config = configparser.ConfigParser() config.read(r'ddd.ini') print(config.sections()) print(config.options('section1')) print(config.items('section1')) print(config.get('section1', 'a1')) print(config.getint('section1', 'a1')) print(config.getfloat('section1', 'a2')) print(config.getboolean('section1', 'a3'))