学习笔记六 数据库操作,加密
一.安装第三方模块
两种方式:
1、pip install xxx
pip问题:
1、提示没有pip命令的,
把python的安装目录、安装目录下面的scripts目录加入到环境变量里面
2、Unknown or unsupported command 'install'
1、先执行 where pip
2、找到不是python目录下叫pip的文件,改成其他的名字就ok了
3、电脑里面装了多个python
python2 python3
1、先去python2安装目录里面把python.exe的名字改成python2.exe
2、再把python3安装目录里面的python.exe名字改成python3.exe
python2 -m pip install xxx
python3 -m pip install xxx
2、手动安装
1、.whl
pip install /Users/nhy/Downloads/PyMySQL-0.9.2-py2.py3-none-any.whl
2、.tar.gz
先解压
然后在命令行里面进入到这个目录下
python setup.py install
二.数据库操作
取一条数据fetchone:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test',charset='utf8') 3 cur = conn.cursor() #建立游标 4 sql='select * from app_myuser;' 5 cur.execute(sql)#只是执行sql,并不会返回数据 6 #print(cur.fetchall()) #获取到所有返回的数据 7 print(cur.fetchone()) #只取一条数据 8 cur.close() 9 conn.close()
(1122, 'liuyana03', '123658', 1)
取所有数据fetchall:
import pymysql conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
cur = conn.cursor() #建立游标 sql='select * from app_myuser;' cur.execute(sql)#只是执行sql,并不会返回数据 print(cur.fetchall()) #获取到所有返回的数据 #print(cur.fetchone()) #只取一条数据 cur.close() conn.close()
((1122, 'liuyana03', '123658', 1), (1123, 'wpp', '1234567', 1), (1128, 'wangcan', 'hhhh', 1), (1129, 'wangcan1', 'hhhh', 1), (1185, 'wangliwen', '123456', 1), (1186, 'wangzhihua', '123456', 1), (1187, 'zyb123', '456789', 1), (1188, 'nhy1233', '456789', 1), (1190, 'aaaa', '1234566', 1), (1191, 'zzf', '123456', 1), (1194, 'yulin123', '411111156789', 1), (1195, 'cui123', '000789', 1), (1197, 'nhy001', '456789', 1), (1198, 'liuhn', '123123', 1), (1200, 'zyq123', '123456', 1), (1201, 'hushaoyan', '123456', 1), (1202, 'qml', '456789', 1), (1203, 'qiuqiu', '456789', 1), (1204, 'yj123', '456789', 1), (1208, 'est', '234234', 1), (1211, 'dyx123', '456789', 1), (1214, 'liucuiyun', '123456', 1), (34353, 'lyc', '123456', 0), (34360, '猜我是谁', '1234566', 1), (34363, 'tanailing', '456789', 2), (34365, 'zjh', '456789', 1), (34366, 'gzo', '123456', 1), (34368, 'liuchang', '123456', 1), (34370, 'yxj999', '123456', 1), (34371, 'tianlixin12', '400789', 1), (34372, 'yh', '456789', 1), (34374, 'dapengpeng', '456789', 1), (34375, 'n1233333', '123456', 1), (34377, 'qqcttp', '123456', 1), (34379, 'liuchang2', '123456', 1), (34382, 'zhang333', '34343', 3), (34384, 'zhizhi123', '456789', 1), (34385, 'tian123', '400789', 1), (34388, 'yaya', '123098', 1), (34389, 'tianrrrrr12', '400789', 1), (34391, 'nhy123', '456789', 1), (34392, 'lyc123', '123456', 0), (34393, 'qml001', '456789', 1), (34399, 'zhangjuanho', '456789', 1), (34406, 'm1233333', '123456', 1), (34415, 'gzo1', '123456', 1), (34416, 'xuxinya', '123456', 1), (34417, 'yh1', '456789', 1), (34420, 'est1', '234234', 1), (34421, 'yy1233333', '123456', 1), (34424, 'zhangjh', '456789', 2), (34425, 'zhangjuh', '456789', 2), (34426, 'est11', '234234', 1), (34430, 'nhuy1233', '456789', 1), (34433, 'yy1233', '123456', 1), (34435, 'test2', '234234', 1), (34442, 'zyb1234', '456789', 1), (34444, 'zyb12345', '456789', 1), (34446, 'yh11', '456789', 1), (34465, 'zhizhi456', '456789', 1), (34479, '你是猪', '123456', 1), (34484, 'xuxinya1', '123456', 1), (34485, 'cuimeiping', '100789', 1), (34486, 'nhuey1233', '456789', 1), (34488, 'y951233', '123456', 1), (34490, 'hgh', '4567890', 1), (34491, 'zzf1', '123456', 1), (34493, 'yjj123', '456789', 1), (34494, 'lyy', '12333', 1), (34495, 'yanran', '456789', 1), (34496, 'cuimeiping1', '100789', 1), (34497, 'nhy002', '456789', 1), (34500, 'qml002', '456789', 1), (34501, 'fff', '456789', 1), (34502, 'lyy1', '12333', 1), (34503, 'ffff', '456789', 1), (34504, 'lyy2', '12333', 1), (34507, 'chunji111', '456789', 10), (34513, 'nhue1233', '456789', 1), (34515, 'lyy5', '12333', 1), (34519, 'zyh1', '123456', 1), (34522, 'test202', '234234', 1), (34524, 'test203', '234234', 1), (34525, 'nhy1230', '456789', 1), (34527, 'zyh11', '123456', 1), (34531, 'khy7744', '1234456', 3), (34534, 'liucuiyun01', '123456', 1), (34535, 'liu0001', '123456', 1), (34536, 'liu0002', '123456', 1), (34540, 'liucuiyun03', 'e10adc3949ba59abbe56e057f20f883e', None), (34542, 'liucuiyun05', 'e10adc3949ba59abbe56e057f20f883e', None), (34543, 'liucuiyun06', 'c33367701511b4f6020ec61ded352059', 1), (34544, 'zyb000', '123456', 1), (34545, 'zyb888', '827ccb0eea8a706c4c34a16891f84e7b', 1), (34546, 'zyq111', 'e10adc3949ba59abbe56e057f20f883e', 1), (34547, 'zyb999', 'e10adc3949ba59abbe56e057f20f883e', 1), (34549, 'qml003', '456789', 1), (34550, 'zhy999', '123456', None), (34551, 'zyh998', '123456', None), (34553, 'zyh997', '123456', None), (34554, 'zyh996', '123456', None), (34555, 'zyh990', '123456', None), (34556, 'wtt', 'e10adc3949ba59abbe56e057f20f883e', 1), (34557, 'wttwtt123', '7d37e3cbff8e2f569ee74d9393c5a4a1', 1), (34568, 'wtt12888', 'f7177163c833dff4b38fc8d2872f1ec6', 1), (34569, 'hsy123', '456789', 1), (34584, 'test666', '123', 1), (34597, 'rrr', '28dd2c7955ce926456240b2ff0100bde', 1), (34598, 'wtt8888', '2a38a4a9316c49e5a833517c45d31070', 1), (34599, 'a', '7c5398c9fd9b0376af683ce0681f93b3', 1), (34601, 'b', 'c1bcf3bfb0c27f357d8f9385dc2ee38b', 1), (34602, 'cc', 'cb39f3d2456ef872af118512d1a0c2f5', 1), (34603, 'qmltest', 'qmltest', 1), (34605, 'jjj', 'jjj', 1), (34611, 'qmlqmlqml', 'qmlqmlqml', 1), (34615, 'hr001', 'c70978b200e6f7507cd55e341f022550', 1), (34618, 'qml123', 'qml123', 1), (34619, 'username', 'news_pwd', 1), (34620, 'rfdsfsdf', 'fsdfsdf', 1), (34622, 'hr002', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34623, 'hzh', 'fc49d07911be544a10e819426734d03a', 1), (34624, 'hr003', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34625, 'zyh666', '123456', None), (34626, 'hr004', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34627, 'test666gaga', '123', 1), (34632, 'tessssstt', 'abe45d28281cfa2a4201c9b90a143095', 1), (34633, 'tessstt', 'abe45d28281cfa2a4201c9b90a143095', 1), (34634, 'tess1', 'ba88c155ba898fc8b5099893036ef205', 1), (34636, 'ytest', '8b634156edde77e407764d5166e34d20', 1), (34637, 'testss', '8b634156edde77e407764d5166e34d20', 1), (34638, 'tess12', '8b634156edde77e407764d5166e34d20', 1), (34639, 'liuhn111', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34640, 'hate', '8b634156edde77e407764d5166e34d20', 1), (34649, 'liuhn001', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34651, 'hushaoyan1', '123456', 1), (34652, 'liuhn002', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34653, 'zyh889', '251b57454f0c600aa20a51ab03756845', None), (34654, 'zyh678', '251b57454f0c600aa20a51ab03756845', None), (34655, 'nhy1123', '456789', 1), (34657, 'nhy11263', '456789', 1), (34658, 'hdl1', '456789', 1), (34660, 'hdl2', '456789', 1), (34661, 'hdl3', '456789', 1), (34663, 'hdl4', '456789', 1), (34664, 'zhangyq', 'e10adc3949ba59abbe56e057f20f883e', 1), (34666, '111', '111', None), (34678, '1111', '111', None), (34679, '5555', '11', None), (34680, 'chun111', '456789', 10), (34682, 'chu3111', '456789', 10), (34683, 'ch2u3111', '456789', 10), (34684, 'zha11san', '3424i', 1), (34686, '2324213', '321', 1), (34687, 'sf4sdf', '232124', 1), (34688, 'dasd45', '453', 1), (34689, 'sdfsdf', '45345', 1), (34690, 'TPRERE', 'cd294ce73f97a8f66b8a6fae31d32dfe', 1), (34691, 'xiaohe123', '123456', 1), (34692, 'hushaoyan2', 'e10adc3949ba59abbe56e057f20f883e', 1), (34693, 'ghghgh', '<md5 HASH object @ 0x00000000029', 1), (34694, 'dasdas34', 'b3ddbc502e307665f346cbd6e52cc10d', 1), (34695, 'shan02', 'e10adc3949ba59abbe56e057f20f883e', 1), (34696, 'zhuxi001', '123123', 1), (34698, 'zhuxi002', '123123', 1), (34699, 'zhuxi003', '123123', 1), (34700, 'zhuxi004', '123123', 1), (34708, '1', '123123', 0), (34711, 'test1', '8b634156edde77e407764d5166e34d20', 1), (34712, 'yutest', 'abe45d28281cfa2a4201c9b90a143095', 1), (34714, 'hsy001', 'e10adc3949ba59abbe56e057f20f883e', 1), (34718, 'hhh', '4567890', 1), (34721, 'kkk', '4567890', 1), (34726, 'sss', '4567890', 1), (34729, 'kk', 'dead7d2efb1e8e354edf3723c1e700f7', 1), (34734, "'", '178a1dfdbd13268d265fee2c9925e564', 1), (34735, "''", 'dead7d2efb1e8e354edf3723c1e700f7', 1), (34737, '小哥哥', 'f594768cb81bf3193fc87235ffa689ab', 1), (34738, 'fffg', '476be781e259f92e9d7c376c787df5e9', 1), (34739, '1122', '52c69e3a57331081823331c4e69d3f2e', 1), (34740, 'hhhh', '52c69e3a57331081823331c4e69d3f2e', 1))
以上的数据均是以元组形式返回,如果需要以字典形式返回,需要在建立游标的时候指定关键字,当有多个返回的时候,是将所有的dict放在一个list里返回:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标,结果以dict形式返回
4 sql='select * from app_myuser;'
5 cur.execute(sql)#只是执行sql,并不会返回数据
6 #print(cur.fetchall()) #获取到所有返回的数据
7 print(cur.fetchone()) #只取一条数据
8 cur.close()
9 conn.close()
{'id': 1122, 'username': 'liuyana03', 'passwd': '123658', 'is_admin': 1}
fetchone与fetchall不能同时使用,返回的结果在一个结果集中,cur.execute(sql)结果有10条,当fetone先执行,取走一条,fetchall再去取,只能取到剩下的9条,而非10条;反之,fetchall先去取,则10条全部取走,fetchone再去取则一条也取不到,获得None
数据库插入后,虽然执行了但实际数据不会提交到数据库,必须执行commit操作:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标
4 sql='insert into app_myuser (username,passwd,is_admin) values ("nhy123","456789",1);'
5 cur.execute(sql)#只是执行sql,并不会返回数据
6 conn.commit()
7 cur.close()
8 conn.close()
如果不想每次都手动提交,可以再connect的时候设置autocommit,以上代码等同于:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test',autocommit=True)
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标
4 sql='insert into app_myuser (username,passwd,is_admin) values ("nhy123","456789",1);
5 cur.execute(sql)#只是执行sql,并不会返回数据
6 cur.close()
7 conn.close()
三.加密
1 import hashlib #python3 2 #import md5 #python2 3 s = 'admin123wefsdfsdddddddddd345345dsfdsfcsafsadfds' 4 #bytes 5 m = hashlib.md5(s.encode()) 6 print(m.hexdigest()) 7 m = hashlib.sha224(s.encode()) 8 print(m.hexdigest()) 9 m = hashlib.sha256(s.encode()) 10 print(m.hexdigest()) 11 m = hashlib.sha512(s.encode()) 12 print(m.hexdigest())
b38bbea537ed1405e53e86c274337573
4b22dc6e3ae830d943270da0b82a12836c78c6e9f4f53c2ed229e07a
362b4b094e1cf27ccc3943e93be48c2097060194c5c679a018908fe2bf7c65a7
e56e13d0ceea515a50beed24b910e52b8363e41a85a2f4485c7ea49c1d79cf2f37e7167aa635478c05ab4e06a5262d766f30eceaf5f098bf8ae3ee881bcadd7a
需要导入hashlib包,字符串不能直接加密,加密之前需要进行encode编码成byte后才能进行加密,并且加密之后是无法逆操作解密的。
为了增加暴力破解的难度,还需要加盐操作:
1 def my_md5(s,salt=''): 2 s = s+salt 3 news = str(s).encode() 4 m = hashlib.md5(news) 5 return m.hexdigest() 6 7 print(my_md5("degkjhsd"))
9663eb1ee05a1cf7f89bb1b47c0ebbeb
四.递归
递归通俗的解释就是自己调自己,Python有递归次数限制,默认最大次数为1000
递归比较方便的一种用法是遍历文件目录查找某个指定文件,使用os.walk:
1 import os 2 def find_movie(keyWord,path=r"C:/Users/Administrator/Desktop/"): 3 for cur_path, cur_dirs, cur_files in os.walk(path): 4 for file in cur_files: 5 if file.endswith(keyWord): 6 print(cur_path) 7 find_movie('.html')
C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/print C:/Users/Administrator/Desktop/print C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print3
五.内置函数
1.math模块:
1 import math 2 res1 = max([1,2,3,4]) 3 res2 = sum(range(1,101)) 4 print(res1) 5 print(res2) 6 print(chr(66)) #把数字转成对应ascii码表里面对应的值 7 print(ord('A')) #把字母转成对应ascii码表里面对应的数字 8 print(round(80.23556, 2))#返回浮点数的四舍五入值
4 5050 B 65
80.24
2.dir:查看某个对象里有哪些方法
import random print(dir(list)) print(dir(random))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
3.bool:进行布尔转换,结果为True或False
1 print(bool(None)) #转布尔类型的 True False 2 print(bool('')) #转布尔类型的 True False 3 print(bool([])) #转布尔类型的 True False 4 print(bool({})) #转布尔类型的 True False 5 print(bool(())) #转布尔类型的 True False 6 print(bool(0)) #转布尔类型的 True False 7 print(bool(123)) #转布尔类型的 True False 8 print(bool([1,3,4])) #转布尔类型的 True False 9 print(bool([1.5])) #转布尔类型的 True False 10 #非空即真,非0即真
False
False
False
False
False
False
True
True
True
4.sort 与 sorted :
- sort 是应用在 list 上的方法,属于列表的成员方法,将原list排序,返回None;sorted是一个内建函数,可以对所有可迭代的对象进行排序操作。
- list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
- sort使用方法为ls.sort(),而sorted使用方法为sorted(ls)
1 s=[3,4,5,6,1,2,8] 2 s.sort() 3 print(s) 4 s='356128' 5 print(sorted(s,reverse=True))#排序reverse=True降序,False为升序(默认) 6 print(list(reversed(s)))#反转
[1, 2, 3, 4, 5, 6, 8] ['8', '6', '5', '3', '2', '1'] ['8', '2', '1', '6', '5', '3']
5.zip:
用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。
1 name = ['nhy','lyl','qlm'] 2 money = [50,100,1000,50,50] 3 print(zip(name,money)) 4 print(list(zip(name,money)))
<zip object at 0x00000000025EB548> [('nhy', 50), ('lyl', 100), ('qlm', 1000)]
通过*可以解压:
1 a = [1,2,3] #此处可迭代对象为列表 2 b = [4,5,6] 3 4 v = zip(a, b) # 压缩 5 print(list(v)) 6 7 w = zip(*zip(a, b)) # 解压 8 print(list(w))
[(1, 4), (2, 5), (3, 6)]
[(1, 2, 3), (4, 5, 6)]
1 #v1,v2,v3可是是任何可迭代对象,如:字符串、列表、元祖、字典 2 v1 = {1:11,2:22} #此处可迭代对象为字典 3 v2 = {3:33,4:44} 4 v3 = {5:55,6:66} 5 6 v = zip(v1,v2,v3) #压缩 只讲key压缩 7 print(list(v)) 8 9 w = zip(*zip(v1,v2,v3)) #解压 10 print(list(w))
[(1, 3, 5), (2, 4, 6)]
[(1, 2), (3, 4), (5, 6)]
6.filter与map:
filter函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表对象,需要手动list将其转换。
该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
1 def is_odd(n): 2 return n % 2 == 1 3 4 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 5 print(newlist) 6 print(list(newlist))
<filter object at 0x000000000259D390>
[1, 3, 5, 7, 9]
map和filter类似,不同的是他不会帮你过滤,只是依次将序列作用于函数上,注意比较上述代码
1 def is_odd(n): 2 return n % 2 == 1 3 4 newlist = map(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 5 print(newlist) 6 print(list(newlist))
<map object at 0x000000000260D390>
[True, False, True, False, True, False, True, False, True, False]
7.eval:
eval函数就是实现list、dict、tuple与str之间的转化
str函数把list,dict,tuple转为为字符串
1 # 字符串转换成列表 2 a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" 3 print(type(a)) 4 b = eval(a) 5 print(b) 6 # 字符串转换成字典 7 a = "{1: 'a', 2: 'b'}" 8 print(type(a)) 9 b = eval(a) 10 print(type(b)) 11 print(b) 12 # 字符串转换成元组 13 a = "([1,2], [3,4], [5,6], [7,8], (9,0))" 14 print(type(a)) 15 b=eval(a) 16 print(type(b)) 17 print(b)
<class 'str'> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] <class 'str'> <class 'dict'> {1: 'a', 2: 'b'} <class 'str'> <class 'tuple'> ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
8.exec:执行一段“”“”直接的python代码(前提是这段代码是正确的)
1 s2 =''' 2 import os 3 print(os.getcwd()) 4 for i in range(5): 5 print(i) 6 ''' 7 8 exec(s2) #执行一些简单的python代码,运算、定义变量
0
1
2
3
4
9.匿名函数:
即临时定义的一个只有函数体没有函数名的函数,改函数因为没有函数名则不可被调用,一次性有效:
语法 lambda 参数列表: 表达式 (冒号前面是入参,冒号后面是返回值)
res = map(lambda num:str(num).zfill(2),range(1,34)) print(list(res))
匿名函数中:num是入参,str(num).zfill(2)是函数体;由lambda生成函数后,用range(1,34)组合成一个map函数
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33']