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

查看:1060
本文介绍了关于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

解决方案

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

for Ip in ip:
    ip = str(Ip)
    self.qu.put(ip)

这篇关于关于python设置多线程后数据重复问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆