#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import getpass
import time
import socket
import re
import threading
from Queue import Queue
import platform
import types
from subprocess import Popen, PIPE
import struct
import sys
from db import intodb
__all__ = ['Thread', 'Threadpool', 'OK', 'FINISH', 'ERROR']
OK = 0x0
FINISH = 0x1
ERROR = 0x2
class Thread(threading.Thread):
def __init__(self, worker, task_queue, msg_queue, threadpool):
super(Thread, self).__init__()
self.worker = worker
self.task_queue = task_queue
self.threadpool = threadpool
self.msg_queue = msg_queue
def run(self):
count = 0
while True:
self.threadpool.event.wait()
if self.task_queue.empty():
self.threadpool.InActiveOne()
break
task = self.task_queue.get()
try:
ret = self.worker(task)
self.msg_queue.put((task, ret))
if (not ret) and (ret[0] == FINISH):
self.threadpool.clearQueue()
except Exception as e:
self.msg_queue.put((task, ERROR))
finally:
self.task_queue.task_done()
class Threadpool(object):
def __init__(self, worker, max_threads=10, thread=Thread,
queue=Queue, lock=threading.RLock()):
self.worker = worker
self.thread = thread
self.event = threading.Event()
self.lock = lock
self.task_queue = queue()
self.msg_queue = queue()
self.max_threads = max_threads
self.active_threads = 0
self.start()
def add(self, tasks):
for task in tasks:
self.task_queue.put(task)
len_tasks = self.task_queue.qsize()
self.lock.acquire()
create_tasks = self.max_threads - self.active_threads
if len_tasks < create_tasks:
create_tasks = len_tasks
for i in xrange(create_tasks):
self.ActiveOne()
self.lock.release()
def ActiveOne(self):
self.lock.acquire()
t = self.thread(self.worker, self.task_queue, self.msg_queue, self)
t.setDaemon(True)
t.start()
self.active_threads += 1
self.lock.release()
def InActiveOne(self):
self.lock.acquire()
self.active_threads -= 1
self.lock.release()
def status(self):
return self.task_queue.qsize(), self.active_threads
def join(self):
self.task_queue.join()
def printmsg(self):
pass
def clearQueue(self):
self.stop()
while True:
if self.task_queue.empty():
break
self.task_queue.get()
self.task_queue.task_done()
self.start()
def start(self):
self.event.set()
def stop(self):
self.event.clear()
class InScaner:
def __init__(self, domain):
self.NUM = 200
self._re_IP = r'\d+\.\d+\.\d+\.\d+'
self._re_startwithIP = r'^\d+\.\d+\.\d+\.\d+.*'
self._re_network = r'^\d+\.\d+\.\d+'
self.re_ip = re.compile(self._re_IP)
self.re_startwithIP = re.compile(self._re_startwithIP)
self.re_network = re.compile(self._re_network)
self.host_ip = socket.gethostbyname(socket.gethostname())
self.domain = domain
self.path = os.getcwd()
self.host_hostname = '' # os.popen('hostnam