欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何用Python的Queue模块轻松遍历IP地址列表

最编程 2024-07-25 22:09:23
...
import threading
import queue
class mythread(threading.Thread):
def __init__(self,n):
super(mythread, self).__init__()
self.n=n
def run(self):
while not self.n.empty():
ip=self.n.get()
print(ip)

def main():
threads=[]
threads_count=5
n=queue.Queue()
for i in range(1,255):
n.put('192.168.1.'+str(i))
for i in range(threads_count):
t=mythread(n)
threads.append(t)
for i in range(threads_count):
threads[i].start()
for i in range(threads_count):
threads[i].join()


if __name__ == '__main__':
main()
》》》

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7

...

......

...

192.168.1.245
192.168.1.246
192.168.1.247
192.168.1.248
192.168.1.249
192.168.1.250
192.168.1.251
192.168.1.252
192.168.1.253
192.168.1.254