def a(): coll = con() o = [] if 3 % 15 == 0: print 'ss' else: print '333'
def showRecordCount(): data = request.args page = int(data['page']) limit = int(data['limit']) coll = con() c = coll.find({"finish": 'Y'}).count() if c % 15 == 0: count = c / 15 else: count = c / 15 + 1 if int(page) <= count: s = [] for i in coll.find().limit(limit).skip((page - 1) * limit): s.append(i) return json.dumps({'data': s, 'total': count}) else: return json.dumps({'status': -1, 'message': 'page not exit'})
def write(self, msg): s = msg.split(',') coll = con() data = coll.find_one() # 如果数据库为空,那设第一条id为0,其他添加的数据在当前最大id上加1 if data is None: if s[0] == '1': # time = self.timeChange(int(s[1])) coll.insert_one({ "_id": 1, "setType": s[0], "time": s[1], "size": '', "taskName": s[2], "startTime": s[3], "ip": s[4], "pid": '', "pypid": '', "download": '', "endTime": '', "packSize": '', "status": 'Waiting', "finish": 'N' }) elif s[0] == '0': # size = self.sizeChange(float(s[1])) coll.insert_one({ "_id": 1, "setType": s[0], "time": '', "size": s[1], "taskName": s[2], "startTime": s[3], "ip": s[4], "pid": '', "pypid": '', "download": '', "endTime": '', "packSize": '', "status": 'Waiting', "finish": 'N' }) else: for i in coll.find().sort([("_id", -1)]).limit(1): last_id = i['_id'] if s[0] == '1': # time = self.timeChange(int(s[1])) coll.insert_one({ "_id": last_id + 1, "setType": s[0], "time": s[1], "size": '', "taskName": s[2], "startTime": s[3], "ip": s[4], "pid": '', "pypid": '', "download": '', "endTime": '', "packSize": '', "status": 'Waiting', "finish": 'N' }) elif s[0] == '0': # size = self.sizeChange(float(s[1])) coll.insert_one({ "_id": last_id + 1, "setType": s[0], "time": '', "size": s[1], "taskName": s[2], "startTime": s[3], "ip": s[4], "pid": '', "pypid": '', "download": '', "endTime": '', "packSize": '', "status": 'Waiting', "finish": 'N' }) for i in coll.find().sort([("_id", -1)]).limit(1): self.q.put(msg + ',' + str(i['_id']))
def info(self, msg3): self.q1.put(str(os.getpid())) coll = con() s = msg3.split(',') flag, size, name, run_time, ip, id = s[0], s[1], s[2], s[3], s[4], s[5] coll.update({"_id": int(s[5])}, {"$set": {"pypid": str(os.getpid())}}) # 最终文件压缩成的名字,其他文件都被删除 zip_name = u'/home/Web_demo/test/Web/pcapzip/' + name + '.zip' # 设定的时间如果小于当前时间则不符合要求 # source = int(time.mktime(time.strptime(run_time, "%Y-%m-%d %H:%M:%S"))) source = int(time.mktime(time.strptime(run_time, "%Y-%m-%d %H:%M"))) if source > int(time.time()): if flag == '1': sign1 = 1 sign2 = 1 # 在死循环中判断到了指定的时间进行抓包,同时拿到pid,通过kill pid来终止抓包 while (sign1): if source == int(time.time()): sign1 = 0 sub = subprocess.Popen( 'tcpdump -i ens33 -s 0 -Z root -w /home/Web_demo/test/Web/pcapzip/' + name + '.pcap host ' + ip, shell=True) PID = str(sub.pid) coll.update({"_id": int(s[5])}, {"$set": {"pid": PID}}) coll.update({"_id": int(s[5])}, {"$set": { "status": 'Catching' }}) # 到了时间就kill掉这个子进程 while (sign2): if source + int(size) == int(time.time()): subprocess.call('kill -9 ' + PID, shell=True) sign2 = 0 # 把结束时间加入 over_time = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(time.time())) coll.update({"_id": int(s[5])}, {"$set": { "endTime": over_time }}) oldname = u'/home/Web_demo/test/Web/pcapzip/' + name + '.pcap' # 判包的大小 # filesize = self.getPcapSize(oldname) filesize = os.path.getsize(oldname) coll.update({"_id": int(s[5])}, {"$set": { "packSize": str(filesize) }}) # 压缩 f = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) f.write(oldname, name + '.pcap') f.close() os.remove(oldname) coll.update({"_id": int(s[5])}, {"$set": { "download": name + '.zip' }}) coll.update({"_id": int(s[5])}, {"$set": {"finish": 'Y'}}) coll.update({"_id": int(s[5])}, {"$set": {"status": 'Finish'}}) elif flag == '0': sign3 = 1 sign4 = 1 while (sign3): if source == int(time.time()): sign3 = 0 sub2 = subprocess.Popen( 'tcpdump -i ens33 -s 0 -C ' + size + ' -Z root -w /home/Web_demo/test/Web/pcapzip/' + name + '.pcap host ' + ip, shell=True) PID = str(sub2.pid) coll.update({"_id": int(s[5])}, {"$set": {"pid": PID}}) coll.update({"_id": int(s[5])}, {"$set": { "status": 'Catching' }}) oldname = u'/home/Web_demo/test/Web/pcapzip/' + name + '.pcap' rm_name = u'/home/Web_demo/test/Web/pcapzip/' + name + '.pcap1' # 抓到所需大小的包会生成.pcap1/.pcap2...的文件,根据生成的.pcap1来判断是否完成,完成之后,将判断条件的.pcap1删除,将所需文件压缩,将原文件删除 while (sign4): if os.path.exists(rm_name) == True: subprocess.Popen('kill -9 ' + PID, shell=True) os.remove(rm_name) sign4 = 0 over_time = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(time.time())) coll.update({"_id": int(s[5])}, {"$set": { "endTime": over_time }}) # filesize = self.getPcapSize(oldname) filesize = os.path.getsize(oldname) coll.update({"_id": int(s[5])}, {"$set": { "packSize": filesize }}) f = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) f.write(oldname, name + '.pcap') f.close() os.remove(oldname) coll.update({"_id": int(s[5])}, {"$set": { "download": name + '.zip' }}) coll.update({"_id": int(s[5])}, {"$set": {"finish": 'Y'}}) coll.update({"_id": int(s[5])}, {"$set": {"status": 'Finish'}}) else: coll.update({"_id": int(s[5])}, {"$set": {"status": 'TimeError'}}) self.q1.get()
def createTask(): coll = con() if request.method == 'GET': data = request.args page = int(data['page']) limit = int(data['limit']) # number = 5 - int(pcap.q1.qsize()) # count 表示数据的分页数 c = coll.find().count() if c % 15 == 0: count = c / 15 else: count = c / 15 + 1 if int(page) <= count: s = [] for i in coll.find({ "finish": 'N' }).limit(limit).skip((page - 1) * limit): s.append(i) return json.dumps({ 'status': 1, 'message': 'ok', 'payload': { 'data': s, 'total': count } }) else: return json.dumps({'status': -1, 'message': 'page not exist'}) elif request.method == 'POST': if pcap.q1.qsize() < 5: a = json.loads(request.get_data()) s = [] name = a['taskName'] for i in coll.find(): s.append(i['taskName']) if name in s: return json.dumps({"status": -1, "message": "name is exist"}) else: setType = a['setType'] size = a['size'] startTime = a['startTime'] ip = a['ip'] taskName = a['taskName'] pcap.write(setType + ',' + size + ',' + taskName + ',' + startTime + ',' + ip) pcap.action() return json.dumps({"status": 1, "message": "ok"}) else: return json.dumps({"status": -1, "message": "task is full"}) elif request.method == 'PUT': try: id = json.loads(request.get_data())['_id'] for i in coll.find({"_id": int(id)}): if i['pid'] == '' and i['status'] == 'TimeError': pass elif i['status'] == 'Cancel': pass elif i['pid'] == '' and i['status'] == 'Waiting': # 首先判断是否有这个进程pid,不然的话会报错 s = subprocess.check_output('pgrep python', shell=True) if str(i['pypid']) in s.split('\n'): subprocess.call('kill -9 ' + str(i['pypid']), shell=True) pcap.q1.get() else: pass elif i['pid'] != '': s = subprocess.check_output('pgrep python', shell=True) if str(i['pid']) in s.split('\n'): subprocess.call('kill -9 ' + str(i['pypid']), shell=True) pcap.q1.get() else: pass coll.update({"_id": int(id)}, {"$set": {"status": 'Cancel'}}) return json.dumps({"status": 1, "message": "ok"}) except: return json.dumps({{"status": -1, "message": "Id is no exist"}}) elif request.method == 'DELETE': try: id = json.loads(request.get_data())['_id'] coll.remove({"_id": int(id)}) return json.dumps({"status": 1, "message": "ok"}) except: return json.dumps({{"status": -1, "message": "Id is no exist"}})