Python——hashilib 模块(哈希模块)

hashilib 模块 摘要算法

import hashlib # 提供摘要算法的模块
md5 = hashlib.md5()
md5.update(b'alex3714')
print(md5.hexdigest())
 动态加盐
 用户名 密码
 使用用户名的一部分或者 直接使用整个用户名作为盐
 import hashlib   # 提供摘要算法的模块
 md5 = hashlib.md5(bytes('',encoding='utf-8')+b'(用户名的一部分)')
  md5.update(b'123456')
 print(md5.hexdigest())

 configparse

# configparse
 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('example.ini', 'w') as f:
    config.write(f)

    
    
    
 import configparser
#
 config = configparser.ConfigParser()
#---------------------------查找文件内容,基于字典的形式

 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'])          #<Section: bitbucket.org>
#
 for key in config['bitbucket.org']:     # 注意,有default会默认default的键
     print(key)

 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')   # 增加section
 config.remove_section('bitbucket.org')   # 删除一个section
 config.remove_option('topsecret.server.com',"forwardx11")  # 删除一个配置项
 config.set('topsecret.server.com','k1','11111')
 config.set('yuan','k2','22222')
 f = open('new2.ini', "w")
 config.write(f) # 写进文件
 f.close()    
View Code

logging 模块 日志

#例子
import logging
logger = logging.getLogger()
fh = logging.FileHandler('log.log',encoding='utf-8')
sh = logging.StreamHandler()    # 创建一个屏幕控制对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
# 文件操作符 和 格式关联
fh.setFormatter(formatter)
sh.setFormatter(formatter2)
# logger 对象 和 文件操作符 关联
logger.addHandler(fh)
logger.addHandler(sh)
logging.debug('debug message')       # 低级别的 # 排错信息
logging.info('info message')            # 正常信息
logging.warning('警告错误')      # 警告信息
logging.error('error message')          # 错误信息
logging.critical('critical message') # 高级别的 # 严重错误信息
例子

 

posted @ 2018-10-16 09:53  澄心元素  阅读(447)  评论(0编辑  收藏  举报