hashlib,hmac,subprocess,configparser模块讲解
hashlib模块
1、什么叫hash:hash是一种算法,算法接收传入的内容,经过运算得到一串hash。
2、特点:
1)只要传入的内容一样,得到的hash值必然一样=====》文件的完整性校验
2)不能由hash值反解成内容=====》把密码做成hash值,不要用明文传输密码
3)只要使用hash算法不变,无论校验内容有多大,得到的hash值长度是固定的
import hashlib m=hashlib.md5() m.update('hello'.encode('utf-8')) m.update('world'.encode('utf-8')) m.update('egon'.encode('utf-8')) print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5
3、不同的算法所生成的hash值长度不同
import hashlib m=hashlib.md5() m.update('helloworldegon'.encode('utf-8')) print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5 import hashlib m=hashlib.sha256() m.update('helloworldegon'.encode('utf-8')) print(m.hexdigest()) #57e48f7b4b4d699d6b22ec34f368fc5512c598bb97800aa006754545bf318acc
4、密码加盐(提高加密安全性,提高破解密码难度)
import hashlib pwd='zhonghanlinag124' m=hashlib.md5() m.update('天王盖地虎'.encode('utf-8')) m.update(pwd.encode('utf-8')) m.update('天'.encode('utf8')) m.update('提莫两米五'.encode('utf-8')) print(m.hexdigest()) #2a70c6b001bb0bdd4b8107de8f26940c
5、hmac模块强制加盐
import hmac m=hmac.new('天王盖地虎,小鸡炖模块'.encode('utf-8')) m.update('sdfhjlksd'.encode('utf-8')) print(m.hexdigest())#e57faacbe5b0a3f9ce9516d66829b89e
subprocess模块
1、dos命令
tasklist | findstr python 过滤进程命令
tasklist /? 查看帮助
tasklist /F /PID 12360 强制关闭进程
2、linux系统
ps aux | grep python 查看过滤进程信息
kill -9 PID 杀死进程
3、利用python实现系统命令
import os while True: cmd=input('>>>: ').strip() if not cmd : continue os.system(cmd)
import subprocess obj=subprocess.Popen('dir', shell=True, stdout=subprocess.PIPE,#正确结果 stderr=subprocess.PIPE,#错误结果 ) # print('运行结果:',res) #读取正确结果 res1=obj.stdout.read() print('正确结果: ',res1.decode('gbk')) #读取错误结果 res2=obj.stderr.read() print('错误结果: ',res2.decode('gbk'))
configparser模块
文件:my.ini
[egon] age=18 pwd=123 sex=male salary=5.1 is_beautifull=Ture [xxx] age=30 pwd=123456 sex=female salary=4.111 is_beautifull=Falase
import configparser config=configparser.ConfigParser() config.read('my.ini') secs=config.sections() print(secs)#['egon', 'xxx'] res=config.options('egon') print(res)#['age', 'pwd', 'sex', 'salary', 'is_beautifull'] age=config.get('egon','age') print(age,type(age))#18 <class 'str'> age=config.getint('egon','age') print(age,type(age))#18 <class 'int'> salary=config.getfloat('egon','salary') print(salary,type(salary))#5.1 <class 'float'> b=config.getboolean('egon','is_beautifull') print(b,type(b))
2018-08-22 22:42:03

浙公网安备 33010602011771号