一 hashlibpython
主要用於字符串加密算法
1 編程
1 import hashlib 2 md5obj=hashlib.md5() # 實例化一個md5摘要算法的對象 3 md5obj.update('alex3714'.encode('utf-8')) # 使用md5算法的對象來操做字符串 4 ret = md5obj.hexdigest() #獲取算法的結果 hex+digest 16進制+消化 5 print(ret,type(ret)) 6 7 #加鹽 8 md5obj=hashlib.md5('hello'.encode('utf-8')) # 實例化一個md5摘要算法的對象,加鹽 9 md5obj.update('alex3714'.encode('utf-8'))# 使用md5算法的對象來操做字符串 10 ret=md5obj.hexdigest() 11 print(ret) 12 13 #動態加鹽 14 username='hu' 15 md5obj=hashlib.md5(username.encode('utf-8')) 16 md5obj.update('alex3714'.encode('utf-8'))# 使用md5算法的對象來操做字符串裏面必須是bytes類型 17 ret=md5obj.hexdigest() 18 print(ret)
二 logging日誌模塊json
經常使用的格式是windows
1 # logger對象的方式配置 2 logger = logging.getLogger() 3 # 吸星大法 4 5 # 先創造一個格式 6 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 7 # 往文件中輸入 8 fh = logging.FileHandler('log.log',encoding='utf-8') # 創造了一個能操做文件的對象fh 9 fh.setFormatter(formatter) # 高可定製化 10 logger.addHandler(fh) 11 logger.setLevel(logging.DEBUG) 12 sh = logging.StreamHandler() #sh是在屏幕上面顯示的 13 # sh.setFormatter(formatter1) 14 logger.addHandler(sh) 15 fh.setLevel(logging.ERROR) #文件裏面顯示error級別以上的 16 sh.setLevel(logging.DEBUG) #屏幕上面顯示debug級別以上的 17 18 logger.debug('logger debug message') 19 logger.info('logger info message') 20 logger.warning('logger warning message') 21 logger.error('程序出錯了') 22 logger.critical('logger critical message')
三 configparser 網絡
#該模塊適用於配置文件的格式與windows ini文件相似,能夠包含一個或多個節(section),每一個節能夠有多個參數(鍵=值)。工具
1 用configparser寫文件加密
1 import configparser 2 3 config = configparser.ConfigParser() 4 5 config["DEFAULT"] = {'ServerAliveInterval': '45', 6 'Compression': 'yes', 7 'CompressionLevel': '9', 8 'ForwardX11':'yes' 9 } 10 11 config['bitbucket.org'] = {'User':'hg'} 12 13 config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} 14 15 with open('example.ini', 'w') as configfile: 16 17 config.write(configfile)
2 用configparser查找文件spa
1 import configparser 2 3 config = configparser.ConfigParser() 4 5 #---------------------------查找文件內容,基於字典的形式 6 7 print(config.sections()) # [] 8 9 config.read('example.ini') 10 11 print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] 12 13 print('bytebong.com' in config) # False 14 print('bitbucket.org' in config) # True 15 16 17 print(config['bitbucket.org']["user"]) # hg 18 19 print(config['DEFAULT']['Compression']) #yes 20 21 print(config['topsecret.server.com']['ForwardX11']) #no 22 23 24 print(config['bitbucket.org']) #<Section: bitbucket.org> 25 26 for key in config['bitbucket.org']: # 注意,有default會默認default的鍵 27 print(key) 28 29 print(config.options('bitbucket.org')) # 同for循環,找到'bitbucket.org'下全部鍵 30 31 print(config.items('bitbucket.org')) #找到'bitbucket.org'下全部鍵值對 32 33 print(config.get('bitbucket.org','compression')) # yes get方法Section下的key對應的value
四 序列化debug
1 概念
# 什麼叫序列化呢?
# { '10100011':{'name':,age: ,class:},}
# 數據類型 —— 字符串的過程
# 何時要用序列化呢?
# 數據從內存到文件
# 數據在網絡上傳輸 字節 - 字符串 - 字典
# python中的序列化模塊都有哪些?
# json 通用的 支持的數據類型 list tuple dict
# pickle python中通用的 支持幾乎全部python中的數據類型
# shelve python中使用的便捷的序列化工具
2 json
1 #dumps和loads是和內存交互的 2 #dump和load是和文件交互的 3 import json 4 dic={'k':'v'} 5 # print(type(dic)) 6 # json_dic=json.dumps(dic) # 字典轉字符串的過程 ——序列化 7 # print(json_dic) 8 # print(dic) 9 # print(type(json_dic)) 10 # print(json.loads(json_dic)) #字符串 轉回其餘數據類型 —— 反序列化
注意:能夠dump屢次,可是不能屢次load
怎樣dump多條數據呢?
1 # 若是要dump多條數據 2 # 每一條數據先dumps一下 編程字符串 而後打開文件 write寫進文件裏 \n 3 # 讀取的時候按照標誌讀取或者按行讀 4 # 讀出來以後 再使用loads 5 6 with open('aaa','w')as f: 7 str_dic=json.dumps(dic) 8 f.write(str_dic+'\n') 9 f.write(str_dic + '\n') 10 f.write(str_dic + '\n') 11 with open('aaa')as f: 12 for line in f: 13 print(json.loads(line.strip()))
3 pickle
1 import pickle 2 class A: 3 def __init__(self,name): 4 self.name=name 5 alex=A('alex') 6 print(pickle.dumps(alex)) 7 8 with open('b_pickle','wb')as f: 9 pickle.dump(alex,f) 10 pickle.dump(alex, f) 11 with open('b_pickle','rb')as f: 12 while True: 13 try: 14 obj=pickle.load(f) 15 print(obj.name) 16 except EOFError: 17 break
總結
#1.pickle支持更多的數據類型# 2.pickle的結果是二進制# 3.pickle在和文件交互的時候能夠被屢次load