关于python设置多线程后数据重复问题

本意是想获取一个ip段的存活主机的信息,但是使用多线程后,它会重复数据:
图片描述

获取信息的代码:

    try:
            ip = IP(self.ip)
        except Exception,e:
            print '%s %s:' % (colored('[*]','red'),self.ip) + 'ERROR'
            exit(-1)
        for Ip in ip:
            ip = str(Ip)
            self.qu.put(ip)
        while self.qu.qsize() > 0:            
            try:
                ip = self.qu.get()
                r = requests.get('http://'+str(ip),headers=header,timeout=Timeout,allow_redirects=False)
                r.encoding = 'utf-8' 
                status = r.status_code
                title = re.search(r'<title>(.*)</title>', r.text)
                if title:                    
                    title = title.group(1).strip()
                else:
                    title = 'None'
                try:
                    banner = r.headers['server']
                except:
                    pass
                screenLock.acquire()
                if status == 200 or status == 403 or status == 400:
                    print '%s Is scanning %s' % (colored('[+]','green'),ip)
                    print '%s status:%s' % (colored('[-]','blue'),status)
                    print '%s banner:%s' % (colored('[-]','blue'),banner)
                    print '%s title:%s' % (colored('[-]','blue'),title)
                    self.WriteOut('[%s] %s - %s - %s' % (status,ip,banner,title))
            except Exception,e:
                pass
            finally:
                screenLock.release()

设置多线程:

    for i in range(args.threadNum):
        t = threading.Thread(target=scan.run)
        t.start()

默认线程数是20

阅读 5k
1 个回答

你把 self.qu.put(ip) 操作放在了线程里,等于重复执行了 20 次

for Ip in ip:
    ip = str(Ip)
    self.qu.put(ip)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题