zoukankan      html  css  js  c++  java
  • day23_hashlib_json,pickle,subprocess_configparse

    # 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了,所以eval的重点还是通常用来执行一个字符串表达式,并返回表达式的值。
    #
    # import json
    # x="[null,true,false,1]"
    # print(eval(x)) #报错,无法解析null类型,而json就可以
    # print(json.loads(x))
    # -----------------
    # 将序列化的结果写入文件的简单方法
    # with open('test.json',mode='at',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: # 这个要一下在读取出来不不是占用内存很大么?? load dump 的参数是文件对象
    # l = json.load(f)
    # print(l, type(l))

    # json验证: json格式兼容的是所有语言通用的数据类型,不能识别某一语言的所独有的类型
    # json.dumps({1,2,3,4,5})

    # json强调:一定要搞清楚json格式,不要与python混淆
    # l=json.loads('[1, "aaa", true, false]')
    # l=json.loads("[1,1.3,true,'aaa', true, false]")

    # print(l[0])
    # 把对象(变量)从内存中变成可存储或传输的过程称之为序列化

    # 把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling
    # 无论数据是怎样创建的,只要满足json格式,就可以json.loads出来,不一定非要dumps的数据才能loads,但是loads的数据必须是双引号
    # json 和 dump 有两个参数
    # 1、什么是序列化&反序列化
    # 内存中的数据类型---->序列化---->特定的格式(json格式或者pickle格式)
    # 内存中的数据类型<----反序列化<----特定的格式(json格式或者pickle格式)

    # 土办法:
    # {'aaa':111}--->序列化str({'aaa':111})----->"{'aaa':111}"
    # {'aaa':111}<---反序列化eval("{'aaa':111}")<-----"{'aaa':111}" #反序列化,将字符串转为内存中 json 格式
    # eval 类似json.loads()

    # 2、为何要序列化
    # 序列化得到结果=>特定的格式的内容有两种用途
    # 用途1 1、可用于存储=》用于存档
    # 用途2 2、传输给其他平台使用=》跨平台数据交互
    # python java
    # 列表 特定的格式 数组

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


    # 3、如何序列化与反序列化
    # 示范1
    # 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))


    # 示范2:
    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)


    # 了解
    # l = json.loads(b'[1, "aaa", true, false]')
    # print(l, type(l))

    # with open('test.json',mode='rb') as f:
    # l=json.load(f)


    # res=json.dumps({'name':'哈哈哈'})
    # print(res,type(res))

    # res=json.loads('{"name": "u54c8u54c8u54c8"}') #???为什么可以将乱码转换为 中文
    # print(res,type(res))


    # python2 和python3的json序列化兼容问题
    # import pickle
    #
    # with open('a.pkl',mode='wb') as f:
    # # 一:在python3中执行的序列化操作如何兼容python2
    # # python2不支持protocol>2,默认python3中protocol=4
    # # 所以在python3中dump操作应该指定protocol=2
    # pickle.dump('你好啊',f,protocol=2)
    #
    # with open('a.pkl', mode='rb') as f:
    # # 二:python2中反序列化才能正常使用
    # res=pickle.load(f)
    # print(res)
    # -------------
    # 4、猴子补丁
    # 在入口处打猴子补丁
    # import json
    # import ujson
    #
    #
    # def monkey_patch_json(): #
    # json.__name__ = 'ujson'
    # json.dumps = ujson.dumps
    # json.loads = ujson.loads
    #
    #
    # monkey_patch_json() # 在入口文件出运行
    #
    # import ujson as json # 不行

    # 后续代码中的应用
    # json.dumps()
    # json.loads()
    # 5.pickle模块
    import pickle
    # res=pickle.dumps({1,2,3,4,5})
    # print(res,type(res))
    # s=pickle.loads(res)
    # print(s,type(s))

    # ------------------------
    # configparse 模块
    # import configparser
    # config=configparser.ConfigParser()
    # config.read('test.ini') #读取文件对象
    # 1 获取sections
    # print(config.sections()) #根据文件名获取sections,没有参数,前面文件对象已实例化
    # 获取某一section下的 options名字,以列表的形式显示
    # print(config.options('section1'))
    # 2 获取 items将section下的数据以元祖的像是张氏
    # print(config.items("section1"))
    # for m,k in config.items("section1"): #将元素分别分配给 m,k
    # print(m,k)

    # 4 获取section下的valeu ,参数为 section,options ,get获取到的是 字符串,
    # res=config.get('section1','user')
    # print(res,type(res))
    # config.getint("section","options") #getinit获取到的直接是int 类型
    # config.getfloat("section1","age") #getfloat获取到的是 浮点型

    # ----------------- 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'))
    # ----------------
    # hashlib 模块
    # 1 什么是 哈希 hash 定义及特点
    # hash一类算法,该算法接收传入的内容,经过运算得到一串 hash 值
    # 1 只要传入的值一样,得到的hash值必然一样
    # 2 不能由 hash 值反解成内容
    # 3 不管传入的内容有多大,只要使用的hash算法不变,得到的 hash 值长度是一定的

    # 2 hash 用途
    # 用途1 ;特点2 用于密码密文传输与验证
    # 用途2 ;特点1 3,用于文件完整性校验
    # how to use
    import hashlib

    # m=hashlib.md5()
    # m.update("hello 中china".encode("utf-8"))
    # print(m.hexdigest())
    # m.update('whatyou乐山大佛'.encode('utf-8')) #继续添加内容
    # print(m.hexdigest()) #16进制

    cryptograph = 'aee949757a2e698417463d47acac93df'
    import hashlib

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

    # --------------方式一
    # dic={}
    # for data in passwds:
    # s=hashlib.md5()
    # s.update(data.encode("utf-8"))
    # dic[data]=s.hexdigest()
    #
    # for key,value in dic.items():
    # if value==cryptograph:
    # print("sucess")
    # -------------- 方式二 区别,一种是 update,一种是实例化对象直接传参了
    # dic={}
    # for data in passwds:
    # res=hashlib.md5(data.encode("utf-8"))
    # dic[data]=res.hexdigest()
    # print(dic)
    # for key,value in dic.items():
    # if value==cryptograph:
    # print("sucess")

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

    # # subprocess 模块
    #
    import subprocess
    #
    obj = subprocess.Popen("echo 123;;ls /;ls /root", shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
    print(obj)
    res=obj.stdout.read()
    print(res.decode("utf-8"))
    err_res=obj.stderr.read()
    print(err_res.decode('utf-8'))

  • 相关阅读:
    【转载】Java反射: 数组
    【转】Maven3把命令行创建的web工程转成Eclipse和IntelliJ Idea的工程
    (转载)Sumblime Text 2 常用插件以及安装方法
    [转]h5页面测试总结
    maven学习讲解
    struts2权威指南学习笔记:struts2引入自定义库
    《Struts2.x权威指南》学习笔记2
    《Struts2.x权威指南》学习笔记1
    secureCRT 如何上传下载文件
    zabbix 4.2 发送警告邮件Python脚本
  • 原文地址:https://www.cnblogs.com/pythonwork/p/14705379.html
Copyright © 2011-2022 走看看