1、configparser模块

配置文件如下:

# 注释1
; 注释2

[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2]
k1 = v1

读取

 1 import configparser
 2 
 3 config=configparser.ConfigParser()
 4 config.read('a.cfg')
 5 
 6 #查看所有的标题
 7 res=config.sections() #['section1', 'section2']
 8 print(res)
 9 
10 #查看标题section1下所有key=value的key
11 options=config.options('section1')
12 print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
13 
14 #查看标题section1下所有key=value的(key,value)格式
15 item_list=config.items('section1')
16 print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
17 
18 #查看标题section1下user的值=>字符串格式
19 val=config.get('section1','user')
20 print(val) #egon
21 
22 #查看标题section1下age的值=>整数格式
23 val1=config.getint('section1','age')
24 print(val1) #18
25 
26 #查看标题section1下is_admin的值=>布尔值格式
27 val2=config.getboolean('section1','is_admin')
28 print(val2) #True
29 
30 #查看标题section1下salary的值=>浮点型格式
31 val3=config.getfloat('section1','salary')
32 print(val3) #31.0

改写

 1 import configparser
 2 
 3 config=configparser.ConfigParser()
 4 config.read('a.cfg',encoding='utf-8')
 5 
 6 
 7 #删除整个标题section2
 8 config.remove_section('section2')
 9 
10 #删除标题section1下的某个k1和k2
11 config.remove_option('section1','k1')
12 config.remove_option('section1','k2')
13 
14 #判断是否存在某个标题
15 print(config.has_section('section1'))
16 
17 #判断标题section1下是否有user
18 print(config.has_option('section1',''))
19 
20 
21 #添加一个标题
22 config.add_section('egon')
23 
24 #在标题egon下添加name=egon,age=18的配置
25 config.set('egon','name','egon')
26 config.set('egon','age',18) #报错,必须是字符串
27 
28 
29 #最后将修改的内容写入文件,完成最终的修改
30 config.write(open('a.cfg','w'))

基于上述方法,添加一个ini文档

 1 import configparser
 2   
 3 config = configparser.ConfigParser()
 4 config["DEFAULT"] = {'ServerAliveInterval': '45',
 5                       'Compression': 'yes',
 6                      'CompressionLevel': '9'}
 7   
 8 config['bitbucket.org'] = {}
 9 config['bitbucket.org']['User'] = 'hg'
10 config['topsecret.server.com'] = {}
11 topsecret = config['topsecret.server.com']
12 topsecret['Host Port'] = '50022'     # mutates the parser
13 topsecret['ForwardX11'] = 'no'  # same here
14 config['DEFAULT']['ForwardX11'] = 'yes'
15 with open('example.ini', 'w') as configfile:
16    config.write(configfile)
17 
18 基于上述方法添加一个ini文档

2、hashlib模块

 1、什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法),该算法接受传入的内容,经过运算得到一串hash值

 2、hash值的特点是:

  1 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验

  2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码

  3 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的

 

 hash算法就像一座工厂,工厂接收你送来的原材料(可以用m.update()为工厂运送原材料),经过加工返回的产品就是hash值

 

 1 import hashlib
 2  
 3 m=hashlib.md5()# m=hashlib.sha256()
 4  
 5 m.update('hello'.encode('utf8'))
 6 print(m.hexdigest())  #5d41402abc4b2a76b9719d911017c592
 7  
 8 m.update('alvin'.encode('utf8'))
 9  
10 print(m.hexdigest())  #92a7e713c30abbb0319fa07da2a5c4af
11  
12 m2=hashlib.md5()
13 m2.update('helloalvin'.encode('utf8'))
14 print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af
15 
16 '''
17 注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样
18 但是update多次为校验大文件提供了可能。
19 '''

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

1 import hashlib
2  
3 # ######## 256 ########
4  
5 hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
6 hash.update('alvin'.encode('utf8'))
7 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7

模拟撞库破解密码

 1 import hashlib
 2 passwds=[
 3     'alex3714',
 4     'alex1313',
 5     'alex94139413',
 6     'alex123456',
 7     '123456alex',
 8     'a123lex',
 9     ]
10 def make_passwd_dic(passwds):
11     dic={}
12     for passwd in passwds:
13         m=hashlib.md5()
14         m.update(passwd.encode('utf-8'))
15         dic[passwd]=m.hexdigest()
16     return dic
17 
18 def break_code(cryptograph,passwd_dic):
19     for k,v in passwd_dic.items():
20         if v == cryptograph:
21             print('密码是===>\033[46m%s\033[0m' %k)
22 
23 cryptograph='aee949757a2e698417463d47acac93df'
24 break_code(cryptograph,make_passwd_dic(passwds))
25 
26 模拟撞库破解密码

python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密:

1 import hmac
2 h = hmac.new('alvin'.encode('utf8'))
3 h.update('hello'.encode('utf8'))
4 print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940

注意注意注意!

 1 #要想保证hmac最终结果一致,必须保证:
 2 #1:hmac.new括号内指定的初始key一样
 3 #2:无论update多少次,校验的内容累加到一起是一样的内容
 4 
 5 import hmac
 6 
 7 h1=hmac.new(b'egon')
 8 h1.update(b'hello')
 9 h1.update(b'world')
10 print(h1.hexdigest())
11 
12 h2=hmac.new(b'egon')
13 h2.update(b'helloworld')
14 print(h2.hexdigest())
15 
16 h3=hmac.new(b'egonhelloworld')
17 print(h3.hexdigest())
18 
19 '''
20 f1bf38d054691688f89dcd34ac3c27f2
21 f1bf38d054691688f89dcd34ac3c27f2
22 bcca84edd9eeb86f30539922b28f3981
23 '''
24 
25 注意!注意!注意

3、suprocess模块

 1 import  subprocess
 2 
 3 '''
 4 sh-3.2# ls /Users/egon/Desktop |grep txt$
 5 mysql.txt
 6 tt.txt
 7 事物.txt
 8 '''
 9 
10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
12                  stdout=subprocess.PIPE)
13 
14 print(res.stdout.read().decode('utf-8'))
15 
16 
17 #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
19 print(res1.stdout.read().decode('utf-8'))
20 
21 
22 #windows下:
23 # dir | findstr 'test*'
24 # dir | findstr 'txt$'
25 import subprocess
26 res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函数备课',shell=True,stdout=subprocess.PIPE)
27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
28                  stdout=subprocess.PIPE)
29 
30 print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码

 

 posted on 2018-08-06 21:27  二十二a  阅读(128)  评论(0)    收藏  举报