MCPcopy Create free account
hub / github.com/Jack-Cherish/python-spider / check_ip

Function check_ip

daili.py:66–95  ·  view source on GitHub ↗
(ip, lose_time, waste_time)

Source from the content-addressed store, hash-verified

64 2017-05-27
65"""
66def check_ip(ip, lose_time, waste_time):
67 #命令 -n 要发送的回显请求数 -w 等待每次回复的超时时间(毫秒)
68 cmd = "ping -n 3 -w 3 %s"
69 #执行命令
70 p = sp.Popen(cmd % ip, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
71 #获得返回结果并解码
72 out = p.stdout.read().decode("gbk")
73 #丢包数
74 lose_time = lose_time.findall(out)
75 #当匹配到丢失包信息失败,默认为三次请求全部丢包,丢包数lose赋值为3
76 if len(lose_time) == 0:
77 lose = 3
78 else:
79 lose = int(lose_time[0])
80 #如果丢包数目大于2个,则认为连接超时,返回平均耗时1000ms
81 if lose > 2:
82 #返回False
83 return 1000
84 #如果丢包数目小于等于2个,获取平均耗时的时间
85 else:
86 #平均时间
87 average = waste_time.findall(out)
88 #当匹配耗时时间信息失败,默认三次请求严重超时,返回平均好使1000ms
89 if len(average) == 0:
90 return 1000
91 else:
92 #
93 average_time = int(average[0])
94 #返回平均耗时
95 return average_time
96
97"""
98函数说明:初始化正则表达式

Callers 1

daili.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected