您的位置:首页 > 脚本大全 > > 正文

python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)

更多 时间:2021-10-13 00:35:53 类别:脚本大全 浏览量:617

python strip用法

Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析

本文实例讲述了python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法。分享给大家供大家参考,具体如下:

1、shelve模块

shelve类似于一个key-value数据库,可以很方便的用来保存python的内存对象,其内部使用pickle来序列化数据

简单来说,使用者可以将一个列表、字典、或者用户自定义的类实例保存到shelve中,下次需要用的时候直接取出来,

就是一个python内存对象,不需要像传统数据库一样,先取出数据,然后用这些数据重新构造一遍所需要的对象。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • import shelve
  • import datetime
  • d = shelve.open('shelve_test') # 打开一个文件
  • info = {
  •   "age":23,
  •   "job":"it"
  • }
  • name = ["alex", "rain", "test"]
  • d["name"] = name # 持久化列表
  • d["info"] = info # 持久化字典
  • d["data"] = datetime.datetime.now()
  • d.close()
  • 运行结果:产生3个文件

    python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)

    从shelve中数据读取:get方法

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • import shelve
  • import datetime
  • d = shelve.open('shelve_test') # 打开一个文件
  • print(d.get("name"))
  • print(d.get("info"))
  • print(d.get("data"))
  • 运行结果:

    ['alex', 'rain', 'test']
    {'job': 'it', 'age': 23}
    2017-09-29 18:31:12.013709

    2、xml模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,在json还没诞生时,

    大家只能选择用xml,至今很多传统公司如金融行业的很多系统的接口还主要是xml。xml的格式如下,就是通过<>节点来区别数据结构的。

    (1)xml文件示例代码如下:文件名为:xml_test.xml

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • <?xml version="1.0"?>
  • <data>
  •   <country name="liechtenstein">
  •     <rank updated="yes">2</rank>
  •     <year>2008</year>
  •     <gdppc>141100</gdppc>
  •     <neighbor name="austria" direction="e"/>
  •     <neighbor name="switzerland" direction="w"/>
  •   </country>
  •   <country name="singapore">
  •     <rank updated="yes">5</rank>
  •     <year>2011</year>
  •     <gdppc>59900</gdppc>
  •     <neighbor name="malaysia" direction="n"/>
  •   </country>
  •   <country name="panama">
  •     <rank updated="yes">69</rank>
  •     <year>2011</year>
  •     <gdppc>13600</gdppc>
  •     <neighbor name="costa rica" direction="w"/>
  •     <neighbor name="colombia" direction="e"/>
  •   </country>
  • </data>
  • (2)python中操作xml模块

    xml协议在各种语言里的都是支持的,在python中可以用以下模块操作xml 。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • #python中操作xml模块
  • import xml.etree.elementtree as et
  • tree = et.parse("xml_test.xml"#要处理的xml文件名
  • root = tree.getroot()  #root是一个内存对象
  • print(root)
  • print(root.tag)    #打印标签名
  • #print(et.parse("xml_test.xml").getroot().tag)
  • # 遍历xml文档
  • for child in root:
  •   print(child.tag, child.attrib)  #打印下一级的标签名和属性
  •   for i in child:
  •     print(i.tag,i.attrib,i.text)
  • python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)

    运行结果:

    <element 'data' at 0x0062e8a0>
    data
    country {'name': 'liechtenstein'}
    rank {'updated': 'yes'} 2
    year {} 2008
    gdppc {} 141100
    neighbor {'direction': 'e', 'name': 'austria'} none
    neighbor {'direction': 'w', 'name': 'switzerland'} none
    country {'name': 'singapore'}
    rank {'updated': 'yes'} 5
    year {} 2011
    gdppc {} 59900
    neighbor {'direction': 'n', 'name': 'malaysia'} none
    country {'name': 'panama'}
    rank {'updated': 'yes'} 69
    year {} 2011
    gdppc {} 13600
    neighbor {'direction': 'w', 'name': 'costa rica'} none
    neighbor {'direction': 'e', 'name': 'colombia'} none

    只遍历节点year,代码如下:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • #python中操作xml模块
  • import xml.etree.elementtree as et
  • tree = et.parse("xml_test.xml"#要处理的xml文件名
  • root = tree.getroot()  #root是一个内存对象
  • print(root)
  • print(root.tag)    #打印标签名
  • # 只遍历year 节点
  • for node in root.iter('year'):
  •   print(node.tag, node.text)
  • 运行结果:

    <element 'data' at 0x0050e8d0>
    data
    year 2008
    year 2011
    year 2011

    3、configparser模块

    用于生成和修改常见配置文档,常见文档格式如下:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • [default]
  • serveraliveinterval = 45
  • compression = yes
  • compressionlevel = 9
  • forwardx11 = yes
  • [bitbucket.org]
  • user = hg
  • [topsecret.server.com]
  • port = 50022
  • forwardx11 = no
  • python生成配置文档:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • #python生成配置文档
  • import configparser
  • config = configparser.configparser()
  • config["default"] = {'serveraliveinterval': '45',
  •            'compression': 'yes',
  •            'compressionlevel': '9'}
  • config['bitbucket.org'] = {}
  • config['bitbucket.org']['user'] = 'hg'
  • config['topsecret.server.com'] = {}
  • topsecret = config['topsecret.server.com']
  • topsecret['host port'] = '50022' # mutates the parser
  • topsecret['forwardx11'] = 'no' # same here
  • config['default']['forwardx11'] = 'yes'
  • with open('example.ini', 'w') as configfile:
  •   config.write(configfile)
  • 4、hashlib模块

    做一个映射关系,将字符串转成数字,用于加密相关的操作。

    3.x里主要提供 sha1, sha224, sha256, sha384, sha512 ,md5 算法。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • #!/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # author:zhengzhengliu
  • import hashlib
  • m = hashlib.md5()  #生成对象
  • m.update(b"hello")
  • m.update(b"it's me")
  • print(m.digest())
  • m.update(b"it's been a long time since last time we ...")
  • print(m.digest()) #2进制格式hash
  • print(len(m.hexdigest())) #16进制格式hash
  • print(m.hexdigest())
  • # ######## md5 ########
  • hash = hashlib.md5()
  • hash.update(b'admin')
  • print("md5:",hash.hexdigest())
  • # ######## sha1 ########
  • hash = hashlib.sha1()
  • hash.update(b'admin')
  • print("sha1:",hash.hexdigest())
  • # ######## sha256 ########
  • hash = hashlib.sha256()
  • hash.update(b'admin')
  • print("sha256:",hash.hexdigest())
  • 运行结果:

    b']\xde\xb4{/\x92z\xd0\xbf$\x9cr\xe3br\x8a'
    b'\xa0\xe9\x89e\x03\xcb\x9f\x1a\x14\xaa\x07?<\xae\xfa\xa5'
    32
    a0e9894503cb9f1a14aa073f3caefaa5
    md5: 21232f297a57a5a743894a0e4a801fc3
    sha1: d033e22ae348aeb5660fc2140aec35850c4da997
    sha256: 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918

    5、hmac 模块

    它内部对我们创建 key 和 内容 再进行处理然后再加密。

    散列消息鉴别码,简称hmac,是一种基于消息鉴别码mac(message authentication code)的鉴别机制。

    使用hmac时,消息通讯的双方,通过验证消息中加入的鉴别密钥k来鉴别消息的真伪;一般用于网络通信中消息加密。

    前提是双方先要约定好key,就像接头暗号一样,然后消息发送把用key把消息加密,接收方用key + 消息明文再加密,

    拿加密后的值 跟 发送者的相对比是否相等,这样就能验证消息的真实性,及发送者的合法性了。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • import hmac
  • h = hmac.new(b'zxc', 'cvb你好'.encode(encoding="utf-8"))
  • print(h.digest())
  • print(h.hexdigest())
  • #运行结果:
  • #b'\xc1\x89\t#vq\xa4\x00\xbf\xed\xb2_\xc1s\xfa\xd2'
  • #c18909235651a400bfedb25fc173fad2
  • 希望本文所述对大家python程序设计有所帮助。

    原文链接:https://blog.csdn.net/loveliuzz/article/details/78136069