Python多线程ping某个网段的IP是否被占用

背景:在某些业务上或许要填充大批量的IP,但是怎么能够快速的知道这些IP是否被占用呢,例如:192.168.137.X这个网段的IP,或许向某些大企业的一些内网IP呢,有些IP都是好几个项目组公用的.

1、可以写一个串行方法,for循环进行一个一个的用ping命令去测试。如果超时则没有被占用,如果有返回数据,说明IP是通的,是被占用的。

1.1、定义一个某IP段的列表,1~255的所有IP都在

例如:ip_list = ['60.60.60.' + str(i) for i in range(1, 256)]

1.2、写一个方法ping测试函数

# -*- coding: utf-8 -*-
import os


ip_list = ['60.60.60.' + str(i) for i in range(1, 256)]

"""串行方法"""
def ping_test(ip):
    res = os.popen(f"ping {ip} -n 1 -w 1")
    print(res.read())

for ip in ip_list:
    ping_test(ip)

结论:这个方法也是可以的 ping测试时OK的,-n 1 返回结果只有1条,-w 1 超时时间是1S,快速知道是否被占用。但是如果没被IP执行的时间是1秒,大概也需要五分钟,体验是很差的。2、

2、使用并行的方法进行ping测试,因为python有GIL全局解释锁的原因,python多线程是很鸡肋的,因此加上一个队列来进行真正的并行。

2.1、定义工作线程的个数,实例化一个Queue对象。使用一个循环经IP都放到一个队列里面进行后边调用。

import threading
from queue import Queue

WORK_THREAD = 100  # 定义100个工作线程
IP_QUEUE = Queue()  # 实例化一个队列对象
for i in ip_list:
    IP_QUEUE.put(i)  # 循环将ip放到队列里,队列是全局可以使用的  

2.2、写一个测试函数进行ping测试,写一个线程的调用方法,进行对对一个函数调用,最后在执行ping_threading函数。打印结果

def ping_ip_queue():
    while not IP_QUEUE.empty():
        ip = IP_QUEUE.get()
        res = os.popen(f"ping {ip} -n 1 -w 1")

def ping_threading():
    threads = []
    for i in range(WORK_THREAD):
        thread = threading.Thread(target=ping_ip_queue)
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()

总结:最后使用打印,并行方法使用的时间大概是 func_name:ping_threading, time consuming:1.67 S,已经调高了近百倍的速率。

完整代码:

# -*- coding: utf-8 -*-
# @Time    : 2022/6/9 15:45
# @FileName: ping_test.py
# @Software: PyCharm

import time
import os
import threading
from queue import Queue
from functools import wraps
from memory_profiler import profile

ip_list = ['60.60.60.' + str(i) for i in range(1, 256)]

WORK_THREAD = 100
IP_QUEUE = Queue()
ip_list.append('www.baidu.com')
for i in ip_list:
    IP_QUEUE.put(i)

""" 写一个装饰器,进行函数消耗时间的统计"""

def timer(func):
    @wraps(func)
    def wrapper(*arg, **kwargs):
        start = time.time()
        func(*arg, **kwargs)
        end = time.time()
        print(f'func_name:{func.__name__}, time consuming:{round(end-start, 2)} S ')
        return func
    return wrapper


q1 = Queue()  # 这个是放没有被占用的IP
q2 = Queue()  # 这个是放被占用的IP


def ping_ip_queue():
    while not IP_QUEUE.empty():
        ip = IP_QUEUE.get()
        res = os.popen(f"ping {ip} -n 1 -w 1")
        if '请求超时' in res.read():
            q1.put(ip)
        else:
            q2.put(ip)

"""profile 是一个内存分析函数,最近法装饰"""

@timer
@profile
def ping_threading():
    threads = []
    for i in range(WORK_THREAD):
        thread = threading.Thread(target=ping_ip_queue)
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()


if __name__ == '__main__':
    ping_threading()
    ress = dict()
    while not q1.empty():
        ress[(q1.get())] = 1
    while not q2.empty():
        ress[(q2.get())] = 0
    print(ress)

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值