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