zoukankan      html  css  js  c++  java
  • ping ip

    def ip_and_time():
        """
        get ip to ping from ip.txt
        then return two list , each ip that ping successfully and the delay time of them
        :return
        """
    
        time_list, ip_list = [], []
        ip_txt = open('ip.txt', 'w')
        ip_success, ip_fail = 0, 0
        log_write = open('log.txt', 'a')
        for ip in get_ip():
            # 每个ip ping2次,等待时间为1s
            cmd_return = os.popen('ping -n 2 -w 1 %s' % ip).read()
            if 'ms' in cmd_return:
                ip_list.append(ip.strip())
                delay = cmd_return[cmd_return.rfind('='):]
                delay_time = int(delay.replace('ms', '').replace('=', ''))
                if delay_time <= 50:
                    delay_time = 150
                    time_list.append(delay_time)
                    ip_success += 1
                elif 50 < int(delay_time) <= 100:
                    delay_time = 200
                    time_list.append(delay_time)
                    ip_success += 1
                elif 100 < int(delay_time) <= 150:
                    delay_time = 250
                    time_list.append(delay_time)
                    ip_success += 1
                elif 150 < int(delay_time) <= 200:
                    delay_time = 300
                    time_list.append(delay_time)
                    ip_success += 1
                elif 200 < int(delay_time) <= 250:
                    delay_time = 350
                    time_list.append(delay_time)
                    ip_success += 1
                elif 250 < int(delay_time) <= 300:
                    delay_time = 400
                    time_list.append(delay_time)
                    ip_success += 1
                elif 300 < int(delay_time) <= 350:
                    delay_time = 450
                    time_list.append(delay_time)
                    ip_success += 1
                elif 350 < int(delay_time) <= 400:
                    delay_time = 500
                    time_list.append(delay_time)
                    ip_success += 1
                else:
                    delay_time = 550
                    time_list.append(delay_time)
                    ip_success += 1
            elif len(cmd_return) <= 160:
                # 当ping2次时窗口输出字符串长度小于160记为超时,否则记为传输过期
                ip_list.append(ip.strip())
                time_list.append(400)
                ip_fail += 1
            else:
                ip_list.append(ip.strip())
                time_list.append(400)
                ip_fail += 1
    
        log_write.close()
        ip_txt.close()
        return ip_list, time_list
  • 相关阅读:
    [导入]自由的生活
    [导入]宁静
    [导入]书店
    [导入]娶老婆的15条金科玉律
    [导入]静静的日子
    [导入]生活无聊的日子
    [导入]新的任务
    [导入]问题:我是一个内向的男生。请问怎么追求自己喜欢的女孩
    [导入]奋斗
    java 多种方式文件读取
  • 原文地址:https://www.cnblogs.com/vickey-wu/p/8274578.html
Copyright © 2011-2022 走看看