一、hashilib(摘要算法):
概念:
1.不管算法多么不同,摘要的功能始终不变。
2.对于相同的字符串使用同一个算法进行摘要,得到的值总是不变的。
3.使用不同算法对相同的字符串进行摘要,得到的值应该不同
4.不管使用什么算法,hashlib的方式永远不变
功能:
1.密码的密文存储
2.文件的一致性验证
密文登陆:
import hashlib usr = input('username:') pwd = input('password:') with open('file')as f: for line in f: user,passwod,role = line.split('|') md5 = hashlib.md5(bytes('salt',encoding='utf-8')+b'') md5.update(bytes(pwd,encoding='utf-8')) md5_pwd = md5.hexdigest() if usr == user and md5_pwd == passwod: print('登陆成功') else: print('登陆失败')
二、configparser(创建文件):
可以用configparser创建如下类型的文件
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
实现代码
import configparser config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval':'45', 'Compression':'yes', 'CompressionLevel':'9', 'ForwardX11':'yes' } config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Port':'50022', 'ForwardX11':'no' } with open('file.ini','w')as configfile: config.write(configfile)
查看文件用法
import configparser config = configparser.ConfigParser() config.read('file.ini') #查看之前一定要指定文件 # print(config.sections()) #查看组(不显示default) # print('bitbucket.org'in config) #是否有这个组 print(config['bitbucket.org']['user']) #查看user对应的值 print(config.options('bitbucket.org')) #查看所有的key,以列表形式
文件的增删改操作
import configparser config = configparser.ConfigParser() config.read('file.ini') config.add_section('haha') #新建一个haha组 config.set('haha','k2','22222') #在haha组里加一行k2 = 22222 config.remove_section('bitbucket.org') #删除一个组 config.remove_option('topsecret.server.com','forwardx11') #删除某一个值 config.write(open('file2.ini','w')) #要改变文件一定要写这个,创建新的文件
三、logging(日志模块)
简单版本日志模块:不可能输出中文
import logging logging.basicConfig(level=logging.ERROR, #打印什么级别以上,默认不打印debug format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') try: int(input('num>>>')) except ValueError: logging.error('is not number') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
logger对象配置
import logging logger = logging.getLogger() #别名 fh = logging.FileHandler('test.log',encoding='utf-8') #创建日志写入文件 sh = logging.StreamHandler() #输出到控制台 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') formatter2 = logging.Formatter('%(asctime)s - %(message)s') #设置格式 logger.setLevel(logging.INFO) #定义日志级别,int或者str都可以 fh.setFormatter(formatter) #文件和格式关联(使用日志格式) sh.setFormatter(formatter2) logger.addHandler(fh) #对象和文件关联 logger.addHandler(sh) 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')
配置参数

logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有: filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。 filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。 format:指定handler使用的日志显示格式。 datefmt:指定日期时间格式。 level:设置rootlogger(后边会讲解具体概念)的日志级别 stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。 format参数中可能用到的格式化串: %(name)s Logger的名字 %(levelno)s 数字形式的日志级别 %(levelname)s 文本形式的日志级别 %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有 %(filename)s 调用日志输出函数的模块的文件名 %(module)s 调用日志输出函数的模块名 %(funcName)s 调用日志输出函数的函数名 %(lineno)d 调用日志输出函数的语句所在的代码行 %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示 %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数 %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 %(thread)d 线程ID。可能没有 %(threadName)s 线程名。可能没有 %(process)d 进程ID。可能没有 %(message)s用户输出的消息