Python模块:configparser、hashlib、(subprocess)
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)的更多相关文章
- 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 ...
- python3之xml&ConfigParser&hashlib&Subprocess&logging模块
1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...
- python基础-7.3模块 configparser logging subprocess os.system shutil
1. configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 继承至2版本 ConfigParser,实现了更多智能特征,实现更有可预见性,新 ...
- python 模块之-hashlib
python 模块hashlib import hashlib m=hashlib.md5() # 生成MD5加密对象 m.update('jiami-string'.encode(' ...
- python 模块之hashlib
Hashlib模块 Python里面的hashlib模块提供了很多加密的算法,这里介绍一下hashlib的简单使用事例,用hashlib的md5算法加密数据,其他的所有加密算法使用方式上基本类似. h ...
- Python模块之OS,subprocess
1.os 模块 简述: os 表示操作系统 该模块主要用来处理与系统相关操作 最常用的是文件操作 打开 获取 写入 删除 复制 重命名 常用操作 os.getcwd() : 返回当前文件所在文件夹路径 ...
- python模块 os&sys&subprocess&hashlib模块
os模块 # os模块可根据带不带path分为两类 # 不带path print(os.getcwd()) # 得到当前工作目录 print(os.name) # 指定你正在使用的操作系统,windo ...
- json/pickle/shelve/xml/configparser/hashlib/subprocess - 总结
序列化:序列化指把内存里的数据类型转成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes为什么要序列化:可以直接把内存数据(eg:10个列表,3个嵌套字典)存到硬盘 ...
- 常用模块之hashlib,subprocess,logging,re,collections
hashlib 什么是hashlib 什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,M ...
- python模块学习 hashlib
一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...
随机推荐
- shell 2 解析
---- shell 3 /home/oracle/utility/macro/call_autopurge_arch.sh Description: Call purge archive log f ...
- IntelliJ IDEA安装与破解
1.软件下载 文中使用到的安装包下载 2.部署 安装一路下一步即可. 把下载的JetbrainsCrack-3.1-release-enc.jar放在安装目录的bin目录下 3.修改配置文件 在安装的 ...
- 实现通知栏Notification
课程Demo public class MainActivity extends Activity implements OnClickListener{ NotificationManager ma ...
- 掌握Spark机器学习库-08.2-朴素贝叶斯算法
数据集 iris.data 数据集概览 代码 import org.apache.spark.SparkConf import org.apache.spark.ml.classification.{ ...
- Visual SVN自动给文件加锁
在使用SVN作为版本控制器的时候,在VS里安装VISUALSVN插件,当修改文件公共文件的时候需要先Get Lock,如果对于多次操作这个鼠标操作显得是一些复杂,自动给文件加锁的操作实际是给文件加一个 ...
- 一致性hash学习
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...
- 理清楚HTML和DHTML和XML的概念
DHTML 不是 W3C 标准DHTML 指动态 HTML(Dynamic HTML).DHTML 不是由万维网联盟(W3C)规定的标准.DHTML 是一个营销术语 - 被网景公司(Netscape) ...
- swal用法
swal({ title: "确认删除?", text: "Your will not be able to recover this imaginary fil ...
- 10CSS高级滤镜
CSS高级滤镜 滤镜特效的应用 filter:滤镜属性(参数1,参数2,……) filter是滤镜属性选择符. 透明度——alpha opacity代表透明度等级,参数值从0到100,从完全透明 ...
- js模拟输入支付密码
html <div class="content"> <!--<div class="title">支付宝支付密码:</di ...