22加密模块/日志模块
购物程序代码作业
import os
import json
# 获取执行文件所在的路径
current_path = os.path.dirname(__file__)
# 拼接db文件夹的路径
data_path = os.path.join(current_path, 'db')
if not os.path.exists(data_path): # 如果文件夹路径不存在
os.mkdir(data_path) # 创建该文件夹
# 定义一个记录用户是否登录的全局变量
'''不要直接记录整个用户字典 安全性较低 不符合编程原则!!!'''
is_login = {
'username': None # 用户登录成功之后修改为当前用户名
}
# 登录认证装饰器
def login_auth(func_name):
def inner(*args, **kwargs):
if is_login.get('username'):
res = func_name(*args, **kwargs)
return res
else:
print('您还没有登录 请先登录')
login()
return inner
def register():
# 1.获取用户名和密码、确认密码
username = input('username>>>:').strip()
password = input('password>>>:').strip()
confirm_pwd = input('confirm_pwd>>>:').strip()
# 2.先比对两次密码是否一致
if not password == confirm_pwd:
print('两次密码不一致')
return # 结束函数运行(这里也可以在外层弄个大循环)
# 3.拼接用户数据文件路径
file_path = os.path.join(data_path, f'{username}.json') # ...db/tony.json
# 4.校验用户名是否已存在>>>:校验文件路径是否存在
if os.path.isfile(file_path): # 文件存在了 就是用户存在了 那用户名肯定就存在了
print('用户名已存在')
return # 结束函数运行
# 5.初始化用户数据
user_dict = {'username': username, 'password': password, 'balance': 15000, 'shop_car': {}}
# 6.序列化到文件中
with open(file_path, 'w', encoding='utf8') as f:
json.dump(user_dict, f, ensure_ascii=False)
print(f'{username}注册成功')
def login():
# 1.获取用户名和密码
username = input('username>>>:').strip()
password = input('password>>>:').strip()
# 2.拼接用户文件路径
file_path = os.path.join(data_path, f'{username}.json')
# 3.判断文件路径是否存在 如果存在表示有这个用户 不存在表示没有
if not os.path.isfile(file_path):
print('用户名不存在')
return
# 4.用户名存在 获取用户详细数据 再比对密码
with open(file_path, 'r', encoding='utf8') as f:
user_dict = json.load(f) # {'username':'jason','password':123}
# 5.判断密码是否正确
if password == user_dict.get('password'):
print('登录成功')
# 6.修改全局字典
is_login['username'] = username
else:
print('密码错误')
@login_auth
def add_shop_car():
"""
购物车数据格式 {'极品木瓜':[个数,单价]}
"""
# 1.定义购物车数据(代码直接写死 也可以通过专门的商品文件便于后期维护更新)
# 11.创建一个临时存储商品数据的字典
temp_shop_car = {} # {'极品木瓜':[10,22],'印度飞饼':[100,3]}
# 2.商品可以反复购买 所以这里加循环
while True:
# 3.循环打印商品信息供用户选择 最好给商品添加编号
for i, j in enumerate(good_list): # i=0 j=['挂壁面', 3]enumerater
print("商品编号:%s | 商品名称:%s | 商品单价:%s" % (i, j[0], j[1]))
# 4.获取用户想要购买的商品编号
choice = input('请输入您想要购买的商品编号或者输入y退出添加>>>:').strip()
# 12.如果是y那么修改用户购物车数据保存
if choice == 'y':
file_path = os.path.join(data_path, '%s.json' % is_login.get('username'))
with open(file_path, 'r', encoding='utf8') as f:
user_dict = json.load(f)
# 修改用户购物车数据(不能直接替换 还应该考虑购物车内是否已经存在数据)
real_shop_car = user_dict.get('shop_car') # {'极品木瓜':[1,22]}
for good_name in temp_shop_car:
if good_name in real_shop_car:
real_shop_car[good_name][0] += temp_shop_car[good_name][0]
else:
real_shop_car[good_name] = temp_shop_car[good_name]
user_dict['shop_car'] = real_shop_car
# 重新写入文件
with open(file_path, 'w', encoding='utf8') as f:
json.dump(user_dict, f, ensure_ascii=False)
print('商品数据添加购物车成功')
break
# 5.校验商品编号是否是纯数字
if not choice.isdigit():
print('商品编号必须是数字')
continue
# 6.将用户输入的字符串类型编号转成整数类型便于后续索引取值
choice = int(choice)
# 7.校验数字范围是否在商品列表的索引范围内
if not choice in range(len(good_list)):
print('商品编号超出了现有的范围')
continue
# 8.获取用户想要购买的商品数据
target_good_list = good_list[choice]
target_good_name = target_good_list[0]
target_good_price = target_good_list[1]
# 9.获取用户想要购买的当前商品的数量
target_num = input('请输入你想要购买的数量>>>:').strip()
if target_num.isdigit(): # 数量在付钱之前 你想要多少都可以
target_num = int(target_num)
# 10.确定了商量和数量 添加购物车 但是要考虑用户还可能购买其他商品
"""临时购物车字典里面可能已经存在一些商品 也可能是第一次购买"""
if target_good_name not in temp_shop_car: # {'极品木瓜':[1,666]}
temp_shop_car[target_good_name] = [target_num, target_good_price]
else:
temp_shop_car[target_good_name][0] += target_num # {'极品木瓜':[1+9,666]}
else:
print('商品的个数必须是纯数字')
@login_auth
def clear_shop_car():
# 1.先获取用户购物车数据
file_path = os.path.join(data_path, '%s.json'%is_login.get('username'))
# 2.获取用户购物车
with open(file_path, 'r', encoding='utf8') as f:
user_dict = json.load(f)
shop_car = user_dict.get('shop_car') # {'商品名称':[个数,单价]}
# 定义一个总价
total_money = 0
# 3.计算商品总价(只需要字典的value 可以使用字典的内置方法values())
for good_num,good_price in shop_car.values(): # [个数,单价]
total_money += good_num * good_price
# 4.有了总价还需要判断用户能否买得起
balance = user_dict.get('balance')
if balance > total_money:
rest_money = balance - total_money
user_dict['balance'] = rest_money
# 5.扣钱完毕 清空购物车
user_dict['shop_car'] = {}
# 6.保存数据
with open(file_path,'w',encoding='utf8') as f:
json.dump(user_dict, f, ensure_ascii=False)
print(f'结算成功 今日消费{total_money} 余额{rest_money} 欢迎下次光临!!!')
else:
print('钱不够 你个穷逼!!!')
func_dict = {'1': register, '2': login, '3': add_shop_car, '4': clear_shop_car}
while True:
print("""
1.用户注册
2.用户登录
3.添加购物车
4.结算购物车
""")
choice = input('请输入您想要执行的功能编号>>>:').strip()
if choice in func_dict:
func_name = func_dict.get(choice)
func_name()
else:
print('功能编号不存在')
hashlib加密模块(ps:常见的加密算法>>>:md5 base64 hmac sha256 sha1)
# 什么是加密
将明文(人看得懂)数据通过一些手段变成密文数据(人看不懂)
密文数据的表现形式一般都是一串没有规则的字符串
# 加密算法
加密算法有很多种>>>(将明文变密文的内部规则)
算法的难易程度可以根据产生密文的长短来判断
越长意味着算法越复杂
# 什么时候使用加密
涉及到隐私数据的时候 应该考虑使用加密
最为常见的就是对用户的密码加密 防止密码泄露
# 基本使用(仅仅学习一个md5够用了,剩下的了解即可)
import hashlib
# 1.指定算法>>>:md5算法(最为常见 一般的业务需求足够了)
# md5 = hashlib.md5()
# 2.将明文数据传递给算法对象
# md5.update(b'hello') # 只能接收bytes类型 这里以hello为例子
"""如果字符串中是纯数字和英文 那么直接在前面加b转成bytes类型"""
# 3.获取加密之后的密文数据
# res = md5.hexdigest()
# print(res) # 5d41402abc4b2a76b9719d911017c592
# 在传入数据的时候 只要内容一致 那么算法的结果肯定一致(不管是一次传入,还是多次!)
md5 = hashlib.md5()
# md5.update(b'hello')
# md5.update(b'world')
# md5.update(b'jason')
# print(md5.hexdigest()) # 8faebe82e744992e51c86845cac3e1b7
md5.update(b'helloworldjason')
print(md5.hexdigest()) # 8faebe82e744992e51c86845cac3e1b7
记忆这四句代码即可
加密补充
1.加密之后的结果是无法直接反解密的
8faebe82e744992e51c86845cac3e1b7
# 所谓的反解密其实是暴力破解>>>:反复的猜
"""
md5解密内部本质
提前想好很多可能是密码的组合
123 自己加密
321 自己加密
222 自己加密
{'密文1':123,'密文2':321}
"""
2.加盐处理
增加破解的难度
import hashlib
md5 = hashlib.md5()
# 加盐处理(添加一些额外的干扰项)
md5.update('你追我'.encode('utf8')) #增加干扰项
md5.update(b'123')
print(md5.hexdigest()) # ce850e70febde5f3506fec0479dc0f963.动态加盐
干扰项动态变化
可以是用户名的一部分 也可以当前时间...
比如time.time()等(干扰性更强)
'''日志模块就是在程序的各个环境记录 便于后续的查看'''
# 针对日志模块 我们只需要听流程思路 最后CV即可 无需详细记忆
# 1.日志等级
import logging
# 日志按照重要程度分为五个级别:默认只有达到warning警告级别及以上才会记录日志
# logging.debug('debug message') # 10 大大小小都记录
# logging.info('info message') # 20 日常记录
# logging.warning('warning message') # 30 警告记录
# logging.error('error message') # 40 出错记录
# logging.critical('critical message') # 50 系统快瘫痪记录
# 2.基本使用
import logging
file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf-8', )
# 所有的格式不需要记忆 后续几乎都是拷贝加修改(下面的简单了解即可)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[file_handler, ],
level=logging.ERROR
)
logging.error('FBI警告宇帆 少看点 注意身体')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人