23---常用模块2

一 json&pickle模块

1 什么是序列化和反序列化

序列化指的是把内存的数据类型转换成一种特定的格式的内容,该格式的内容可用于存储或者传输给其他平台使用
序列化:
内存中的数据类型----》序列化----》特定的格式(json/pickle格式)
反序列化:
内存中的数据类型《----反序列化《----特定的格式(json/pickle格式)

2 为什么要用

序列化得到结果==》特定格式的内容有两种用途
   1 可用于存储====》用于存档(自己的程序使用)
   2 传输给其他平台使用====》跨平台数据交互(两种语言共有的数据类型)(给其他语言的程序使用)
      python列表----特定格式----java数组

强调:
针对用途1的特定一种格式:可以是一种专用的格式===》pickle只有python可以识别
针对用途2的特定一种格式:应该是一种通用能够被所有语言识别的格式====》json

3 如何用序列化和反序列化

1 json模块
# 序列化
import json
res = json.dumps([1,2,3,4,True,False])
print('序列化结果',res)  # 'true'---字符串类型
# 序列化结果可以存在文件中:复杂方法
with open('json.txt', 'w', encoding='utf-8') as f:
    f.write(res)

# 将序列化的结果写入文件的简单方法:
with open('json.txt', 'w', encoding='utf-8') as f:
    json.dump([1,2,3,4,True,False],f)
# 反序列化
res1 = json.loads(res)
print('反序列化',res1,type(res1))
# 将文件中的数据反序列化:复杂方法
with open('json.txt', 'r', encoding='utf -8') as f:
    json_res = f.read()
    res = json.loads(json_res)
    print(res)

# 反序列化的简单方法
with open('json.txt', 'r', encoding='utf -8') as f:
    res = json.load(f)
    print(res,type(res))
# 强调:
# json格式兼容的是所有语言通用的数据类型,不能用于某种语言的特定类型
# 一定要搞清楚json格式不要与python混淆
2 pickle模块
# pickle模块与json模块用法相同
import pickle
# 序列化
res = pickle.dumps({1,2,3})
# 集合属于python独有的
print(res)
# 反序列化
res1 = pickle.loads(res)
print(res)

二 configparser模块--解析配置文件

import configparser
config = configparser.ConfigParser()
config.read('test.ini')

# 获取所有section
print(config.sections())
# 获取section所有option
print(config.options('section1'))
# 获取每个option与其对应的值
print(config.items(section='section1'))
# 获取option对应的值
print(config.get('section1','k1'))
print(config.getfloat('section1','k3'))
print(config.getboolean('section1','k2'))
# 注释,test.ini文件
; 注释
[section1]
k1 = v1
k2 = True
k3 = 13
[section2]
k4 = '哈哈'

三 hashlib模块

1 什么是哈希hash

hash一类算法,该算法接受传入的内容,经过运算得到一串hash值

2 hash值的特点

I 只要传入的内容一样,得到的hash值必然一样
II 不能由hash值返解成内容
III 不管传入的内容有多大,只要使用的hash算法不变,得到的hash值长度是一定

3 hash的用途

用途1:特点II用于密码密文传输与验证
用途2:特点I、III用于文件完整性校验

4 如何使用

Ⅰ 密码加密
import hashlib

m=hashlib.md5()
m.update('hello'.encode('utf-8'))
m.update('world'.encode('utf-8'))
res=m.hexdigest() # 'helloworld'
print(res)

m1=hashlib.md5('he'.encode('utf-8'))
m1.update('llo'.encode('utf-8'))
m1.update('w'.encode('utf-8'))
m1.update('orld'.encode('utf-8'))
res=m1.hexdigest()# 'helloworld'
print(res)
Ⅱ 模拟撞库
cryptograph='aee949757a2e698417463d47acac93df'
import hashlib

# 制作密码字段
passwds=[
    'alex3714',
    'alex1313',
    'alex94139413',
    'alex123456',
    '123456alex',
    'a123lex',
]

dic={}
for p in passwds:
    res=hashlib.md5(p.encode('utf-8'))
    dic[p]=res.hexdigest()

# 模拟撞库得到密码
for k,v in dic.items():
    if v == cryptograph:
        print('撞库成功,明文密码是:%s' %k)
        break
Ⅲ 提升撞库的成本=>密码加盐
mport hashlib

m=hashlib.md5()

m.update('天王'.encode('utf-8'))
m.update('alex3714'.encode('utf-8'))
m.update('盖地虎'.encode('utf-8'))
print(m.hexdigest())

四 subprocess模块

调用操作系统的命令

import subprocess
obj = subprocess.Popen('dir',shell=True,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE,
                 )
print(obj)
right = obj.stdout.read()
print(right.decode('gbk'))
error = obj.stderr.read()
print(error.decode('gbk'))

猜你喜欢

转载自www.cnblogs.com/Kathrine/p/12607949.html