转载:https://www.cnblogs.com/yuanchenqi/article/5732581.html

configparser

来看一个好多软件的常见文档格式如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

如果想用python生成一个这样的文档怎么做呢?

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'<br>
with open('example.ini', 'w') as configfile:
config.write(configfile)
import configparser

config = configparser.ConfigParser()

#---------------------------------------------查
print(config.sections()) #[] config.read('example.ini') print(config.sections()) #['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config)# False print(config['bitbucket.org']['User']) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no for key in config['bitbucket.org']:
print(key) # user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11 print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] print(config.get('bitbucket.org','compression'))#yes #---------------------------------------------删,改,增(config.write(open('i.cfg', "w"))) config.add_section('yuan') config.remove_section('topsecret.server.com')
config.remove_option('bitbucket.org','user') config.set('bitbucket.org','k1','') config.write(open('i.cfg', "w")) 增删改查

增删改查

hashlib

用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

import hashlib

m=hashlib.md5()# m=hashlib.sha256()

m.update('hello'.encode('utf8'))
print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592 m.update('alvin'.encode('utf8')) print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af m2=hashlib.md5()
m2.update('helloalvin'.encode('utf8'))
print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af

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

import hashlib

# ######## 256 ########

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

  

24.configparser&hashlib的更多相关文章

  1. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  2. python学习道路(day6note)(time &datetime,random,shutil,shelve,xml处理,configparser,hashlib,logging模块,re正则表达式)

    1.tiim模块,因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ print("time".ce ...

  3. 常用模块之 re shutil configparser hashlib xldt和xlwd

    shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...

  4. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  5. python3之xml&ConfigParser&hashlib&Subprocess&logging模块

    1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...

  6. (常用)configparser,hashlib,hamc模块

    configparser模块 #专门解析my.ini这种形式的文件(cnf) import configparser  config=configparser.ConfigParser()  conf ...

  7. 常用模块xml,shelve,configparser,hashlib

    XML 什么XML:全称 可扩展标记语言 标记指的是代表某种含义的字符 XML<> 为什么需要XML 为能够在不同的平台间继续数据的交换 为了使交换的数据能让对方看懂 就需要按照一定的语法 ...

  8. 8.2、常用模块介绍2:xml,configparser,hashlib

    xml: 介绍:包含关于可扩展标记语言xml的函数 使用: python有三种方法解析XML--SAX,DOM,以及ElementTree,由于xml技术落后,所以这里不对sax,dom介绍: xml ...

  9. Python常用模块(logging&re&时间&random&os&sys&shutil&序列化&configparser&&hashlib)

    一. logging(日志模块) 二 .re模块 三. 时间模块 四. random模块 五. os模块 六. sys模块 七. shutil模块 八. 序列化模块(json&pickle&a ...

随机推荐

  1. 通知: Spring Cloud Alibaba 仓库迁移

    最近,Spring Cloud 官方修改了各个第三方项目的发布策略,第三方 spring-cloud 项目需要自身维护.基于此策略,Spring-Cloud-Alibaba 项目迁移到了 alibab ...

  2. 巨蟒python全栈开发-第11阶段 ansible_project6

    今日大纲: 1.计划任务前端页面 2.计划任务新增实现 3.计划任务编辑 4.项目详情 5.文件上传 6.replace模块介绍 1.计划任务前端页面 2.计划任务新增实现 3.计划任务编辑 4.项目 ...

  3. Python基础:14生成器

    yield表达式只用于定义生成器函数,且只能存在于函数的定义体中.只要一个函数内部使用了yield表达式,则该函数就成为生成器函数. 当调用生成器函数时,它返回一个称为生成器的迭代器.然后该生成器控制 ...

  4. 薪资管理系统(Java面向对象思想)

    package com.test3; import java.util.*; import java.io.*; /** * @author qingfeng * 重要思想:面向对象思想(添加员工管理 ...

  5. linux更新系统时间

    查看时间 date 更新时间 yum install ntpdate ntpdate time.windows.com

  6. Android教程 -08 ToolBar的使用和主题的介绍

    ActionBar 简介 视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷 讲解ToolBar之前首先需要了解 ActionBar, 两者使用起来基本上一致. Android 3 ...

  7. 免费淘宝IP地址库简介及PHP/C#调用实例

    https://yq.aliyun.com/ziliao/25800?spm=a2c4e.11155472.0.0.68027abfcpFb7O 摘要: 本文讲的是免费淘宝IP地址库简介及PHP/C# ...

  8. PageHelper实现分页查询

    PageHelper是基于拦截器实现的myBatis分页插件 PageHelper的Github主页 : https://github.com/pagehelper/Mybatis-PageHelpe ...

  9. LEMP--如何在Ubuntu上安装Linux、Nginx、MySQL和PHP

    简介 LEMP是用来搭建动态网站的一组软件,首字母缩写分别表示Linux.Nginx(Engine-X).MySQL和PHP. 本文将讲述如何在Ubuntu安装LEMP套件.当然,首先要安装Ubunt ...

  10. 最小生成树prim、

    过年那几天确实没好好学习.在老家闲着也是闲着.可是就是没看书. 回来这几天又一直在弄个人博客.买域名云服务器备案什么的- -. 麻烦死了呢. 在腾讯花1块钱备案了一个网站www.goodgoodstu ...