hashlibloggingconfigparser

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import hashlib

# hash:哈希算法,结果是内存地址
# print(hash('123')) #每次都不一样

'''
hashlib模块,与加密相关,被称作 摘要算法
什么是摘要算法呢?摘要算法又称哈希算法、散列算法。它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)。
    1、是一堆算法的合集,包含很多算法(加密的)
    2、hashlib的过程:将字符串--->数字的过程
    3、在不同的电脑上,hashlib对相同的字符串转化成的数字相同
应用场景:
    密文(密码):将密码用算法加密放置到数据库,每次取出验证
    文件的校验
分类:
    md5:加密算法,常用算法,可以满足一般的常用的需求
    sha:加密算法,级别高一些,数字越大级别越高,加密的效率越低,越安全

# ---------- md5
s1 = '12343254'
ret = hashlib.md5()  # 创建一个md5对象
ret.update(s1.encode('utf-8')) # 调用此update方法对参数进行加密 bytes类型
print(ret.hexdigest())  # 得到加密后的结果 定长

# 无论多少数据,加密后的结果都是定长的
# 同一个数据,但会的md5值相同
'''

'''
有一些黑客,将常用的密码与对应的md5值放到一个库中,然后进行撞库
解决办法:加盐

s2 = '12345'
ret = hashlib.md5('@$1*(^&@^2wqe'.encode('utf-8')) # 盐 - @$1*(^&@^2wqe
ret.update(s2.encode('utf-8'))
print(ret.hexdigest())
'''

'''
#弊端:黑客如果盗取了固定盐。。。。

# 改进:变成随机盐 但是不能太随机,到时候不好匹配
# 账号密码:将账号或者各种变形 设置成随机盐
username = 'alex'
password = '12345'
ret = hashlib.md5(username[::-1].encode('utf-8'))
ret.update(password.encode('utf-8'))
print(ret.hexdigest())
'''
'''
# --------------sha系列
# sha1 与 md5 级别相同,但是 sha1 比 md5 更安全一些
ret = hashlib.sha1()
ret.update('123456'.encode('utf-8'))
print(ret.hexdigest())

# sha512 级别最高,效率低,安全性最大
ret = hashlib.sha512()
ret.update('1234'.encode('utf-8'))
print(ret.hexdigest())
'''


'''
# ------------ 文件的校验 
# 对于小文件可以,但是超大的文件内存受不了
# 通过校验前后文件编码过后的内容,可以查看文件有没有丢失

def func(file_name):
    with open(file_name,mode='rb') as f1:
        ret = hashlib.md5()
        ret.update(f1.read())
        return ret.hexdigest()
print(func('hash_exec'))
'''
'''
# 校验的时候也可以分段校验
# 分段的时候切记里面的任何一个字符都不要落下
s1 = 'I am 旭哥, 都别惹我.... 不服你试试'
ret = hashlib.md5()
ret.update(s1.encode('utf-8'))
print(ret.hexdigest())  # 15f614e4f03312320cc5cf83c8b2706f

s1 = 'I am 旭哥, 都别惹我.... 不服你试试'
ret = hashlib.md5()
ret.update('I am'.encode('utf-8'))
ret.update(' 旭哥, '.encode('utf-8'))
ret.update('都别惹我....'.encode('utf-8'))
ret.update(' 不服你试试'.encode('utf-8'))
print(ret.hexdigest())  # 15f614e4f03312320cc5cf83c8b2706f
'''
#大文件 - 一次校验一部分
def func(file_name):
    with open(file_name,'rb') as f1:
        ret = hashlib.md5()
        while True:
            content = f1.read(1024)
            if content: # 检验文件内容是否为空
                ret.update(content)
            else:
                break
        return ret.hexdigest()
hashlib
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# 配置文件:放置一些常用的变量,路径.
# 帮助你操作(创建,增,删,改,查)一个配置文件

import configparser

'''
# 创建一个配置文件
config = configparser.ConfigParser()
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('配置文件.ini','w') as configfile:
    config.write(configfile)
'''


'''
#基于字典形式,查找文件内容
config = configparser.ConfigParser()
config.read('配置文件.ini')
print(config.sections()) #没有DEFAULT,它是特殊的,可以看做一个全局的。
print('111'in config) # False 判断节名是否在配置文件中

#对配置文件中的节对应的项,取值
print(config['bitbucket.org']['user']) # hg
print(config['bitbucket.org']) # <Section: bitbucket.org> 可迭代对象

for key in config['bitbucket.org']:
    print(key) #注意:有default会默认default里面节的值

print(config.options('bitbucket.org')) #找到default和bitbucket.org下的键
print(config.items('bitbucket.org')) #找到default和bitbucket.org下的键值对

print(config.get('bitbucket.org','compression')) #yes 判断节点和项是否匹配

'''

#增删改
config = configparser.ConfigParser()
config.read('配置文件.ini')
config.add_section('alex')
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")

config.set('alex','k1','w')
config.write(open('配置文件2.ini','w'))
配置文件
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# 配置文件:放置一些常用的变量,路径.
# 帮助你操作(创建,增,删,改,查)一个配置文件

import configparser

'''
# 创建一个配置文件
config = configparser.ConfigParser()
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('配置文件.ini','w') as configfile:
    config.write(configfile)
'''


'''
#基于字典形式,查找文件内容
config = configparser.ConfigParser()
config.read('配置文件.ini')
print(config.sections()) #没有DEFAULT,它是特殊的,可以看做一个全局的。
print('111'in config) # False 判断节名是否在配置文件中

#对配置文件中的节对应的项,取值
print(config['bitbucket.org']['user']) # hg
print(config['bitbucket.org']) # <Section: bitbucket.org> 可迭代对象

for key in config['bitbucket.org']:
    print(key) #注意:有default会默认default里面节的值

print(config.options('bitbucket.org')) #找到default和bitbucket.org下的键
print(config.items('bitbucket.org')) #找到default和bitbucket.org下的键值对

print(config.get('bitbucket.org','compression')) #yes 判断节点和项是否匹配

'''

#增删改
config = configparser.ConfigParser()
config.read('配置文件.ini')
config.add_section('alex')
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")

config.set('alex','k1','w')
config.write(open('配置文件2.ini','w'))
logging—低配版
原文地址:https://www.cnblogs.com/liangying666/p/9282362.html