Python之旅.第四章.模块与包

一、hashlib模块

什么叫hash:hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值

hash值的特点是:

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'))   #update接受bytes,可以辅助分批导入

print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5

 

m=hashlib.md5()

with open(r'D:codeSH_fullstack_s1day18上节课复习','rb') as f:

    for line in f:

        m.update(line)

    hv=m.hexdigest()

print(hv) #f2a3a94efd0809e8a9c5ac8794c4bb2d

          953cd74a08f4fbb7e69a4bda8dfad056

 

密码加盐

import hashlib

pwd='alex3714'

 

m=hashlib.md5()

m.update('一行白鹭上青天')

m.update(pwd.encode('utf-8'))

m.update('天'.encode('utf-8'))

m.update('小雨一米五'.encode('utf-8'))

print(m.hexdigest())

 

hashlib下的不同算法

import hashlib

 

# m=hashlib.md5()

# m.update('helloworld'.encode('utf-8'))

# print(m.hexdigest()) #fc5e038d38a57032085441e7fe7010b0

 

m=hashlib.sha256()

m.update('helloworld'.encode('utf-8'))

print(m.hexdigest()) #936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af

 

m=hashlib.sha512()

m.update('helloworld'.encode('utf-8'))

print(m.hexdigest()) #1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60

 

hmac必须加盐

import hmac

m=hmac.new('天王盖地虎,小鸡炖模块'.encode('utf-8'))

m.update('alex3814'.encode('utf-8'))

print(m.hexdigest())

 

二、subprocess模块 #系统命令

dos命令

dir

cd

ipconfig

tasklist

tasklist | findstr python  # |为管道,tasklist的结果不直接丢给屏幕而是传入管道,findstr从管道接到结果进行筛选

taskkill /?  #/?可以用来查看用法

D:code>tasklist | findstr python

python.exe                   12360 Console                    1     11,024 K

D:code>taskkill /F /PID 12360   #F表示强制关闭, 12360为PID时随机分配的

 

linux系统(了解)

ps aux | grep python #查询系统里的python进程

kill -9 PID 停止进程

 

import os

while True:

    cmd=input('>>>: ').strip()

    if not cmd:continue

    # print('%s run' %cmd)

    res=os.system(cmd)  #以上方法只能在一台机器上运行,不适合网络传输

 

    network.send(res)  

 

import os

 

res=os.system('dixCVr')

print('运行结果:',res)

 

import subprocess

 

obj=subprocess.Popen('dir',

                     shell=True,  #调用命令解释器;shell为命令解释器

                     stdout=subprocess.PIPE,  #将正确运行的结果传入管道

                     stderr=subprocess.PIPE   #将错误运行的结果传入管道

                     )

 

res1=obj.stdout.read()

print('正确结果1111: ',res1)

 

res2=obj.stdout.read()

print('正确结果2222: ',res2) #只能取一次,取走了就没有了

 

res2=obj.stderr.read()

print('错误结果:',res2.decode('gbk'))

 

三、configparser模块 #用于处理ini后缀文件

my.ini文件中主要有两种类型,section和option

import configparser

 

config=configparser.ConfigParser()

config.read('my.ini')

 

secs=config.sections()

print(secs)

 

print(config.options('egon'))

 

age=config.get('egon','age')`

age=config.getint('egon','age')

print(age,type(age))

 

salary=config.getfloat('egon','salary')

print(salary,type(salary))

 

b=config.getboolean('egon','is_beatifull')

print(b,type(b))

 

原文地址:https://www.cnblogs.com/yangli0504/p/8781320.html