作业:
1、把登录与注册的密码都换成密文形式
import hashlib
pwd = input('请输入密码:').strip()
with open('db.txt','a',encoding='utf-8') as f:
hash1 = hashlib.md5(pwd.encode('utf-8'))
print(hash1.hexdigest(),type(hash1.hexdigest()))
f.write(hash1.hexdigest() + '
')
2、文件完整性校验(考虑大文件)
import hashlib
def copy():
with open('db.txt','rb') as f1,
open('db1.txt','wb') as f2:
f2.write(f1.read())
copy()
with open('db.txt','rb') as f:
d = []
a = f.read(10)
while a:
m = hashlib.md5(a)
d.append(m.hexdigest())
f.seek(10, 1)
a = f.read(10)
print(d)
with open('db1.txt','rb') as f:
d1 = []
a = f.read(10)
while a:
m = hashlib.md5(a)
d1.append(m.hexdigest())
f.seek(10,1)
a = f.read(10)
print(d1)
for i in range(len(d)):
if d[i] != d1[i]:
print('文件不完整')
break
else:
print('文件完整')
3、注册功能改用json实现
import json,hashlib
def register():
user = input('请输入账号:').strip()
pwd = input('请输入密码:').strip()
pwd1 = input('请确认密码:').strip()
if pwd == pwd1:
import hashlib
m = hashlib.md5(pwd.encode('utf-8'))
m = m.hexdigest()
with open('db.txt','a',encoding='utf-8') as f:
json.dump('{}:{}:{}'.format(user,m,0),f)
print('注册成功')
else:
print('密码不一致')
register()
4、项目的配置文件采用configparser进行解析
import configparser as conf
settings = conf.ConfigParser()
settings.read('conf.txt')
print(settings.sections())
print(settings.items('section1'))