hashlib 和 logging 和栈

#  密码 密文 摘要算法 hashli
# 摘要算法
# a = alex3714 ===摘要==> 174692740812ab238919
# alex3714 ===摘要==> 174692740812ab238919

# 登录 md5 sha
# 密码不能使用明文存储
# 密文存储 摘要算法
# 校验文件一致性 md5
# 网络的上传下载
# 保证多台机器状态的一致

import hashlib
# md5_obj = hashlib.md5()
# # md5算法的对象
# md5_obj.update(b'alex3714') # 使用md5摘要算法对'alex3714'进行摘要
# res = md5_obj.hexdigest() # 获取摘要之后的结果
# print(res,type(res)) #aee949757a2e698417463d47acac93df 32位

# user = input('user : ')
# passwd = input('passwd : ')
# md5_obj = hashlib.md5()
# md5_obj.update(passwd.encode('utf-8'))
# passwd = md5_obj.hexdigest()
# if user == 'alex' and passwd == 'aee949757a2e698417463d47acac93df':
# print('登陆成功')

# md5_obj = hashlib.sha1()
# # md5算法的对象
# md5_obj.update(b'alex3714') # 使用md5摘要算法对'alex3714'进行摘要
# res = md5_obj.hexdigest() # 获取摘要之后的结果
# print(res) #8a003668a9c990f15148f9e4046e1410781533b6 40

# 相同的字符串使用相同的算法 在任何时候 得到的结果都是一致的


# 全世界的md5算法都是一样的
# 123456 111111
# md5_obj = hashlib.md5()
# md5算法的对象
# md5_obj.update(b'123456') # 使用md5摘要算法对'alex3714'进行摘要
# res = md5_obj.hexdigest() # 获取摘要之后的结果
# print(res,type(res)) #aee949757a2e698417463d47acac93df 32位
# 123456 e10adc3949ba59abbe56e057f20f883e
# 撞库

# 加盐
# md5_obj = hashlib.md5('盐'.encode('utf-8'))
# # md5算法的对象
# md5_obj.update(b'alex3714') # 使用md5摘要算法对'alex3714'进行摘要
# res = md5_obj.hexdigest() # 获取摘要之后的结果
# print(res,type(res))
#aee949757a2e698417463d47acac93df 32位
#0e249b9c16ea1d840ce700587cada978

# 动态加盐 _ 校园管理系统
# username = 'alex' # alex alex3714
# # egon egon5068
# md5_obj = hashlib.md5(username.encode('utf-8')+'盐'.encode('utf-8'))
# md5_obj.update(b'alex3714')
# res = md5_obj.hexdigest()
# print(res)

# 校验文件一致性
# with open('userinfo','rb') as f:
# md5_obj = hashlib.md5()
# md5_obj.update(f.read())
# res = md5_obj.hexdigest()
# print(res)
#
# with open('userinfo','rb') as f:
# md5_obj = hashlib.md5()
# for line in f:
# md5_obj.update(line) # update操作可以在hexdigest之前执行多次
# # 分次对一个长字符串进行摘要
# res = md5_obj.hexdigest() # 结果是对整个长字符串的摘要结果
# print(res)

#56fc9aa78c2dd71d547988b24bec198a

# md5_obj = hashlib.md5()
# md5_obj.update(b'aaabbb')
# res = md5_obj.hexdigest()
# print(res) #6547436690a26a399603a7096e876a2d
#
# md5_obj = hashlib.md5()
# md5_obj.update(b'aa')
# md5_obj.update(b'abbb')
# res = md5_obj.hexdigest()
# print(res) #6547436690a26a399603a7096e876a2d

# logging 模块
# logging
# 操作日志的模块
# 什么叫日志
# 给用户看的
# 用户的重要行为
# 登录 涉及安全
# 账单 钱
# 给开发和运维和测试人员看的
# 自测 logging.debug('一些中间结果')
# 测试 1++++++1
# 运维
# 记录
# 打印在屏幕上
# 写入文件里
# logging的优势
# 格式更加规范
# 等级更加鲜明

# 简单的配置用法

import logging
# logging.basicConfig(level=logging.DEBUG, # 必须是大写
# format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
# datefmt='%a, %d %b %Y %H:%M:%S')
# # filename='test.log',
# # filemode='a')
# logging.debug('debug message') # 调试
# logging.info('info message') # 信息
# logging.warning('warning message') # 警告
# logging.error('error message') # 错误
# logging.critical('critical message') # 严重错误


# # 使用logger对象的用法
# # import logging
# # 首先创建一个logger对象
# logger = logging.getLogger()
#
# #创建一个格式
# fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 创建格式
#
# # 创建一个 文件句柄 控制向哪个文件中输出 用什么格式
# fh = logging.FileHandler('test3.log',encoding='utf-8') # 创建文件句柄
# fh.setFormatter(fmt) # 把文件句柄和格式连接
#
# # 创建一个 屏幕句柄 控制向屏幕输出 用什么格式
# sh = logging.StreamHandler()
# sh.setFormatter(fmt) #把屏幕局输出 和 格式绑在一起
#
# # 将logger对象和文件句柄,屏幕句柄绑在一起
# logger.addHandler(fh)
# logger.addHandler(sh)
# logger.setLevel(logging.DEBUG) # 首先必须要整体对logger进行设置
# sh.setLevel(logging.INFO) # 设置等级
# fh.setLevel(logging.WARNING) # 设置等级
# logger.debug('logger debug message')
# logger.info('logger info message')
# logger.warning('logger warning message')
# logger.error('logger error message')
# logger.critical('logger critical message')


# 处理 字典 写入文件 等
# .py 里面的所有值 都不需要进行转换或者处理 直接当做变量使用
#通用性不高
# 文本格式 key = value
# 都要进行文件处理 _ 通用
# ini
# [北京校区] # section
# 课程 = python,linux # option
# python讲师 = egon,yuanhao,nezha,boss_gold
# linux讲师 = 李导,何首乌
# [上海校区]
# 课程 = go,linux
# python讲师 = egon
# linux讲师 = 李导,何首乌

# import configparser
# config = configparser.ConfigParser()
# # config 是一个操作配置文件的对象
# config["DEFAULT"] = {'ServerAliveInterval': '45',
# 'Compression': 'yes',
# 'CompressionLevel': '9',
# 'ForwardX11':'yes'
# }
# config['bitbucket.org'] = {'User':'hg'}
# config['topsecret.server.com'] = {'Host Port':'50022',
# 'ForwardX11':'no'}
# with open('example.ini', 'w') as configfile:
# config.write(configfile)
#
# import configparser
#
# config = configparser.ConfigParser()
# # print(config.sections()) # []
# config.read('example.ini')
# print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
#
# print('bytebong.com' in config) # False
# print('bitbucket.org' in config) # True
# print(config['bitbucket.org']["user"]) # hg
# print(config['DEFAULT']['Compression']) #yes
# print(config['topsecret.server.com']['ForwardX11']) #no
#
# print(config['bitbucket.org'],6666) #<Section: bitbucket.org>
#
# for key in config['bitbucket.org']: # 注意,有default会默认default的键
# print(key,8)
#
# print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
# print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
# print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value


# import configparser
# config = configparser.ConfigParser()
# config.read('example.ini')
# config.add_section('yuan')
# config.remove_section('bitbucket.org')
# config.remove_option('topsecret.server.com',"forwardx11")
# config.set('topsecret.server.com','k1','11111')
# config.set('yuan','k2','22222')
# config.write(open('example.ini', "w"))

# 栈 进阶
# 队列 先进先出
# 栈 先进后出
# 压栈

# l = [menu]
# while l:
# menu = l.pop()
# for key in menu:
# print(key)
# inp_key = input('>>>')
# if inp_key in menu and menu[inp_key]:
# l.append(menu[inp_key])
posted @ 2018-03-14 18:51  xuerh  阅读(166)  评论(0编辑  收藏  举报