1、把登录与注册的密码都换成密文形式
import hashlib
def select(username):
with open('db.txt','rt',encoding='utf8') as f:
for line in f:
if username in line:
user_data = line.strip().split(':')
return user_data
def save(username,password):
with open('db.txt','at',encoding='utf8') as f:
f.write('{}:{}
'.format(username,password))
def register():
print('账号注册中。。。')
while True:
username = input('请输入你要注册的账号:').strip()
user_data=select(username)
if user_data:
print('你输入的账号已存在,请重新输入。')
continue
while True:
pwd = input('请为你的账号设置密码:').strip()
pwd1 = input('请重新输入确认密码:').strip()
if pwd == pwd1:
print('账号[ %s ]注册成功。'%username)
m=hashlib.md5(pwd.encode('utf8'))
password=m.hexdigest()
save(username,password)
return
else:
print('两次输入的密码不一致,请重新设置密码。')
def login():
print('账号登录中。。。')
while True:
username = input('请输入你的账号:').strip()
user_data = select(username)
if not user_data:
print('你输入的账号不存在,请重新输入。')
continue
while True:
pwd = input('请输入你的密码:').strip()
m = hashlib.md5(pwd.encode('utf8'))
password = m.hexdigest()
if password == user_data[1]:
print('登录成功。')
return
else:
print('密码错误,请重新输入。')
register()
login()
2、文件完整性校验(考虑大文件)
import os
import random
import hashlib
def check_integrity(src_file,download_file):
total_size = os.path.getsize(src_file)
for i in range(10):
with open(src_file,'rb') as f1,open(download_file,'rb') as f2:
num = random.randint(0,total_size)
f1.seek(num)
f2.seek(num)
content1,content2 = f1.read(1024),f2.read(1024)
m1,m2 = hashlib.md5(content1),hashlib.md5(content2)
res1,res2 = m1.hexdigest(),m2.hexdigest()
if res1 != res2:
return '下载的文件是不完整的'
return '下载的文件是完整的'
3、注册功能改用json实现
import os
import json
import hashlib
def select(username):
user_data=[]
with open('db.json','rt',encoding='utf8') as f:
all_user = json.load(f)
if username in all_user:
user_date=[username,all_user.get(username)]
return user_date
def save(username,password):
with open('db.json', 'rt', encoding='utf8') as f1,
open('db.json.a.swap','wt',encoding='utf8') as f2:
all_user = json.load(f1)
all_user[username]=password
json.dump(all_user,f2)
os.remove('db.json')
os.rename('db.json.a.swap','db.json')
def register():
print('账号注册中。。。')
while True:
username = input('请输入你要注册的账号:').strip()
user_data=select(username)
if user_data:
print('你输入的账号已存在,请重新输入。')
continue
while True:
pwd = input('请为你的账号设置密码:').strip()
pwd1 = input('请重新输入确认密码:').strip()
if pwd == pwd1:
print('账号[ %s ]注册成功。'%username)
m=hashlib.md5(pwd.encode('utf8'))
password=m.hexdigest()
save(username,password)
return
else:
print('两次输入的密码不一致,请重新设置密码。')
register()
4、项目的配置文件采用configparser进行解析
import configparser
conf = configparser.ConfigParser()
conf.read('config.ini')
# 1、获取sections
print(conf.sections())
# 2、获取某一section下的所有options
print(conf.options('section1'))
# 3、获取items
print(conf.items('section1'))
# 4、
res=conf.get('section1','user')
print(res,type(res))
res=conf.getint('section1','age')
print(res,type(res))
res=conf.getboolean('section1','is_admin')
print(res,type(res))
res=conf.getfloat('section1','salary')
print(res,type(res))