python多线程使用例子threading + Queue
来源:互联网 发布:大数据相关体系架构 编辑:程序博客网 时间:2024/05/18 02:32
多线程使用例子threading + Queue
#/usr/bin/env python#-*-coding:utf8-*-#__author__: pyphrb # focus web security#about nmap scanimport osimport Queueimport datetimeimport timefrom libnmap.process import NmapProcessfrom libnmap.parser import NmapParser, NmapParserExceptionimport sysimport reimport loggingimport threadingscanResult = []threads = []logging.basicConfig(level=logging.DEBUG,\ format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',\ datefmt='%a, %d %b %Y %H:%M:%S', filename= 'logs/info.log',filemode='a')class Mynmap(object): def __init__(self, scanIp): self.scanIp = scanIp def startNmap(self): self.nmapScan = NmapProcess(self.scanIp, options='-sV -T4 -A -Pn -p 22-65534') self.rc = self.nmapScan.run() if self.nmapScan.rc == 0: #print self.nmapScan.stdout return self.nmapScan.stdout else: print self.nmapScan.stderr logging.info('nmap scan error'+ self.scanIp) return False def startParse(self): #nmap xml parse func try: self.startNmapScan = self.startNmap() if self.startNmap is not False: self.parse = NmapParser.parse(self.startNmapScan) self.nmapScanreport = self.startReport() else: sys.exit(0) except NmapParserException as e: logging.info(e) sys.exit(0) def startReport(self): self.report = self.parse if self.report: for self.host in self.report.hosts: for self.serv in self.host.services: if len(self.serv.banner) and self.serv.state == 'open': scanResult.append((str(self.host.address) + ' ' + '+' + ' ' + 'NmapService: [' +str(self.serv.state) + ' ' + str(self.serv.protocol) + ' ' + "<" + str(self.serv.port) + ">" + ' ' + str(self.serv.service) + ' ' + str(self.serv.banner)) + ']') else: if self.serv.state == 'open': scanResult.append((str(self.host.address) + ' ' + '+' + ' ' + 'NmapService: [' + str(self.serv.state) + ' ' + str(self.serv.protocol) + ' ' + "<" + str(self.serv.port) + ">" + ' ' + str(self.serv.service)) + ']')class MyThread(threading.Thread): def __init__(self, inputi): self.inputi = inputi threading.Thread.__init__(self) def run(self): while True: if self.inputi.qsize() > 0: self.ip = self.inputi.get() self.myNmap = Mynmap(self.ip) self.myNmap.startParse() else: breakclass Mscan(object): @classmethod def startNmapScan(cls,list_ip): #threadingSum = threading.Semaphore(40) q = Queue.Queue(0) lists = list_ip for ip_list in lists: q.put(ip_list) for j in range(200): threads.append(MyThread(q)) for x in threads: x.start() for y in threads: y.join() return scanResult @classmethod def getHttp(cls,resultList): httpPortList = [] for line in resultList: if 'http' in line.strip(): if re.search(r"<(.*)>", line.strip()): if re.search(r"\d+\.\d+\.\d+\.\d+", line.strip()): httpPortList.append("http://" + re.search(r"\d+\.\d+\.\d+\.\d+", line.strip()).group(0) + ':' + re.search(r"<(.*)>", line.strip()).group(0).strip('>').lstrip("<") + '/') return httpPortList
0 0
- python多线程使用例子threading + Queue
- Python多线程:thread、threading、queue
- python threading和queue的例子
- python多线程模块thread,threading,Queue
- python-利用queue和threading实现多线程下载
- Learning Python(16)--多线程编程(threading,Queue模块)
- thread/threading/Queue/mutex——Python 多线程
- 多线程&多进程解析:Python、os、sys、Queue、multiprocessing、threading
- Python:使用threading模块实现多线程编程
- python中使用threading进行多线程编程
- python多线程thread/threading的使用
- python中使用threading进行多线程编程
- Python:使用threading模块实现多线程编程
- Python:使用threading模块实现多线程
- python 多线程threading模块的使用
- Python:使用threading模块实现多线程
- Python:使用threading模块实现多线程
- 多线程在python中的使用 threading
- 杭电2100Lovekey
- 随机森林代码
- Android Service 线程问题
- Druid连接池
- Android Studio中SVN的文件的颜色所代表的意思
- python多线程使用例子threading + Queue
- vi查找替换命令详解
- Android samples API Demos之UI篇1(ActionBarCompat-ShareActionProvider)
- gdb 如何制作(gdb target,gdb server)
- 【老罗笔记】哪来的天才——练习中的平凡与伟大
- kafka原理
- 收藏夹
- 安卓版本和API的Level对应的关系
- live555MediaServer代码研究Media Server