一、hashlib模塊
hashlib模塊,主要用於加密相關的操作,在python3的版本里,代替了md5和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import hashlib #導入模塊 ######## md5 ########### m = hashlib.md5() #創建一個對象 m.update(b"hhf") #加密字符串 b代表byte,是把字符串轉換成byte類型,也可以用bytes()強制轉換 #m.update(bytes("hhf",encoding='utf-8')) #不指定encoding會報錯,TypeError: Unicode-objects must be encoded before hashing #bytes() 強制轉換為字節類型 print(m.digest()) #取加密后字符串 轉換成2進制格式hash print(m.hexdigest()) #16進制格式hash,比較常用 ###########-sha1 ######## hash = hashlib.sha1() hash.update(b'admin') print(hash.hexdigest()) # # ######## sha256 ######## hash = hashlib.sha256() hash.update(b'admin') print(hash.hexdigest()) # # ######## sha384 ######## hash = hashlib.sha384() hash.update(b'admin') print(hash.hexdigest()) # # ######## sha512 ######## hash = hashlib.sha512() hash.update(b'admin') print(hash.hexdigest()) #以上的加密方法都無法解密
二、Python操作數據庫
python3中操作mysql數據需要安裝一個第三方模塊,pymysql,使用pip install pymysql安裝即可,在python2中是MySQLdb模塊,在python3中沒有MySQLdb模塊了,所以使用pymysql
1.操作Mysql(oracle的和mysql的一樣,只不過是庫不一樣而已) pip install pymysql #安裝mysql模塊 import pymysql ip = '127.0.0.1' port = 3306 passwd = 'root' user = 'root' db = 'szz' #建立數據庫連接,指定數據庫的ip地址,賬號、密碼、端口號、操作的數據庫、字符集 conn=pymysql.Connect(host=ip,port=port,user=user,passwd=passwd,db=db,charset='utf8') #conn=pymysql.Connect(host='127.0.0.1',port=3306,user='root',passwd='root',db='szz',charset='utf8') cur = conn.cursor() #在連接上建立一個游標 #cur = conn.cursor(cursor=pymysql.cursors.DictCursor) #建立 一個游標,指定游標類型為字典類型,操作返回的結果為字典類型,這個方便取值 # 執行SQL,並返回收影響行數 #effect_row = cursor.execute("update students set name = 'hhf' where id = 1;") # 執行SQL,並返回受影響行數 #effect_row = cursor.executemany("insert into students (name,age) values (%s,%s); ", [("hhf", 18), ("aaa", 20)]) #sql = 'insert into nhy (id,name,sex) VALUE(80,"cc","女");' sql2 ='select * from nhy;' cur.execute(sql2) #查詢語句可以用這個 print(cur.fetchall()) #獲取表中所有數據,返回結果為List,如果是普通游標,無素為元組,字典游標,元素為字典 #print(cur.fetchone()) #獲取一條,一次獲取一條 #如果先fetchall()再fetchone是沒有結果的,因為游標已經移動到最后,用cur.scroll()來移動游標 #cur.scroll(0,mode='absolute') #absolute 絕對位置 就是把游標直接移到哪里 #cur.scroll(5,mode='relative') # reletive 相對位置 這個是相對於當前游標移動到哪里 #print(cur.fetchmany(3)) #獲取前n行數據 conn.commit() #提交,insert,delete,update必須提交才能生效, #有連接就有關閉 cur.close()#關閉游標 conn.close()#關閉連接 操作數據庫的函數 def OperationDb(host,user,passwd,db,sql,port=3306,charset='utf8'): conn = pymysql.connect( host=host,user=user,passwd=passwd,port=port, db=db,charset=charset ) #建立連接 cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#建立游標 cur.execute(sql)#執行sql #insert update delete語句需要commit()才能提交, #select 不需要 #判斷是什么語句 if sql.startswith('select'): res = cur.fetchall() else: conn.commit() res = 88 cur.close() conn.close() return res if __name__ == '__main__': ip = '127.0.0.1' port = 3306 passwd = 'root' user = 'root' db = 'szz' sql = 'insert into hhf(id,name,sex) VALUE (8,"hhff","未知");' # 插入 sql2 = 'select * from hhf limit 3;' # 查詢 sql3 = 'delete from hhf where id=8' # 刪除 sql4 = 'update hhf set name = "hhf" where id=8' # 更新 s = OperationDb(host=ip, port=port, user=user, passwd=passwd, db=db, sql=sql2) #也可以直接傳值, # s = OperationDb(host='127.0.0.1', user='root', passwd='root', db='szz', sql=sql2) print(s) 2.操作redis redis是一個nosql類型的數據庫,數據都存在內存中,有很快的讀寫速度,python操作redis使用redis模塊,pip安裝即可 pip install redis #安裝redis模塊 import redis,json ip = '127.0.0.1' port=6379 db=0 passwd = '' #連接redis r = redis.Redis(host=ip,port=port,db=db,password=passwd) r.set('age',18) #set值 redis里面存的數據全是字符串 r.set('name',[1,2,3,4]) #可以傳字典和list 最后會被轉成字符串 ,redis連接工具,reload查看更新的redis數據 r.set('name',{1,2,3,4}) #redis里面存的數據都是字符串,如果set的key已存在,就會更新值 r.setnx('ccc','hhf') #set值,如果key不存在會創建,key存在,不操作 r.setex('bbb','hahaha',15) # 可以設置key的失效時間,token的失效時間用的就是這個 name = r.get('name')#從redis中獲取的數據全是bytes類型 print(name) #結果b'{1, 2, 3, 4}' name = name.decode()#要使用decode方法給它轉成字符串才能繼續操作 print(r.get('aaaa'))#get不存在的key,就返回None r.mset(hhf1='haha',hhf2=1888) #批量設置值 r.mget('name','age') #批量獲取key r.delete('age') #刪除某個key r.delete('hhf1','hhf2')#刪除多個key print(r.keys()) #獲取所有的key,返回的結果是List,如果需要使用,得遍歷后decode() print(r.keys('*n*')) #獲取含有n的key # #下面操作哈希類型的key r.hset('hhf_session','name','hhf') r.hset('hhf_session','age',18) print(r.hget('hhf_session','age'))#獲取hhf_session里面age的值,必須兩個參數 print(r.hgetall('hhf_session'))#獲取哈希類型里面所有的值 r.delete('hhf_session') #把key刪除,包括下面的nam和age r.hdel('hhf_session') #會報錯,該方法里面的兩個參數是必填的 r.hdel('hhf_session','name')#刪除hhf_session下的name屬性 r.hsetnx('hname','key2','value23')#給name為hname設置key和value,和上面的不同的是key不存在的時候#才會set r.hmset('hname',{'k1':'v1','k2':'v2'})#批量設置哈希類型的key和value #如果key里面有冒號 就會多個文件夾 r.set('hhf:hhf','haha') r.set('hhf:cccc','aaa') r.hset('sss:hhf','hhf','123456') r.delete("hhf:cccc") 操作redis的函數: def OpRedis(host,passwd,k,v=False,port=6379,db=0): #此函數只實現了redis的set與get方法 只是針對str類型 #del方法及hset hget方法未實現 r = redis.Redis(host=host,password=passwd,port=port,db=db) #連接數據庫 if v:#判斷value是否傳值,如果傳了就說明是set方法 r.set(k,v) res = 88 else: res= r.get(k).decode() #因為從redis里面獲取到的數據都是字節類型的 ,要把它轉成字符串 #所以要用decode方法 return res