Python多线程ping

Python多线程ping,第1张

Python多线程ping(ping3模块)
  1. 代码
    from ping3 import ping
    from concurrent.futures import ThreadPoolExecutor
    
    ######################################## 自定义函数_批量Ping ########################################
    def pings(ips):
        # ips为可迭代对象,每个元素为一个IP地址或域名
        # 返回值为一个字典,key保存ip,value保存是否能ping通
        ips_status = dict()
        # 多线程执行ping函数
        with ThreadPoolExecutor(max_workers=500) as pool:
            results = pool.map(ping,ips)
        for index,result in enumerate(results):
            ip = ips[index]
            if type(result) == float:
                ips_status[ip] = True
            else:
                ips_status[ip] = False
        return ips_status
    
    ips = ["192.168.85.1","192.168.85.200","192.168.85.10","192.168.85.16","192.168.85.254","192.168.85.19"]
    ips_status = pings(ips)
    print(ips_status)
    

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/795022.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存