一、包的使用(****)
1、什么是包?
包是一個含有__init__.py檔案的檔案夾,本質就是一個模塊,是用來被匯入的
2、為何要有包?
首次匯入包這種模塊,發生兩件事
(1)創建模塊的名稱空間,運行包下的__init__.py的檔案,將運行程序中產生的名字都丟入模塊的名稱空間中
(2)在當前位置拿到一個名字aaa,該名字指向__init__.py的名稱空間,即aaa.名字,名字是來自于__init__.py中
代碼:略
3. 相對匯入: 只能在包的目錄下
絕對匯入 :參照的是包的頂級目錄,只要在sys.path里面就行
4.相對匯入簡單方法: .代表當前檔案,..代表當前檔案的上一級,...代表上一級的上一級,依次類推,
ps:環境變數參照執行檔案路徑,sys.path參照的是呼叫者的環境變數
二、json&pickle(*****)
1、什么是序列化與反序列化
記憶體中某一類的資料---------------》特殊的格式
記憶體中某一類的資料《---------------特殊的格式
2、為何要序列化
1、存檔:把記憶體中的資料持久化到硬碟
2、跨平臺互動資料
在python中:
存檔=》推薦用pickle格式
跨平臺互動=》推薦用json格式
3、如何序列化
3.1 low版本的序列化與反序列化方式
1.序列化 items=["圣劍","蝴蝶","BKB"] dic_str=str(items) with open('db.txt',mode='wt',encoding="utf-8") as f: f.write(dic_str) 2.反序列化 with open('db.txt',mode='rt',encoding='utf-8') as f: data=f.read() # "['圣劍', '蝴蝶', 'BKB']" items=eval(data) print(items[0])
3.2:json
優點:跨平臺互動資料
缺點:無法識別所有的python資料型別(集合識別不了)
注意:json格式的字串里的內容不能包含單引號

序列化:dump/dumps
反序列化:load/loads
import json # 序列化方式一: t={"a":1,"b":2} # 字典=======》json格式的字串:"[1,2,3]" res=json.dumps(t) print(res,type(res)) with open("a.json",mode='wt',encoding='utf-8') as f: f.write(res) # json反序列化方式一: with open("a.json",mode='rt',encoding='utf-8') as f: data=f.read() dic=json.loads(data) print(dic,type(dic)) res=json.loads('{"k1":111}') print(res['k1']) # json序列化方式二: t={"a":1,"b":2} # 字典=======》json格式的字串:"[1,2,3]" with open("b.json",mode='wt',encoding='utf-8') as f: json.dump(t,f) # json反序列化方式二: with open("b.json",mode='rt',encoding='utf-8') as f: dic=json.load(f) print(dic,type(dic))
3.3:pickle
優點:可以識別所有python型別
缺點:只能用于python中,無法跨平臺互動
import pickle s = {1,2,3,4,5} #序列化: res=pickle.dumps(s) print(res,type(res)) with open('a.pkl',mode='wb') as f: f.write(res) #反序列化 with open('a.pkl',mode='rb') as f: data=f.read() s=pickle.loads(data) print(type(s))
也可用dump load
XML跟json差不多,比較笨重麻煩
三、hashlib
hash是一種演算法(md5\sha256\sha512等),我們為該演算法傳入內容,該演算法會計算得到一串hash值
hash值具備以下三個特點:
1、如果傳入的內容一樣,并且采用hash演算法也一樣,那么得到個hash值一定是一樣的
2、hash值的長度取決于采用的演算法,與傳入的文本內容的大小無關
3、hash值不可逆
ps:檔案校驗要選取點去校驗
=============md5===============
import hashlib #1 m=hashlib.md5() m.update("你好".encode('utf-8')) m.update("egon".encode('utf-8')) m.update("哈哈哈".encode('utf-8')) # "你好egon哈哈哈" res=m.hexdigest() print(res) #2 m1=hashlib.md5("你".encode('utf-8')) m1.update("好eg".encode('utf-8')) m1.update("on哈哈哈".encode('utf-8')) # "你好egon哈哈哈" res=m1.hexdigest() print(res) #總結:#1與#2的長度一樣 m2=hashlib.md5() with open('aaa.png',mode='rb') as f: for line in f: m2.update(line) print(m2.hexdigest()) #再加密(加鹽) pwd="123" m3=hashlib.md5() m3.update("天王蓋地虎".encode('utf-8')) m3.update(pwd.encode('utf-8')) m3.update("小雞燉蘑菇".encode('utf-8'))
==============sha256=============直接就加了鹽
#以上加密演算法雖然依然非常厲害,但時候存在缺陷,即:通過撞庫可以反解,所以,有必要對加密演算法中添加自定義key再來做加密 import hashlib m= hashlib.sha256('898oaFs09f'.encode('utf8')) m.update('alvin'.encode('utf8')) print (m.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7
模擬撞庫造成密碼泄露
import hashlib passwds=[ 'alex3714', 'alex1313', 'alex94139413', 'alex123456', '123456alex', 'a123lex', ] def make_passwd_dic(passwds): dic={} for passwd in passwds: m=hashlib.md5() m.update(passwd.encode('utf-8')) dic[passwd]=m.hexdigest() return dic def break_code(cryptograph,passwd_dic): for k,v in passwd_dic.items(): if v == cryptograph: print('密碼是===>\033[46m%s\033[0m' %k) cryptograph='aee949757a2e698417463d47acac93df' break_code(cryptograph,make_passwd_dic(passwds))模擬撞庫
==============hmac==============
hmac 模塊,它內部對我們創建 key 和 內容 進行進一步的處理然后再加密
import hmac h1=hmac.new('hello'.encode('utf-8'),digestmod='md5') h1.update('world'.encode('utf-8')) print(h1.hexdigest())#0e2564b7e100f034341ea477c23f283b
注意:
#要想保證hmac最終結果一致,必須保證: #1:hmac.new括號內指定的初始key一樣 #2:無論update多少次,校驗的內容累加到一起是一樣的內容 # 操作一 import hmac h1=hmac.new('hello'.encode('utf-8'),digestmod='md5') h1.update('world'.encode('utf-8')) print(h1.hexdigest()) # 0e2564b7e100f034341ea477c23f283b # 操作二 import hmac h2=hmac.new('hello'.encode('utf-8'),digestmod='md5') h2.update('w'.encode('utf-8')) h2.update('orld'.encode('utf-8')) print(h1.hexdigest()) # 0e2564b7e100f034341ea477c23f283b注意事項
---20---
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/75681.html
標籤:Python
上一篇:Python中的算數運算子
