zoukankan      html  css  js  c++  java
  • hashlib,suprocess,configparser模块


    十 hashlib模块

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

    2、hash值的特点是:
    2.1 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验
    2.2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码
    2.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


    以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。

    import hashlib

    hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
    hash.update('alvin'.encode('utf8'))
    print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7

    python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密:

    import hmac
    h = hmac.new('alvin'.encode('utf8'))
    h.update('hello'.encode('utf8'))
    print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940


    要想保证hmac最终结果一致,必须保证:
    1:hmac.new括号内指定的初始key一样
    2:无论update多少次,校验的内容累加到一起是一样的内容

    二、subprocess模块

    import subprocess

    dos命令
    tasklist | findstr python
    taskkill /? #查询用法
    D:code>tasklist | findstr python
    python.exe 12360 Console 1 11,024 K

    D:code>taskkill /F /PID 12360

    linux系统(了解)
    ps aux | grep python
    kill -9 PID


    在Python中模拟cmd并保存而不输出到屏幕
    import os

    res=os.system('dixCVr')
    print('运行结果:',res)

    import subprocess

    obj=subprocess.Popen('dir',
    shell=True,
    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模块

    配置文件如下:

    [section1]
    k1 = v1
    k2:v2
    user=egon
    age=18
    is_admin=true
    salary=31

    [section2]
    k1 = v1

    import configparser

    config=configparser.ConfigParser()
    config.read('a.cfg')

    查看所有的标题
    res=config.sections() #['section1', 'section2']
    print(res)

    查看标题section1下所有key=value的key
    options=config.options('section1')
    print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

    查看标题section1下所有key=value的(key,value)格式
    item_list=config.items('section1')
    print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

    查看标题section1下user的值=>字符串格式
    val=config.get('section1','user')
    print(val) #egon

    查看标题section1下age的值=>整数格式
    val1=config.getint('section1','age')
    print(val1) #18

    查看标题section1下is_admin的值=>布尔值格式
    val2=config.getboolean('section1','is_admin')
    print(val2) #True

    查看标题section1下salary的值=>浮点型格式
    val3=config.getfloat('section1','salary')
    print(val3) #31.0

    import configparser

    config=configparser.ConfigParser()
    config.read('a.cfg',encoding='utf-8')


    删除整个标题section2
    config.remove_section('section2')

    删除标题section1下的某个k1和k2
    config.remove_option('section1','k1')
    config.remove_option('section1','k2')

    判断是否存在某个标题
    print(config.has_section('section1'))

    判断标题section1下是否有user
    print(config.has_option('section1',''))


    添加一个标题
    config.add_section('egon')

    在标题egon下添加name=egon,age=18的配置
    config.set('egon','name','egon')
    config.set('egon','age',18) #报错,必须是字符串


    最后将修改的内容写入文件,完成最终的修改
    config.write(open('a.cfg','w'))

  • 相关阅读:
    Python Web学习笔记之Python多线程基础
    Python入门之python可变对象与不可变对象
    Python Web学习笔记之SOCK_STREAM和SOCK_DGRAM
    background和background-position相关笔记
    自定义switch开关
    获取浏览器类型和版本号
    随机生成字符串
    white-space详解
    文件选择按钮随笔
    mouse的各种事件
  • 原文地址:https://www.cnblogs.com/zhaodafa/p/8778830.html
Copyright © 2011-2022 走看看