用md5进行进行加密,为了防止撞库,所以一定要加盐,因为加密后不可以反解,因此进行校对时可以使用密文进行,还需注意的是md5加密的是字节,所以需要用encode进行编码
加密实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import hashlib salt = b"asdfghjkl;ei" def func(pwd): # 实例化对象 obj = hashlib.md5(salt) # 写入加密字节 obj.update(pwd.encode("utf-8")) # 获取密文 return obj.hexdigest() name = input(">>>") pwd = input(">>>") if name == "123" and func(pwd) == "fb5e8b7ebcd71d0ac0ecf803ff8aba74": print("登陆成功")
日志文件
logger = logging.basicConfig(filename='xxxxxxx.txt', format='%(asctime)s - %(name)s - (levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=30) # 参数设置 # 等级 # logging.debug('x1') # 10 # logging.info('x2') # 20 # logging.warning('x3') # 30 # logging.error('x4') # 40 # logging.critical('x5') # 50 # logging.log(10,'x6')
# 自定义日志
logging.error('x4')
将日志写入一个文件中
import logging import traceback logger = logging.basicConfig(filename='xxxxxxx.txt', format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=30) def func(): try: a = a +1 except Exception as e: # 获取当前错误的堆栈信息 msg = traceback.format_exc() logging.error(msg) func()
将日志写入多个文件中
import logging # 创建一个操作日志的对象logger(依赖FileHandler) file_handler = logging.FileHandler('log1.log', 'a', encoding='utf-8') file_handler.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")) logger1 = logging.Logger('s1', level=logging.ERROR) logger1.addHandler(file_handler) logger1.error('1') # 在创建一个操作日志的对象logger(依赖FileHandler) file_handler2 = logging.FileHandler('log2.log', 'a', encoding='utf-8') file_handler2.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")) logger2 = logging.Logger('s2', level=logging.ERROR) logger2.addHandler(file_handler2) logger2.error('2')