常用模块(hashlib,configparser,logging)
常用模块(hashlib,configparser,logging)
hashlib
hashlib 摘要算法的模块
md5 sha1 sha256 sha512
摘要的过程 不可逆
能做的事:
文件的一致性检测
用户的加密认证 单纯的md5不够安全 加盐处理 简单的盐可能被破解 且破解之后所有的盐都失效 动态加盐
md5 = hashlib.md5() # 选择摘要算法中的md5类进行实例化,得到md5_obj
md5.update(b'how to use md5 in python hashlib?') # 对一个字符串进行摘要
print(md5.hexdigest()) # 找摘要算法要结果
一篇文章的校验
读文件:一行一行拿
转换成bytes
文件1
文件2
分别打开两个文件,一行一行读,每一行update一下,对比最终的hexdigest
查看某两个文件是否完全一致 —— 文件的一致性校验
加密认证 —— 在存储密码的时候使用密文存储,校验密码的时候对用户的输入再做一次校验

import hashlib
md5 = hashlib.md5()
md5.update(b'alex3714') pwd = input('')
md5 = hashlib.md5()
md5.update(b'alex3714')

加盐
动态加盐
用户名 + 一个复杂的字符串 + 密码
import hashlib
md5 = hashlib.md5(b'suger')
md5.update(b'alex3714')
configparser
文件格式如下

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

想用python生成一个这样的文档怎么做

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 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']) #<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') # 添加一个组
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('new2.ini', "w")) # write的时候才生效

为什么要有配置文件:在程序外修改一些配置
配置文件其实是多种多样的
configparser是专门解决一种样式的配置文件而生的
yaml 是另一种配置规则 python也提供了扩展模块
logging
日志 在程序的运行过程中,人为的添加一些要打印的中间信息
在程序的排错、在一些行为、结果的记录
import logging
logging.debug('debug message') # 调试模式:不是必须出现,但是如果有问题需要借助它的信息调试
logging.info('info message') # 信息模式:必须出现但是对程序正常运行没有影响
logging.warning('warning message') # 警告模式:不会直接引发程序的崩溃,但是可能会出问题
logging.error('error message') # 错误模式:出错了
logging.critical('critical message') # 批判模式:程序崩溃了的时候
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='/tmp/test.log',
filemode='w') logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:
filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”
format:指定handler使用的日志显示格式
datefmt:指定日期时间格式
level:设置rootlogger(后边会讲解具体概念)的日志级别
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用户输出的消息

logging 高级的使用对象配置的模式

logger = logging.getLogger() # 实例化一个logger对象
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log',encoding='utf-8') # 文件句柄-日志文件操作符
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler() # 屏幕流对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 日志输出格式
logger.setLevel(logging.DEBUG) # 设置日志等级,默认是Warning
fh.setFormatter(formatter) # 文件句柄绑格式
ch.setFormatter(formatter)
logger.addHandler(fh) # logger绑文件句柄
logger.addHandler(ch)
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:
配置简单,配了就能直接用
对象模式:
可以随意的控制往哪些地方输出日志
且可以分别控制输出到不同位置的格式
常用模块(hashlib,configparser,logging)的更多相关文章
- python_模块 hashlib ,configparser, logging
hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长 ...
- python常用模块之configparser模块
python常用模块之configparser 作用:解析配置文件 假设在当前目录下有这样一个conf.ini文件 [DEFAULT] ServerAliveInterval = 45 Compres ...
- 常用模块之hashlib,configparser,logging模块
常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- hashlib,configparser,logging模块
一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度 ...
- 常用模块(subprocess/hashlib/configparser/logging/re)
一.subprocess(用来执行系统命令) import os cmd = r'dir D:xxx | findstr "py"' # res = subprocess.Pope ...
- python常用模块补充hashlib configparser logging,subprocess模块
一.hashlib模板 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- hashlib,configparser,logging,模块
一,hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一 ...
- 内置函数 hashlib configparser logging 模块 C/S B/S架构
1.内置函数 # 内置的方法有很多 # 不一定全都在object中 # class Classes: # def __init__(self,name): # self.name = name # s ...
- 序列化 ,hashlib ,configparser ,logging ,collections模块
# 实例化 归一化 初始化 序列化 # 列表 元组 字符串# 字符串# .......得到一个字符串的结果 过程就叫序列化# 字典 / 列表 / 数字 /对象 -序列化->字符串# 为什么要序列 ...
随机推荐
- nginx php-fpm安装手记
首先下载nginx,nginx下载地址:http://www.nginx.org/download/nginx-0.8.53.tar.gz[root@winsyk ~]# mkdir -p /usr/ ...
- BT下载原理分析
版权声明:本文为博主原创文章,未经博主允许不得转载. BitTorrent协议. BT全名为BitTorrent,是一个p2p软件,你在下载download的同时,也在为其他用户提供上传upload, ...
- markdown完整语法规范3.0+编辑工具介绍
以下每一种,我都会挑选最常用的一种写法,一切表述只追求简明扼要.想深究,请查看文末链接. 通用写法:符号+空格+内容 1 引用: 单层引用: > 一级引用 多层引用:内层符号前的空格必须要 &g ...
- ICO图标的制作与应用
制作参看:http://www.shouce.ren/tool/ico?action=make 示例: <link href="./js/favicon.ico" rel=& ...
- 金典 SQL笔记(9)
page301-354其它解决方式 ---开窗函数 --測试数据及表 USE [NB] GO /****** 对象: Table [dbo].[T_Person2] 脚本日期: 08/14/2015 ...
- mui 子页面切换父页面底部导航
在父页面中新增方法: function switchTab(tab){ plus.webview.hide(activeTab); activeTab= tab; plus.webview.show( ...
- RequireJS禁止缓存
通过配置文件可以禁止加载缓存的JS文件, 这个在开发过程中非常有用具体做法如下 require.config({ paths: { "E":"/Scripts/MyMod ...
- 一入python深似海--变量和对象
一.基本原理 Python中一切都是对象,变量是对象的引用. 这是一个普遍的法则.我们举个样例来说.Python是怎样来处理的. x = 'blue' y = 'green' z = x 当pytho ...
- 自动更新本地 GIT 仓库
随着开源软件的兴起,尤其是 GITHUB 的蓬勃发展,很多开源软件都通过 GIT 进行管理,在我的计算机上就一个目录是我关注并使用的开源软件 GIT 本地副本,如何定期更新这些仓库,一个个的更新太累人 ...
- 72、android状态栏一体化,状态栏改变颜色
只能在4.4以上版本使用. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...