一、haslib模块
1、什么叫hash
hashlib是个专门提供hash算法的库,现在里面包括md5, sha1, sha224, sha256, sha384, sha512,使用非常简单、方便。 md5经常用来做用户密码的存储。而sha1则经常用作数字签名。
2、特点:
只要传入的内容一样,得到的hash值必然是一样的。=====》文本完整性校验。
不能由hash值返解成内容。=====》把密码做成hash值,不要用明文传输密码。
只要使用的hash算法不变,无论校验的内容有多大,得到的hash值的长度是固定的。
import hashlib
m=hashlib.md5()
m.update('hellword,monicx'.encode('utf-8'))
print(m.hexdigest())
#bad0cfbd90193f008b8f4cc25eb99f24
#密码加盐:
import hashlib
pwd='monicx123'
m=hashlib.md5()
m.update('天王盖志虎'.encode('utf-8'))
m.update(pwd.encode('utf-8'))
m.update('小雨一米五'.encode('utf-8'))
print(m.hexdigest())
二、subprocess模块
dos命令:
tasklist|findstr python
taskkill /F /PID 2640
tasklist| findstr python
import subprocess
obj=subprocess.Popen('dir',
shell=True,
stdout=subprocess.PIPE,#正确的管道
stderr=subprocess.PIPE)
# print('得到运行结果:',obj)
res1=obj.stdout.read()
print('正确的结果:',res1.decode('gbk'))
# res2=obj.stderr.read()
# print('错误的结果:',res2.decode('gbk'))
运行结果为:
注意:管道里的东西只能取一次,取完后就没有了。
三、configparse模块
它的数据结构如下:
我们来看一下代码:
import configparser
config=configparser.ConfigParser()
config.read('my.ini')
res=config.sections()
print(res)#['monicx', 'lxx']
re=config.options('monicx')
print(re)#['age', 'pwd', 'sex', 'salary', 'is_handsome']
age=config.getint('monicx','age')
print(age,type(age))#19 <class 'int'>
salary=config.getfloat('monicx','salary')
print(salary,type(salary))#5.1 <class 'float'>
b=config.getboolean('lxx','is_handsome')
print(b,type(b))#False <class 'bool'>