码迷,mamicode.com
首页 > 其他好文 > 详细

hashlib,configparser,logging模块

时间:2017-08-17 17:17:13      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:模块   bitbucket   包含   解密   close   level   ons   --   res   

一、常用模块二

hashlib模块

hashlib提供了常见的摘要算法,如md5和sha1等等。

那么什么是摘要算法呢?摘要算法又称为哈希算法、散列算法。它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)。

注意:摘要算法不是一个解密算法。(摘要算法,检测一个字符串是否发生了变化)

应涂:1.做文件校验

   2.登录密码

      密码不能解密,但可以撞库,用‘加盐’的方法就可以解决撞库的问题。所有以后设置密码的时候要设置的复杂一点。

技术分享
 1 import hashlib
 2 # md5_obj = hashlib.md5()  未加盐
 3 md5_obj = hashlib.md5(‘nezha‘.encode(‘utf-8‘)) #加盐后(就让你的密码更牢固了)
 4 md5_obj.update(‘123456‘.encode(‘utf-8‘))
 5 print(md5_obj.hexdigest())
 6 md5_obj.update(‘hello‘.encode(‘utf-8‘))
 7 print(md5_obj.hexdigest())
 8 # -----------
 9 user = ‘haiyan‘
10 password = ‘123456‘
11 md5_obj= hashlib.md5(user.encode(‘utf-8‘))  #加盐(哪怕被人的密码和你的密码一样,
12 # 那你加盐以后就只有你的用户名对应的是你的密码了)
13 md5_obj.update(password.encode(‘utf-8‘))
14 print(md5_obj.hexdigest())
用户密码
技术分享
 1 import hashlib
 2 md5_obj = hashlib.md5()
 3 import os
 4 filesize = os.path.getsize(‘filename‘)  #文件大小
 5 f = open(‘filename‘,‘rb‘)
 6 while filesize>0:
 7     if filesize > 1024:
 8         content = f.read(1024)
 9         filesize -= 1024
10     else:
11         content = f.read(filesize)
12         filesize -= filesize
13     md5_obj.update(content)
14 # for line in f:
15 #     md5_obj.update(line.encode(‘utf-8‘))
16 md5_obj.hexdigest()
文件校验

configparser模块

该模块适用于配置文件的格式与windows  ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

1.创建文件

技术分享
 1 import configparser
 2 config = configparser.ConfigParser()
 3 config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
 4                       ‘Compression‘: ‘yes‘,
 5                      ‘CompressionLevel‘: ‘9‘,
 6                      ‘ForwardX11‘:‘yes‘
 7                      }
 8 config[‘bitbuck et.org‘] = {‘User‘:‘hg‘}
 9 config[‘topsecret.server.com‘] = {‘Host Port‘:‘50022‘,‘ForwardX11‘:‘no‘}
10 with open(‘example.ini‘, ‘w‘) as configfile:
11    config.write(configfile)
创建文件

2.查找文件

技术分享
 1 import configparser
 2 config = configparser.ConfigParser()
 3 # print(config.sections())
 4 config.read(‘example.ini‘)
 5 print(config.sections())  #读出来的是文件里面的组,
 6 # 而且里面的[DEFAULT]组没有显示出来
 7 print(‘bytebong.com‘ in config) # False
 8 print(‘bitbucket.org‘ in config) # True
 9 print(config[‘bitbucket.org‘]["user"])  # hg
10 print(config[‘DEFAULT‘][‘Compression‘]) #yes
11 print(config[‘topsecret.server.com‘][‘ForwardX11‘])  #no
12 print(config[‘bitbucket.org‘])          #<Section: bitbucket.org>
13 for key in config[‘bitbucket.org‘]:     # 注意,有default会默认default的键
14     print(key)
15 print(config.options(‘bitbucket.org‘))  # 同for循环,找到‘bitbucket.org‘下所有键
16 print(config.items(‘bitbucket.org‘))    #找到‘bitbucket.org‘下所有键值对
17 print(config.get(‘bitbucket.org‘,‘compression‘)) # yes       get方法Section下的key对应的value
查找文件

3.增删改操作

技术分享
1 import configparser
2 config = configparser.ConfigParser()
3 config.read(‘example.ini‘)
4 config.add_section(‘yuan‘)
5 # config.remove_section(‘bitbucket.org‘) #删除组
6 # config.remove_option(‘topsecret.server.com‘,"forwardx11") #删除组里面的项
7 config.set(‘topsecret.server.com‘,‘k1‘,‘11111‘)
8 config.set(‘yuan‘,‘k2‘,‘22222‘)
9 config.write(open(‘new2.ini‘, "w"))
增删改操作

logging模块

 

hashlib,configparser,logging模块

标签:模块   bitbucket   包含   解密   close   level   ons   --   res   

原文地址:http://www.cnblogs.com/haiyan123/p/7382604.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!