码迷,mamicode.com
首页 > Web开发 > 详细

json,pickle,configparser,hashlib,subprocess

时间:2020-03-31 19:21:00      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:sub   aaa   read   section   获得   兼容   接受   font   with open   

json:

序列化:把内存的数据类型转换成一个特定的格式内容,这种格式可以存储或者传输给其他平台

内存中的数据 ----> 序列化 ----> 特定的格式(json,pickle)

内存中的数据 <---- 反序列化 <----特定的格式(json,pickle)

强调:

  1. 可用于存储,是一种专门的格式
  2. 可以跨平台数据交互,能被其他语言识别

 

json强调:识别不了set,tuple

  1. json格式的字符串要用双引号表示
  2. 兼容所有是语言通用的数据类型,不能识别某一语言的所独有的类型
import json
# 序列化
json_res=json.dumps([1,aaa,True,False])
# print(json_res,type(json_res)) # "[1, "aaa", true, false]"

# 反序列化
l=json.loads(json_res)
print(l,type(l))
import json

# 序列化的结果写入文件的复杂方法
json_res=json.dumps([1,aaa,True,False])
# print(json_res,type(json_res)) # "[1, "aaa", true, false]"
with open(test.json,mode=wt,encoding=utf-8) as f:
    f.write(json_res)

# 将序列化的结果写入文件的简单方法
with open(test.json,mode=wt,encoding=utf-8) as f:
    json.dump([1,aaa,True,False],f)


# 从文件读取json格式的字符串进行反序列化操作的复杂方法
with open(test.json,mode=rt,encoding=utf-8) as f:
    json_res=f.read()
    l=json.loads(json_res)
    print(l,type(l))

# 从文件读取json格式的字符串进行反序列化操作的简单方法
with open(test.json,mode=rt,encoding=utf-8) as f:
    l=json.load(f)
    print(l,type(l))

pickle:

import pickle
res=pickle.dumps({1,2,3,4,5})
print(res,type(res)) #得到一个二进制

s=pickle.loads(res)
print(s,type(s)) # 返回一个set

configparser:

import configparser

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

# 1、获取sections
print(config.sections())

# 2、获取某一section下的所有options
print(config.options(section1))

# 3、获取items
print(config.items(section1))

# 4、获得section下的user
res=config.get(section1,user)
print(res,type(res))
# 5、获得section下的age
res=config.getint(section1,age)
print(res,type(res))

hashlib:

  1. hash的一类算法,该算法接受传入的内容,得到一串hash值
  2. hash值的特点:
    • 只要传入的内容一样,得到的hash值也是一样的
    • 不管传入的内容多大,只要hash算法不变,得到hash值的长度也是不变
    • 不能由hash值反推传入的内容(用于密码密文传输与验证)
import hashlib

m=hashlib.md5()
m.update(hello.encode(utf-8))
m.update(world.encode(utf-8))
res=m.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


# 提升撞库的成本=>密码加盐
import 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(tasklist, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
print(obj)
res = obj.stdout.read()
print(res.decode(gbk))
err_res = obj.stderr.read()
print(err_res.decode(gbk))

json,pickle,configparser,hashlib,subprocess

标签:sub   aaa   read   section   获得   兼容   接受   font   with open   

原文地址:https://www.cnblogs.com/zhenghuiwen/p/12607400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!