Created
March 23, 2018 09:17
-
-
Save rivmar/f218d27da8ec32c34362a5e687df400c to your computer and use it in GitHub Desktop.
[Python] Get server ping time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import subprocess | |
host = 'google.com' | |
try: | |
output = subprocess.check_output(['ping', '-c', '4', '-q', host]) | |
output = output.decode('utf8') | |
statistic = re.search(r'(\d+\.\d+/){3}\d+\.\d+', output).group(0) | |
avg_time = re.findall(r'\d+\.\d+', statistic)[1] | |
response_time = float(avg_time) | |
except subprocess.CalledProcessError: | |
response_time = 99999999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment