Last active
December 19, 2019 19:25
-
-
Save Gueka/d228192ad07f4880febe1753cd552df3 to your computer and use it in GitHub Desktop.
python3 http-ping.py http://test.com.ar
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
#! /usr/bin/python3 | |
import pip._vendor.requests as requests | |
import sys | |
import time | |
import socket | |
import re | |
# TODO add a validation param | |
url = sys.argv[1] | |
counter = 0 | |
success_counter = 0 | |
def pinger(url): | |
global counter | |
global success_counter | |
while True: | |
try: | |
response = requests.get(url) | |
print('status=' + str(response.status_code) + ' time=' + str(response.elapsed.total_seconds()) + ' ms') | |
success_counter += 1 | |
finally: | |
counter += 1 | |
time.sleep(.300) | |
start_time = time.time() | |
try: | |
clean_host = re.sub(r'(http|https)?:\/\/', '', url) | |
host = socket.gethostbyname(clean_host) | |
except: | |
print('ping: cannot resolve ' + url + ': Unknown host') | |
exit() | |
try: | |
print('PING ' + url + ' (' + host + ')' ) | |
pinger(url) | |
except KeyboardInterrupt: | |
print('\n--- ' + url + ' ping statistics ---') | |
#3 packets transmitted, 3 received, 0% packet loss, time 2017ms | |
elapsed_time = time.time() - start_time | |
percent = ((counter - success_counter) * 100) / counter | |
message = "{} ping transmitted, {} received, {}% packet loss, time {}s" | |
print(message.format(counter, success_counter, round(percent, 2),round(elapsed_time, 4))) | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment