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. 转 SQL - 字符串中的转义字符

    一位同事在使用SQL处理一串字符时,出现一个意料之外的问题:这个字符串中包括字符‘&’.我们先看一下现象:     SQL> select * from v$version;     B ...

  2. 转 PHP抽象类:无法实例化 (不错)

    http://blog.csdn.net/kaituozheboke/article/details/52183726 一.抽象类: 无法实例化 类前加 abstract, 此类就成为抽象类,无法实例 ...

  3. chromedriver与chrome版本对应

    今天把手头有的一些关于selenium测试的资源整理了一下,分享出来. 1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是 ...

  4. LD_LIBRARY_PATH与-L的关系以及延伸

    最近跟同学讨论c++在编译时g++ -L.. 和LD_LIBRARY_PATH的问题,今天在做一个东西的时候发现,我对这两个东西的理解是错误的,经过一番研究,写下我对这些东西的想法,如果有不对的地方, ...

  5. Android开发使用的常见第三方框架汇总

    本文转载:http://blog.csdn.net/liuhaomatou/article/details/44857005 1.volley 项目地址 https://github.com/sman ...

  6. ASP.Net TextBox只读时不能通过后台赋值取值

    给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 1.不设置ReadOnly,设置onfocus=this.blur() ...

  7. 提高SQL查询效率 的10大方法

    一.查询条件精确,针对有参数传入情况 二.SQL逻辑执行顺序 FROM–>JOIN–>WHERE–>GROUP–>HAVING–>DISTINCT–>ORDER–& ...

  8. Sass的的使用一

    sass -v 检测是否安装 Sass 成功 gem update sass 更新 Sass gem uninstall sass 删除/卸载 Sass 的编译有多种方法: 1.命令编译2.GUI工具 ...

  9. 原生 js 整理

    常见的事件 window.event     代表着,事件的状态,只有在事件的过程中才有效.

  10. 开放API接口

    [开放API]——知乎.博客园等开放API接口(更新ing)   Cnodejs.org: https://cnodejs.org/api/ 和风天气: http://docs.heweather.c ...