configparser模块:

此模块用于生成和修改常见配置文档。

一个常见配置文件(.ini的后缀名)格式如下:

[DEFAULT]    #  DEFAULT 是指后面的字典里都会默认有的内容
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

解析配置文件:

>>> import configparser
>>> config = configparser.ConfigParser() # 开始解析配置文件 # 生成ConfigParser的一个对象config
>>> config.sections() # 返回配置文件中的key
[] # 没有read之前 config.sections()是个空列表 >>> config.read('example.ini') # read之后, config这个变量就变成了类似字典的数据类型,用法跟字典差不多 # read()之后,就往config这个对象里面添加了 配置文件中的信息
['example.ini']
>>> config.sections() # read之后就得到了配置文件中的keys
['bitbucket.org', 'topsecret.server.com'] # 判断是否存在某个key
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False >>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
''
>>> for key in config['bitbucket.org']: print(key) # 循环某个key下的value # 由于DEFAULT是每个里面都有的 ,所以也会打印出DEFAULT里面的内容
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

增删改查语法:

配置文件如下:

[group1]
k1 = v1
k2:v2 [group2]
k1 = v1

对上面的配置文件增删改查(以Python2为例):(和字典的增删改查类似)

import ConfigParser

config = ConfigParser.ConfigParser()  # 在Python3中改成了小写 configparser
config.read('i.cfg') # ########## 读 ##########
secs = config.sections()
print secs
options = config.options('group2')
print options item_list = config.items('group2')
print item_list val = config.get('group1','key')
val = config.getint('group1','key') # ########## 改写 ##########
sec = config.remove_section('group1')
config.write(open(
'i.cfg', "w")) sec = config.has_section('wupeiqi')
sec = config.add_section('wupeiqi')
config.write(open('i.cfg', "w")) config.set('group2','k1',11111)
config.write(open('i.cfg', "w")) config.remove_option('group2','age')
config.write(open('i.cfg', "w"))

hashlib加密模块:

同一个内容经过 MD5加密后得到结果永远都是唯一的。而且经过MD5处理后的结果是不可逆的, 由密文无法反推明文,只能够利用撞库的方式去尝试反解,所以密码尽量设置的没有规律、复杂一点。

import hashlib

m = hashlib.md5()
m.update(b'hello') # 如下面关于update的解释, update括号里面必须是二进制格式的bytes,只有这种形式才是可编译的数据类型
m.update(b'neo') print(m.digest())
print(m.hexdigest()) # digest()和hexdigest()的区别看下面的解释 # 打印结果:
# b'\xad\xf1\x11\xe0&\x80;\xda\x00\xd6\xfc\xb6 \xf0&\x9c'
# adf111e026803bda00d6fcb620f0269c '''
hash.update(arg)
Update the hash object with the object arg, which must be interpretable as a buffer of bytes. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b). hash.digest()
Return the digest of the data passed to the update() method so far. This is a bytes object of size digest_size( the size of the resulting hash in bytes. ) which may contain bytes in the whole range from 0 to 255. hash.hexdigest()
Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
'''

字符串转成bytes的方法:

一:

a = "abc"
b = bytes(a,encoding="utf-8") # 利用bytes()把变量a转换成bytes类型

二:

>>> s = "abc"
>>> b = s.encode("utf-8")

.encode()用法:

subprocess模块:

标准写法:

subprocess.run(['df','-h'],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True)

涉及到管道|的命令需要这样写:

subprocess.run('df -h|grep disk1',shell=True) #shell=True的意思是这条命令直接交给系统去执行,不需要python负责解析

Popen()方法

常用参数:

  • args:shell命令,可以是字符串或者序列类型(如:list,元组)
  • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
  • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
  • shell:同上
  • cwd:用于设置子进程的当前目录
  • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。

Popen会在发起命令后立刻返回,而不等命令执行结果。

剩下的。。。真的憋不出该怎么写了,,,  以后慢慢学吧

Python模块:configparser、hashlib、(subprocess)的更多相关文章

  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. python3之xml&ConfigParser&hashlib&Subprocess&logging模块

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

  3. python基础-7.3模块 configparser logging subprocess os.system shutil

    1. configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 继承至2版本 ConfigParser,实现了更多智能特征,实现更有可预见性,新 ...

  4. python 模块之-hashlib

    python 模块hashlib import hashlib m=hashlib.md5()         # 生成MD5加密对象 m.update('jiami-string'.encode(' ...

  5. python 模块之hashlib

    Hashlib模块 Python里面的hashlib模块提供了很多加密的算法,这里介绍一下hashlib的简单使用事例,用hashlib的md5算法加密数据,其他的所有加密算法使用方式上基本类似. h ...

  6. Python模块之OS,subprocess

    1.os 模块 简述: os 表示操作系统 该模块主要用来处理与系统相关操作 最常用的是文件操作 打开 获取 写入 删除 复制 重命名 常用操作 os.getcwd() : 返回当前文件所在文件夹路径 ...

  7. python模块 os&sys&subprocess&hashlib模块

    os模块 # os模块可根据带不带path分为两类 # 不带path print(os.getcwd()) # 得到当前工作目录 print(os.name) # 指定你正在使用的操作系统,windo ...

  8. json/pickle/shelve/xml/configparser/hashlib/subprocess - 总结

    序列化:序列化指把内存里的数据类型转成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes为什么要序列化:可以直接把内存数据(eg:10个列表,3个嵌套字典)存到硬盘 ...

  9. 常用模块之hashlib,subprocess,logging,re,collections

    hashlib 什么是hashlib 什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,M ...

  10. python模块学习 hashlib

    一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...

随机推荐

  1. Snort里的规则目录文件解读(图文详解)

    不多说,直接上干货! snort的规则啊,是基于文本的,它通常存在于snort程序目录中或者子目录中,规则文件按照不同的组,进行分类存放的. snort的安装目录 [root@datatest sno ...

  2. JS-表格数据的添加与删除、搜索

    <!doctype html><html><head><meta charset="utf-8"><title>JS练习 ...

  3. 组合模式和php实现

    组合模式(有时候又叫做部分-整体模式): 将对象组合成树形结构以表示“部分整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性.它使我们树型结构的问题中,模糊了简单元素和复杂元素的概 ...

  4. CCF|碰撞的小球

    import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...

  5. oracle 代码块

    oracle 的代码块模板 declare --声明变量 begin --执行业务逻辑 exception --异常处理 end; --结束 注意:代码块每个sql语句结束都要加冒号 eg: --pl ...

  6. [Windows Server 2012] 阿里云镜像购买和使用方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将演示:阿里云镜像购买 ...

  7. iOS微信页面 长按图片出现【存储图像】和【拷贝】不出现【发送朋友】【保存图片】

    最近遇到一大坑.微信加载的页面中出现图片,长按图片时不出现默认的菜单[发送朋友]等而是[存储图像]和拷贝. 原因:正常在页面中长按图片是没有问题的,但是如果你的页面嵌入了ifram然后又长按在ifra ...

  8. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...

  9. ZooKeeper系列(三)

    前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...

  10. jQuery 点击 星星评分

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...