一、Python内置对象
Numbers数字123 不可变 元组()
string字符串''"" 不可变
Lists列表[] 可变 列表list 字典dict
dictionaries 字典{} 可变
tuples 元组() 不可变
sets 集合set(),() 可变
files 文件open('1.txt')
bytearray 可变
Frozenset 不可变
false true
0b二进制 0o八进制 0x16进制
小数 Decimal('1.0') 分数 Fraction(1,3)
bin(二进制转化)
表达式操作符:
+,-,,*,/,>>位移 **乘方 ,
&位操作 1or0 》》1
and逻辑操作真假,1and0 》》0 99and100 》100
etc.
999 if () else 777 >777
!=不等于 ==等于
内建数学函数:()
pow 乘方 abs 绝对值 round 四舍五入
int 求整
hex16进制 oct八进制 bin(0b二进制)
etc.
pow(2,4) abs()
Utility modules工具模块
random(随机数) math(数学工具模块) , etc.求余 %
import math
import random
random.random() # 0-1随机浮点数
random.randint(1023,65535) #随机端口号
random.choice(['',''])
#1随机IP地址::类型不同不能组合;;;
str(random.randint(1,255))+ '.' +
str(random.randint(1,255))+ '.' +
str(random.randint(1,255))+ '.' +
str(random.randint(1,255))+ '.' +
二、字符串 不能改变 可以赋值新变量
\\ 不被转义 \\n \\t pwd
字符串 序列 不变 大小不变
\n换行;;;‘‘‘’’’
2print-3print()区别 ()
windows \r\n 换行 linux 换行\n
\tab \t 制表符 \\tab
r 前加 r 表示 原始 路径 无转移
b 二进制文件 打开前加 b'
\' \"
linux 打开文件open('1.txt','rb').read()
windows格式文件二进制打开文件\r\n
转换工具: dos2unix (W-L)
s = 'a\nb\tc\"'
结果:a b c"
s = r'a\nb\tc\"' rrrrrrrrrr 原始
print('-'*80) 高级 多态
原则不能修改
在不在 in ( 'k' in s ) true false
0 ; -1判断文件类型:s[-3:] 后边缘不包含
[:] 全部 复制
+ ##前提相同类型 ;
字符串替换 s.replace()
import re
new = s.replace('qytang','cisco')
查找位置: s.find()
大小写: s.upper()
小写 s.lower()
剥除:.strip() ;;;;;;;;;;;;;;;;;;;;;
s = '\nwelcome to qytang\n\r\t'
剩余字符串: s.strip()
去除左边 s.lstrip() 去除右边 s.rstrip()
产生列表: a = list(s)
list 列表 可以修改;;;;;;;;
把列表拼接成字符串: '!'.join(a)
' '.join(a)
字符串格式化: 1个表达式% ,则 不需要 ()
'%d整数 %s字符串 %f实数' % (1,r,2.0)
2方 法
%s字符串 %d 整数 %f 浮点;
''格式化转化后 %()多个对象放到元组()里,一个不需要()
%-10d宽度左对齐%10d右对齐%010d前面补零;;10宽度
x = 1234 -左对齐;6宽度;
'%-6.2f|%05.2f%+06.1f' % (x,x,x)
#-左对齐;6宽度;.2小数后两位;
#05.2f 05右对齐;0用0填充,宽度5,.2两位小数
#+06.1 +就是+ 0用0填充,宽度6位,1位小数;;
'%-5s\n' % ('sdfasd')
print('%-10s 1\n%-10s 2\n%-10s 3\n' % ('pyhton','cisco','qytang'))
方法:::: .format()
'{0},{1}{2}'.format('','','')
#'{w:>右对齐宽度},{e:<左对齐},{r}'.format(w=3,e=2,r=4)
:06.2f%
{:>10,:^10,:<10}.format(x,x,x)
x=3.141592
'{0:06.2f},{1:06.2f}'.format(x,x)
import sys
sys.platform #看系统版本
'{0.platform:>10} = {1[kind]:<10}'.format(sys,{'kind':'laptop'})
#' win32 = laptop'
****正则表达式 ::********;;;;;;;;;;;;;;;;;;;;;;;;;;;;
http://www.regexlab.com/zh/regref.htm
https://zh.wikipedia.org/wiki/正则表达式
\^ \. \$ 匹配符号本身
\t tab \r 回车 \n 换行
\\ 转义符 \r\n 回车换行
import re
re.match('cmd.exe','cmdaexe')
# (正则表达式,匹配的模块、字符串 )
re.match('cmd\.exe','cmdaexe')
*************转义符 \ ;;;;;;;;;;;;;;;;
# 多种字符:
. 表示匹配除\n以外的任意一个字符;;
\s 包括空格、制表符、换页符等空白字符任意一个;
\w 任意一个字母或数字及下划线;
\d 任意一个数字0-9中 ;
import re
re.match('cmd\.exe','cmdaexe')*************
.需要转义 就是一个点;;;;;;;;;;;;;;;;;;;
\s 空白 ;;;;;;;+至少出现1次
#匹配IP::::::::::::
import re
i = '123.45.65.89'
re.match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.',i)
.任何
#{1,3}表示出现1次或者3次
{m,n} 表达式至少重复m次,最多重复n次;;;
*表达式不出现或出现任意次;
+匹配至少出现1次;
?匹配出现0次或1次;
.* 任意字符任意次 ;;;;;;;;;;;;
[] 位
#匹配MAC地址 ::6段
import re
s = '00-50-56-C0-00-13'
re.match('[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}',s)
0-9 a-f A-F [位]
[ ^a-f] #不要什么a-f 除什么之外 ;不需要
^ 开始 与字符串开始的地方匹配,不匹配任何字符;;(以什么开头))
re.match('^abc123','abc123456')
$ 结尾 与字符串结束的地方匹配,不匹配任何字符;;
re.match('abc123$','221abc123')
\b 匹配一个单词边界,单词和空格之间的位置,不匹配任何字符;;;
(a)? a出现0次或者1次;;;+ 1次;;;;;;;;;
abc* 表示*左边c出现任意次数;;;;;;;;;;
| 或 匹配左边或者右面;;;;;;;;;;
re.match('[Rr]|oot', 'root')
##############
import re
re.split('--','aa--bb--cc') #用---分割,产生一个清单;
#['aa','bb','cc']
re.sub('--','..','aa--bb==c') #替换
#'aa..bb..cc'
re.split('--|==','aa--bb==cc')
# ['aa','bb','cc'] # 清单[]
#################################
s='spam/ham/eggs'
import er
s.split('/')
re.match('(.*)/(.*)/(.*)', s).groups()
re.match('(\W*)/(\W*)/(\W*)', s).groups()
('spam','ham','eggs') #元组()
re.match('(\w{4,})/(\w{3,3})/(\w{4,})', s).groups()
\s+ 肯定 \s*可能有空白
l='hello qytang world!'
re.match('hello\s+([a-z]+)\s+([a-z]+)\s*!',l).groups()
find 查找 位置 ;;;;;;;
re.findall() 局部内容;
a = '<qytang>/<cisco>/<python>'
re.findall('<([a-z]+)>',a) ##清单
groupdict() 字典
re.search('(?P<a 1>\w*)/(?P<a2>\w*)',a).groupdict()
Core-ASA设备:::::::::::::
查看IP :: show interface ip brief
##################linux :which python3.7
#!/usr/bin/python3.7(which python3.7)
#-*- coding=utf-8 -*-
s = 'Port-channel1.189 192.168.189.254 YES CONFIG up'
import re
a = re.match('(\w.*\d)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+YES\s+CONFIG\s+(\w+)\s*',s).groups()
print(''+a[0])
print('%-10s : %s' %('',+a[0]))
#步骤:chmod +x xx.py可执行./xx.py dos2unix xx.py
import re
import os
b = '166 54a2.74f7.0326 DY Gil/0/11'
re.match('(\d{1,4})\s+([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4})\s+DY\s+(\w.+\d)\s*',b).groups()
s = os.popen('ifconfig enth0').read()
print(s)
########################
元组()不可变
#string字符串 序列 不可变 不可改
# list 清单 序列vvv列表 可变 任何嵌套 偏移索引
[] 可变 可重复 字典可变 无序
#有序 任意对象 引用 索引 切片[]
test = [1,2,3,4]
test[3] = 6
test可变
字典 dict = ['abc']
+ [] .append()
list(range(1,4))
len 长度
3 in list
##缩进 相同 同一块同一循环同条件 ;;;;;;;;;;;
for x in s:
print(x,end='')
shift+F9
#元组() :序列 不可变 固定长度 可异变 任何嵌套 索引
(10) 不是元组
(10,)是元组
s = tuple('dasdf')
元组 做 键 会话
元组 排序 :
t = (1,23,4,5,6,757,83,23)
p = list(t) #元组转清单
p.sort() #排序
p
t=tuple(p) #排序后为元 组;;清单转元组
字典的键 === 用元组tuple()
排序问题:
t=tuple(t)
元组不可变 用清单list()排序
t=(1,4,2,6,3,5)
tmp=list(t)
tmp.sort()
tmp T = tuple(tmp)
方法: sorted(t2)
t2= tuple(sorted(t))
index(2) #2第一次出现的位置
T.index(2,2)#从2好位置往后查2出现的位置
T.count(2)#2出现几次
r 文件:::::::::::::::::
d = open('','rb').read() #r 读w写 a追加写入 b二进制打开
+ seek 光标
../ 相对路径
d = open(r'/root/ect/ ','r').read()
文件迭代器 ;::::::::::::
d.write('')
d.close()
d.readline() 读取一行
for line in opern('','r'):
print(line,end='') # 不加\n 换行
pickle 原生存储 二进制 仅限 Python
import pickle
f= open('','wb')
pickle.dump(D,'') #写入文件‘‘’’dump
l=open('','r')
d=l.load('') #读取写入数据;load
d.close()
网络::::::::::::::
数据包 .bin二进制文件存储
import struck
t=open('data.bin','wb')
data= struck.pack('>i4sh',7,b'spam',8)
#高位>低位网络字节序 低位<高位 组机字节序
#i整数4个字节 4s 4个str h2字节为8
t.write(data) #写入文件
t = open('data.bin','rb')
data = t.read() #读取二进制
values = struck.unpack('>i4sh',data) #还原二进制包
pickle简单包 ;;;
'''
#################################
1、打开某一个目录下的所有文件搜索文件中特定关键字文件;
并且把找到的文件清单,通过pickle技术保存到一个本地文件带日期。
os.walk() 遍历目录 函数的方法 .walk
import os
import re
from datetime import datetime
file = []
D = r'D:\SXB\1'
for dirpath,dirnames,filenames in os.walk(D):
'''
print(dirpath) #目录路径
print('-' * 80)
print(dirnames) #目录名
print('-' * 80)
print(filenames) #文件名
print('=' * 80)
'''
for filepath in filenames: #遍历文件名
file.append(os.path.join(dirpath,filepath)) #把路径与文件名链接到一起
print(file) #所有文件的绝地路径
find = []
for filename in file(): #循环‘D/1/d/文件和路径’
found = 0 #判断条件
file_open = open(filename,'rb') #二进制方式读取文件
for line in file_open: #遍历每行
if re.match(b'.*123.*',line): #匹配关键字
found = 1 #判断找到;
if found == 1: #如果找到关键文件
find.append(filename) #添加到文件列表中
#存文件
import pickle
file_name = open('123.pkl','wb')
pickle.dump(find,file_name)
find_name.close()
find_name = open('123.pkl','rb')
f = pickle.load(find_name)
print(f)
#日期 ::
now = datetime.now()
pickle = 'classfile_' + str(now.year) + '-' + str(now.month) + '_' + .pkl
from datetime import datetime
now = datetime.now()
print(dir(now))
2、是有struct技术构造TCP头部;;
from random import randint
import struct
source_port = randint(1,65535) #原端口
print(hex(source_port))
dst_port = 23
print(hex(23))
sn = randint(1,65535*65535) #序列号32位
print(hex(sn))
ack_sn = 0
windows = 4096 #窗口大小
print(hex(4096))
data = struck.pack('>HHIIBBH', source_port, dst_port,sn,ack_sn,80,2,4096)
#压缩网络包H 2个字节 I 4个字节
F = open('data.bin','wb')
F.write(data)
F.close()