def port(self,host,port): logging.load('[scan %s - %s ]'%(host,port)) isopen = False data = b'' s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.settimeout(self.timeout) try: s.connect((host,int(port))) isopen = True except Exception as e:# socket.timeout ConnectionRefusedError pass#print('1',e) s.close() return isopen,data try: data = s.recv(256) return isopen,data except Exception as e: pass#print('2',e) try: a = ('GET / HTTP/1.1\r\nHOST: %s\r\n\r\n'%host) s.sendall(a.encode()) data = s.recv(256) return isopen,data #print(data.decode('gbk','ignore')) except Exception as e: pass#print('3',e) finally: s.close() #关闭连接 return isopen,data
def F(f): if f.__name__ not in self.tasklist.keys(): logging.load('Reg-Task: ' + f.__name__ + '\n') self.tasklist[f.__name__] = {} self.tasklist[f.__name__]['handler'] = f self.tasklist[f.__name__]['name'] = f.__name__ self.tasklist[f.__name__]['doc'] = f.__doc__ self.tasklist[f.__name__]['type'] = tasktype if tasktype == '-1': pass elif tasktype == 'sched': pass elif tasktype == 'loopd': pass else: app.task(f) return f
def gettask(): M = models.ScanTask start = True while not self.Queue.full(): logging.load( 'Waiting-Task: [%s]' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) sw = (M.task_code == 'waiting') if start: sw |= (M.task_code == 'PENDING') for Q in (M.select().where(sw).order_by( M.task_level, M.createdate)).limit(self.count): self.Queue.put(Q) Q.task_code = 'PENDING' Q.save() start = False time.sleep(1)
def ping(self, iplist): sock = self.__icmpSocket sock.settimeout(self.timeout) packet = self.__icmpPacket recvFroms = set() #接收线程的来源IP地址容器 iplist = {ip for ip in iplist if self.isUnIP(ip)} sendThr = SendPingThr(iplist, packet, sock, self.timeout) sendThr.start() while True: logging.load('recv %s'%time.time()) try: recvFroms.add(sock.recvfrom(255)[1][0]) except Exception as e: pass#sleep(0.001)#print('ping',e) finally: if not sendThr.isAlive(): break return recvFroms & iplist
def run1(self): pool = ThreadPool(self.settings['threads']) self.FLAG = self.settings['timeout'] try: self.addreq(self.basereq) self.parse(self.basereq.response()) except: self.ISSTART = False return #5分钟后还没有任务加进来就当爬完了 while self.ISSTART and self.FLAG > 0: logging.load('Reload ... Wait for %s' % self.FLAG) try: req = self.ReqQueue.get(block=False) pool.spawn(self.request, req) except queue.Empty: time.sleep(1) self.FLAG -= 1 self.ISSTART = False pool.join()
def run1(self): pool = ThreadPool(self.settings['threads']) self.FLAG = self.settings['timeout'] try: self.request( BaseRequest(self.url, headers=self.settings['headers'])) except Exception as e: print(e) self.ISSTART = False return #5分钟后还没有任务加进来就当爬完了 while self.ISSTART and self.FLAG > 0: logging.load('Reload ... Wait for %s' % self.FLAG) try: req = self.ReqQueue.get(block=False) pool.spawn(self.request, req) except queue.Empty: time.sleep(1) self.FLAG -= 1 self.ISSTART = False pool.join()