random模塊
random.random()用於生成一個浮點數x,范圍為0 =< x < 1
import random >>>print(random.random()) 1.864001829819306
random.uniform(a,b)用於生成一個指定范圍內的隨機符點數,兩個參數其中一個是上限,一個是下限。
import random >>>print(random.uniform(1,10)) 7.559074479037658
#其上限與下限參數位置可以改變,但生成的數值還是在范圍之內
>>>print(random.uniform(10,1)) 5.487835445265534
random.randint(a,b),用於生成一個范圍內的整數,其中a必須大於等於b。
*
print(random.randint(1,5)) 3
print(random.randint(5,1) #引發一個ValueError
random.randrange([start],stop[,step])從一個集合中隨機取出一個數字
import random >>>print(random.randrange(1,100)) 22
>>>print(random.randrange(1,100,2)) #偶數集合在這里等效於random.choice(range(1,100,2))
16
random.choice(seq), seq是一個非空序列,如list、tuple、字符串等。如果為空,引發IndexError
import random >>>print(random.choice('abcdefg')) c >>>print(random.choice('tom','jerry','lisa')) tom >>>print(random.choice(('a','b','c'))) c
random.shuffle(x[,random]),將一個list打亂重新組合,list還是原list,沒有占用空間。
import random >>>alist = [1,2,3,4,5] >>>random.shuffle(alist) >>>print(alist) [2, 5, 4, 3, 1]
random.sample(seq,k),從指定序列中隨機獲取指定長度的片段
import random a = [1,2,3,4,5] >>>print(random,sample(a,3)) [3,1,5]
random更多方法參見官方docs:random module
hashlib模塊
這個模塊實現了一個通用的接口來實現多個不同的安全哈希和消息摘要算法。包括FIPS安全散列算法SHA1,SHA224,SHA256,SHA384和SHA512(在FIPS 180-2中定義)以及RSA的MD5算法
因為哈希在字節上工作,而不是字符,所以要使哈希工作在字符上,需要encode編碼為utf-8,再以十六進制形式進行摘要計算
import hashlib str = 'mzc19971103' md5 = hashlib.md5() md5.update(str[0:5].encode('utf-8')) md5.update(str[5:].encode('utf-8')) print(md5.hexdigest()) sha1 = hashlib.sha1() sha1.update('mzc19971103'.encode('utf-8')) print(sha1.hexdigest())
處理字符串時還可以加鹽,只要加鹽字符串不被泄露,無法根據md5解析出字符串
import hashlib def calc_md5(password): md5 = hashlib.md5() md5.update(password.encode('utf-8')) a = md5.hexdigest() return a db = {} def writeindb(username, password): db[username] = calc_md5(password+'woaini1997') #加鹽
print(db) def login(username, password): p = calc_md5(password+'woaini1997') if username in db and db[username] == p: print('login succ') else: print('username or passwd error') def register(): username = input('input ur username:') password = input('input ur password:') if username in db: print('account exit!') else: writeindb(username, password) def logininput(): username = input('plz input ur account:') password = input('plz input ur password:') if username != "" and password != "": login(username, password) else: print('uname or passwd not empty!') if __name__ == '__main__': while True: # username = input('plz input ur username:\n')
# password = input('plz input ur password:\n')
selec =input('register select:1 login select:2 quit plz select:3\n') if selec == '1': register() elif selec == '2': logininput() elif selec == '3': break
hashlib更多用法參見:Python-hashlib
base64
此模塊提供將二進制數據編碼為可打印ASCII字符並將此類編碼解碼回二進制數據的功能。它為在 RFC 3548中指定的編碼提供編碼和解碼功能,該編碼定義了Base16,Base32和Base64算法,以及事實上的標准Ascii85和Base85編碼.
base64可以直接編碼
import base64 def safe_base64_encode(a): #base64編碼函數
s = base64.b64encode(a) print(s) safe_base64_encode(b'legend')
#打印結果: b'bGVnZW5k'
解碼:
import base64 def safe_base64_decode(a):
s = base64.b64decode(a)print(a) safe_base64_decode(b'bGVnZW5k') #打印結果:
b'legend'
base64.b64encode()和base64.b64decode()參數必須是類字節對象
由於標准的Base64編碼后可能出現字符+和/,在URL中就不能直接作為參數,所以又有一種"url safe"的base64編碼,其實就是把字符+和/分別變成-和_
>>> base64.b64encode(b'i\xb7\x1d\xfb\xef\xff') b'abcd++//'
>>> base64.urlsafe_b64encode(b'i\xb7\x1d\xfb\xef\xff') b'abcd--__'
>>> base64.urlsafe_b64decode('abcd--__') b'i\xb7\x1d\xfb\xef\xff'
