【发布时间】:2022-01-11 14:51:29
【问题描述】:
我有一个 Python 脚本,它通过 ping 运行,每 30 秒确定一次互联网速度,查看用户是否可以通过互联网接听电话,ping 通常不足以知道这一点,所以我想要更多信息,例如下载速度和网络上传速度等等。
如何在 Python 中发生这种情况而不会对 Internet 产生重大影响,从而不会导致 Internet 速度变慢
`
def check_ping(host):
"""
Returns formated min / avg / max / mdev if there is a vaild host
Return None if host is not vaild
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower() == 'windows' else '-c'
# Building the command. Ex: "ping -c 1 $host"
command = ['ping', param, '3', host]
# ask system to make ping and return output
ping = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, error = ping.communicate()
matcher = re.compile(
"(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
# rtt min/avg/max/mdev =
ping_list = r"Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms"
try:
if(not error):
if(platform.system().lower() == 'windows'):
response = re.findall(ping_list, out.decode())[0]
return response
else:
response = matcher.search(out.decode()).group().split('/')
return response
except Exception as e:
logging.error(e)
return None
`
【问题讨论】: