Python练习1
一、linux,基于文件大小,创建时间,修改时间,文件内容,文件名称等进行查找汇总和输出
2019-01-04
只操作文本文件
#!/usr/bin/env python # -*- coding: utf-8 -* # Created by YangYongming at 2018/12/10 17:16 # FileName: main.py import os import time import zipfile fileslist = [] # 文件列表 if not os.path.exists("temporary"): os.popen("mkdir temporary &> /dev/null") def zip_dir(dirname, zipfilename): filelist = [] if os.path.isfile(dirname): filelist.append(dirname) else: for root, dirs, files in os.walk(dirname): for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED) for tar in filelist: arcname = tar[len(dirname):] zf.write(tar, arcname) zf.close() def Get_Cur_time(): """ # 获取当前日期和时间 :return: """ return time.strftime("%Y-%m-%d-%H-%M-%S") def Judge_filetype(filename): """ # 判断一个文件是都是文本文件 :param filename: 文件名称 :return: """ res = os.popen("file %s" % filename) if "text" in res.read(): return True else: return False def Get_FilesList(path): """ # 获取所有文件列表 :param path: 文件夹目录 :return: 所有文件绝对路径组成的列表 """ global fileslist for root, dir, files in os.walk(path): for i in files: file = os.path.join(root,i) # 拿到每个文件的绝对路径 if os.path.isfile(file): # 判断一下是否是文件 if Judge_filetype(file): # 判断是否是文本文件 if os.access(file, os.R_OK): # 判断文件是都可读 fileslist.append(file) # 添加到文件列表中 return fileslist def Content_Keyword(keyword): """ # 基于文件内容关键字查询 :param keyword: 关键字 :return: """ # 创建存储目录 global number dir = "%s[Content:%s]" % (curtime, keyword) os.popen("cd temporary && mkdir %s" % dir) for i in fileslist: # 基于linux命令拿到匹配到文本的行号 result_obj_line = os.popen("grep -n %s %s | cut -d: -f1" % (keyword, i)) # 读取行号字符串 result_line_seq = result_obj_line.read() # 把行号转换成序列,并且将序列内容连接到一起 result_line = ''.join(result_line_seq.split()) # 判断是否都为数字,如果不是全数字,说明匹配到的文本可能是二进制文件或者其他格式文件 if result_line.isdigit(): # 输出文件名称和具体的行号,使用split()把一个文件中匹配到的所有行放在一个数组中,防止字符串错乱显示 print("\033[0;32;40m%s Line:%s\033[0m" % (i, result_line_seq.split())) number += 1 # 匹配到的文件-p拷贝到指定文件夹 os.popen("cd temporary && cp -p %s %s" % (i, dir)) time.sleep(0.03) return dir, number def Name_Type_Keyword(keyword): """ # 基于文件名称查找 :param keyword: 关键字 :return: """ # 创建存储目录 global number dir = "%s[NameType:%s]" % (curtime, keyword) os.popen("cd temporary && mkdir %s" % dir) for i in fileslist: # 拿到文件的基名 basename = os.path.basename(i) # 在基名中查找keyword result_basename_obj = os.popen("echo %s | grep %s" % (basename, keyword)) # 读取查找返回值 result_basename = result_basename_obj.read() # 判断是否有匹配到的内容 if result_basename.strip(): # 输出文件名 print("\033[0;32;40m%s\033[0m" % i) # copy到指定的目录 os.popen("cd temporary && cp -p %s %s" % (i, dir)) number += 1 time.sleep(0.03) return dir, number def Time_Keyword(start_time, end_time, timetype): """ # 按照文件创建时间查询 :param start_time: 起始时间:20181230103050 :param end_time: 截止时间 :timetype: ctime文件创建时间,mtime:文件修改时间 :return: """ global number # 创建存储目录 dir = "%s[%s:%s-%s]" % (curtime, timetype, start_time.strip(), end_time.strip()) os.popen("cd temporary && mkdir %s" % dir) # 拿到起始时间的时间数组 start_timeArray = time.strptime(start_time, "%Y%m%d%H%M%S") end_timeArray = time.strptime(end_time, "%Y%m%d%H%M%S") # 拿到截止时间的时间戳 strt_timeStamp = int(time.mktime(start_timeArray)) end_timeStamp = int(time.mktime(end_timeArray)) for i in fileslist: # 拿到文件时间戳 if timetype == "ctime": time_Stamp = os.path.getctime(i) elif timetype == "mtime": time_Stamp = os.path.getmtime(i) # 文件创建时间的时间戳在起止时间的时间戳内则匹配 if strt_timeStamp <= int(time_Stamp) <= end_timeStamp: # 拿到文件创建时间数组 ctimeArray = time.localtime(time_Stamp) # 按照指定的时间格式 拿到文件创建时间 otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", ctimeArray) # 输出匹配的文件名和创建时间 print("\033[0;32;40m%s Time:%s\033[0m" % (i, otherStyleTime)) # copy到指定的目录 os.popen("cd temporary && cp -p %s %s" % (i, dir)) number += 1 time.sleep(0.03) return dir, number def Size_Keyword(ge_size, le_size): """ # 按照文件大小查找 :param ge_size: 文件大小范围最小值[KB] :param le_size: 文件大小范围最大值[KB] :return: """ # 创建存储目录 global number dir = "%s[Size:%sKB-%sKB]" % (curtime, ge_size, le_size) os.popen("cd temporary && mkdir %s" % dir) for i in fileslist: size_B = os.path.getsize(i) size_KB = size_B / float(1024) if float(ge_size) <= size_KB <= float(le_size): # 输出文件名和文件大小 print("\033[0;32;40m%s Size:%.2f KB\033[0m" % (i, size_KB)) # copy到指定目录 os.popen("cd temporary && cp -p %s %s" % (i, dir)) number += 1 time.sleep(0.03) return dir, number if __name__ == '__main__': number = 0 curtime = Get_Cur_time() os.popen("clear") print("\033[1;31;40m\nNote: Only text files can be manipulated\n\033[0m") path = input("\033[0;36;40mplease input absolute path:\033[0m") if not os.path.exists(path): print("\033[0;36;40m%s:Path does not exist!\033[0m" % path) exit(10) res_fileslist = Get_FilesList(os.path.abspath(path)) print("\033[0;36;40mNumber of documents found: %s" % len(fileslist)) msg = "1.Search based on file content\n" \ "2.Search based on file name or type\n" \ "3.Search based on file creation time\n" \ "4.Search based on file modification time\n" \ "5.Search based on file size\n" num = input("\033[0;36;40m%splease choose:\033[0m" % msg) if num == "1": keyword = input("\033[0;36;40mPlease enter keyword:\033[0m") back = Content_Keyword(keyword.strip()) zip_dir("temporary/%s" % back[0], "temporary/%s.zip" % back[0]) elif num == "2": keyword = input("\033[0;36;40mPlease enter name keyword or type:\033[0m") back = Name_Type_Keyword(keyword.strip()) zip_dir("temporary/%s" % back[0], "temporary/%s.zip" % back[0]) elif num == "3": start_time = input("\033[0;36;40mPlease enter a start time:\033[0m") end_time = input("\033[0;36;40mPlease enter a end time:\033[0m") if bool(end_time) == False: end_time = time.strftime("%Y%m%d%H%M%S", time.localtime()) if start_time.isdigit() and end_time.isdigit() and len(start_time) == 14 and len(end_time) == 14: back = Time_Keyword(start_time, end_time, timetype="ctime") zip_dir("temporary/%s" % back[0], "temporary/%s.zip" % back[0]) else: print("\033[0;31;40mInput error!\033[0m") exit(3) elif num == "4": start_time = input("\033[0;36;40mPlease enter a start time:\033[0m") end_time = input("\033[0;36;40mPlease enter a end time:\033[0m") if bool(end_time) == False: end_time = time.strftime("%Y%m%d%H%M%S", time.localtime()) if start_time.isdigit() and end_time.isdigit() and len(start_time) == 14 and len(end_time) == 14: back = Time_Keyword(start_time, end_time, timetype="mtime") zip_dir("temporary/%s" % back[0], "temporary/%s.zip" % back[0]) else: print("\033[0;31;40mInput error!\033[0m") exit(4) elif num == "5": ge_size = input("\033[0;36;40mPlease enter a minimum file size[KB]:\033[0m") le_size = input("\033[0;36;40mPlease enter a maximum file size[KB]:\033[0m") if ge_size.isdigit() and le_size.isdigit(): back = Size_Keyword(ge_size, le_size) zip_dir("temporary/%s" % back[0], "temporary/%s.zip" % back[0]) else: print("\033[0;31;40mInput error!\033[0m") exit(5) else: print("\033[0;31;40mInput error!\033[0m") exit(1) try: print("\033[0;36;40mTotal number of documents: %s\033[0m" % back[1]) except: pass
运行截图:
二、windows批量pingIP地址/地址段,
sfle:每行一个IP或者网段
upip:存放up的ip
downip: 存放down的ip
#!/usr/bin/env python # -*- coding: utf-8 -* # Created by YangYongming at 2018/11/25 12:26 # FileName: ping.py import os import IPy import queue import threading import time class MyThread(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): # 定义每个线程要运行的函数 global aliveip global downip while True: host = self.queue.get(timeout=2) # 从队列中取Ip host = str(host) res = os.popen("ping -n 3 %s" % host) os.popen("exit\n") if "ms" not in res.read(): print(">>> %s is Down" % host) downip.append(host) else: print(">>> %s is UP" % host) aliveip.append(host) self.queue.task_done() if self.queue.empty(): # 当队列为空时,终止该线程 break if __name__ == '__main__': start = time.time() # 程序开始时间 print("\n Yongming working for you......\n") print("\n start......\n") with open("upip", "w", encoding="utf-8") as tmp1, open("downip", "w", encoding="utf-8") as tmp2: pass # 清空文件 threadlist = [] # 线程列表 queue = queue.Queue() # 队列 aliveip = [] # UP IP列表 downip = [] # DOWN IP列表 num = 0 # 线程数量 with open("sfile", "r", encoding="utf-8") as f1: for line in f1.readlines(): # 读取IP文件 ip = IPy.IP(line) # 获取每个网段的IP地址列表 for x in ip: num += 1 # 计算有多少个IP地址,决定开多少线程 queue.put(x) # 向队列里面放IP if num >= 100: num = 100 # 设置线程数量不超过100 for i in range(num): t = MyThread(queue) # 建立线程,传入队列 t.setDaemon(False) # 主线程执行完成,等待所有的前台线程执行完毕,默认[等待]False t.start() # 线程准备就绪,等待CPU调度 queue.join() # 队列为空在进行以下操作 with open("downip", "a", encoding="utf-8") as f1: for i in downip: f1.write(i + '\n') # DOWN的IP写入文件 with open("upip", "a", encoding="utf-8") as f2: for i in aliveip: f2.write(i + '\n') # UP的IP写入文件 end = time.time() # 结束时间 elapsed = end - start # 计算总耗时 print("\nUP:%s" % len(aliveip)) # 输出UP的IP数量 print("DOWN:%s" % len(downip)) # 输出DOWN的IP数量 print("\nTime taken: %d seconds\n" % elapsed) # 输出总耗时 input("\nEnter to Quit:") time.sleep(30) # 等待10s退出
运行截图:
三,比较AB两个IP列表的不同
Afile:存储AIP列表
Bfile:存储BIP列表
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -* 3 # Created by YangYongming at 2018/12/04 14:53 4 # FileName: ipCompar.py 5 6 7 def Check_Ip(filename): 8 with open(filename, "r", encoding="utf-8") as file1: 9 for i in file1.readlines(): 10 try: 11 IP(i.strip()) 12 except Exception: 13 if i.strip(): 14 print("[Err]IP address format: %s in %s" % (i.strip(), filename)) 15 else: 16 print("[Err]There are blank lines: in %s" % filename) 17 exit(100) 18 19 20 def Contrast_Ip(Afile, Bfile): 21 excle = xlwt.Workbook(encoding="utf-8") 22 sheet = excle.add_sheet('sheet 1') 23 24 style0 = xlwt.XFStyle() 25 al = xlwt.Alignment() 26 al.horz = 0x02 27 al.vert = 0x01 28 style0.alignment = al 29 pattern = xlwt.Pattern() 30 pattern.pattern = xlwt.Pattern.SOLID_PATTERN 31 pattern.pattern_fore_colour = xlwt.Style.colour_map['gray40'] 32 style0.pattern = pattern 33 34 style1 = xlwt.XFStyle() 35 al = xlwt.Alignment() 36 al.horz = 0x02 37 al.vert = 0x01 38 style1.alignment = al 39 40 for i in range(0, 3): 41 col = sheet.col(i) 42 col.width = 256 * 20 43 44 sheet.write(0, 0, 'Only_IN_%s' % Afile, style0) 45 sheet.write(0, 1, 'Only_IN_%s' % Bfile, style0) 46 sheet.write(0, 2, 'Both', style0) 47 48 with open(Afile, "r+") as file1: 49 A_ip = file1.readlines() 50 for i in range(0, len(A_ip)): 51 A_ip[i] = A_ip[i].strip() 52 A_IPSET = set(A_ip) 53 54 with open(Bfile, "r+") as file2: 55 B_ip = file2.readlines() 56 for i in range(0, len(B_ip)): 57 B_ip[i] = B_ip[i].strip() 58 B_IPSET = set(B_ip) 59 60 for i, k in enumerate(A_IPSET.difference(B_IPSET), 1): 61 sheet.write(i, 0, str(k), style1) 62 63 for i, k in enumerate(B_IPSET.difference(A_IPSET), 1): 64 sheet.write(i, 1, str(k), style1) 65 66 for i, k in enumerate(B_IPSET.intersection(A_IPSET), 1): 67 sheet.write(i, 2, str(k), style1) 68 69 excle.save("Ming[%s].xls" % Time) 70 71 return True 72 73 74 if __name__ == '__main__': 75 import xlwt 76 import time 77 from IPy import IP 78 79 Time = time.strftime("%H-%M-%S") 80 print("\nYongming working for you......\n") 81 time.sleep(3) 82 83 Check_Ip(filename="Afile") 84 Check_Ip(filename="Bfile") 85 86 if Contrast_Ip("Afile", "Bfile"): 87 print("Success, please check Ming[%s.xls]" % Time) 88 time.sleep(0.5) 89 else: 90 print("[Err] in Contrast_Ip()") 91 92 input("\nEnter to Quit:") 93 94 print("Quit after 3 seconds......") 95 96 time.sleep(3)
运行结果:会在当前目录生成一个excle文件,如下:
四:我的方法
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -* 3 # Created by YangYongming at 2018/11/19 17:46 4 # FileName: ming.py 5 import os 6 import sys 7 import zipfile 8 import threading 9 import queue 10 import IPy 11 12 13 def Function_Validation(origin_func): 14 """ 15 对类的方法进行验证 16 :param origin_func:源函数名称 17 :return:源函数的返回值 18 """ 19 20 def wrapper(self, *args, **kwargs): 21 """ 22 :param self: 可以直接调用类中的字段和方法 23 :param args: 参数 24 :param kwargs: 参数 25 :return: 26 """ 27 import hashlib 28 md5 = hashlib.md5() 29 # 对类识别码取MD5加密数据 30 md5.update(bytes(str(self.password), encoding="utf-8")) 31 if md5.hexdigest() != yangym.PASS: 32 print("Error yangym()类识别码错误") 33 exit(100) 34 try: 35 # 执行源函数 36 u = origin_func(self, *args, **kwargs) 37 # 返回原函数的返回值给外层函数 38 return u 39 except Exception: 40 # self.revive() #不用顾虑,直接调用原来的类的方法 41 return 'origin_func an Exception raised.' 42 43 return wrapper 44 45 46 class yangym(): 47 # 类入口验证码,实例化yangym时,必须携带PASS[password]字段,只有匹配才能执行类中的方法 48 49 PASS = "05a7319bcb20e06fa52a3dc3685f5f84" 50 51 def __init__(self, password): 52 # password 实例化类的验证码。 53 self.password = password 54 55 @Function_Validation 56 def Del_Blank_Line(self, filename): 57 58 """ 59 清除文件空白行空白行 60 :param filename: 文件名称 61 :return: True 成功;False 失败 62 """ 63 try: 64 with open(filename, "r+", encoding="utf-8") as infp: 65 lines = infp.readlines() # 把源文件内容读出来保存在lines中 66 with open(filename, "w+", encoding="utf-8") as outfp: 67 for li in lines: 68 if li.split(): # 判断是否为空白行 69 outfp.writelines(li) # 将操作后的源文件覆盖写回 70 except FileNotFoundError as e: 71 print("[Err] No such file or directory: %s " % filename) 72 return False 73 except IOError: 74 print("[Err] Permission deny: %s" % filename) 75 return False 76 except Exception as e: 77 print("[Err:Del_Blank_Line()] %s" % e) 78 return False 79 else: 80 return True 81 82 @Function_Validation 83 def Get_IpList(self, filename, repeat=False): 84 """ 85 在文件中获取合法的IP地址 86 :param filename: 文件名称 87 :param repeat: 去除重复行,True去除,False不去除 88 :return: 返回ip地址序列 89 """ 90 import re 91 try: 92 with open(filename, "r", encoding="utf-8") as file1: 93 line = file1.read() 94 line = re.sub(r"[!@#$%^&*-+_~?/|\\]", " ", line) 95 pattern = re.compile( 96 r"(?:\b(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\b\.){3}(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\b") 97 list_ip = pattern.findall(line) 98 if len(list_ip) == 0: 99 return list_ip 100 except FileNotFoundError: 101 print("[Err] No such file or directory: %s " % filename) 102 return False 103 except IOError: 104 print("[Err] Permission deny: %s" % filename) 105 return False 106 except Exception as e: 107 print("[Err:Get_IpList()] %s" % e) 108 return False 109 else: 110 if repeat == True: 111 return set(list_ip) 112 elif repeat == False: 113 return list_ip 114 115 @Function_Validation 116 def Get_CheckCode(self, n=6): 117 """ 118 获取有大小写字母、数字组成的随机n位验证码 119 :param num: 验证码位数,默认为6 120 :return: 返回n位验证码 121 """ 122 import random 123 check_code = str() 124 code = str() 125 for i in range(n): 126 ret = random.randint(0, 9) 127 if ret == 0 or ret == 1 or ret == 4 or ret == 7: 128 code = str(ret) 129 elif ret == 2 or ret == 5 or ret == 8: 130 code = chr(random.randint(65, 90)) 131 elif ret == 3 or ret == 6 or ret == 9: 132 code = chr(random.randint(97, 122)) 133 check_code = check_code + code 134 return check_code 135 136 @Function_Validation 137 def SendMail(self, SenderName, SenderMail, Password, ReceList, Theme, Text, CcList=[], Appendix=None, Port=25, ): 138 """ 139 发送邮件 140 :param SenderName: 发送者昵称 141 :param SenderMail: 发送者邮箱 142 :param Password: 邮箱密码/授权吗 143 :param ReceList: 收件人列表 144 :param Theme: 邮件主题 145 :param Text: 邮件正文 146 :param CcList: 抄送人列表[可选参数] 147 :param Appendix: 附件全路径,不可以使用中文[可选参数] 148 :param Port: 端口,[可选参数],默认"25" 149 :return True:发送成功,False:发送失败 150 """ 151 ret = True 152 try: 153 import smtplib, os 154 from email.mime.text import MIMEText 155 from email.utils import formataddr 156 from email.mime.multipart import MIMEMultipart 157 from email.mime.application import MIMEApplication 158 159 msg = MIMEMultipart('alternative') 160 161 msgText = MIMEText(Text, "Plain", "utf-8") 162 163 msg["from"] = formataddr([SenderName, SenderMail]) 164 msg["To"] = ",".join(ReceList) 165 msg["Cc"] = ",".join(CcList) 166 msg["Subject"] = Theme 167 168 if Appendix != None: 169 attachName = os.path.basename(Appendix) 170 part = MIMEApplication(open(Appendix, 'rb').read()) 171 part.add_header('Content-Disposition', 'attachment', filename=attachName) 172 msg.attach(part) 173 174 msg.attach(msgText) 175 176 if Port == 25: 177 server = smtplib.SMTP("smtp.163.com", 25) 178 elif Port == 465: 179 server = smtplib.SMTP_SSL("smtp.163.com", 465) 180 181 server.login(SenderMail, Password) 182 server.sendmail(SenderMail, ReceList, msg.as_string()) 183 server.quit() 184 except ModuleNotFoundError as e1: 185 print("[Err] %s" % e1) 186 ret = False 187 except Exception as e2: 188 print("[Err:SendMail()] %s" % e2) 189 return ret 190 191 @Function_Validation 192 def Copy_AToB_File(self, fileA, fileB): 193 """ 194 把文件A的内容追加到文件B中 195 :param fileA: 源文件 196 :param fileB: 目标文件 197 :return: 成功返回True, 失败返回False 198 """ 199 try: 200 with open(fileA, "r", encoding="utf-8") as A, open(fileB, "r+", encoding="utf-8") as B: 201 Afile = A.readlines() 202 B.seek(0, 2) 203 for i in Afile: 204 B.writelines(i.strip() + "\n") 205 except FileNotFoundError as e: 206 print("[Err] %s" % e) 207 return False 208 else: 209 return True 210 211 @Function_Validation 212 def IpCount(self, ipfile): 213 """ 214 统计每个IP地址出现的次数 215 :param ipfile: 包含纯IP地址的文件,每行一个,不要出现空白行 216 :return: 返回一个字典{IP地址:次数,} 217 """ 218 sip = [] 219 dic = {} 220 try: 221 with open(ipfile, "r+", encoding="utf-8") as f1: 222 for i in f1.readlines(): 223 sip.append(i.strip()) 224 except FileNotFoundError: 225 print("[Err] No such file or directory: %s " % ipfile) 226 return False 227 else: 228 setip = set(sip) 229 for i in setip: 230 num = sip.count(i) 231 dic[i] = num 232 return dic 233 234 @Function_Validation 235 def IpQuery(self, ip): 236 """ 237 :param ip: IP地址 238 :return: 执行成功返回归属地址信息,执行失败返回"False" 239 """ 240 import requests 241 from bs4 import BeautifulSoup 242 url = 'http://m.ip138.com/ip.asp?ip=' 243 kv = {'User-Agent': 'Mozilla/5.0'} 244 link = url + str(ip) 245 try: 246 r = requests.get(link, headers=kv) 247 r.raise_for_status() 248 r.encoding = r.apparent_encoding 249 soup = BeautifulSoup(r.text, 'lxml') 250 result = soup.select('p[class="result"]')[0].string 251 return result 252 except requests.HTTPError: 253 return False 254 255 @Function_Validation 256 def Get_FilesList(self, dirpath): 257 fileslist = list() 258 for root, dir, files in os.walk(dirpath): 259 for i in files: 260 file = os.path.join(root, i) # 拿到每个文件的绝对路径 261 if os.path.isfile(file): # 判断一下是否是文件 262 fileslist.append(file) # 添加到文件列表中 263 return fileslist 264 265 @Function_Validation 266 def zip_dir(self, dirname, zipfilename): 267 """ 268 :param dirname: 需要打包的文件或目录名称 269 :param zipfilename: 目标文件名 270 :return: 271 """ 272 filelist = list() 273 if os.path.isfile(dirname): 274 filelist.append(dirname) 275 else: 276 for root, dirs, files in os.walk(dirname): 277 for name in files: 278 filelist.append(os.path.join(root, name)) 279 280 zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED) 281 for tar in filelist: 282 arcname = tar[len(dirname):] 283 zf.write(tar, arcname) 284 zf.close()