Skip to content

Instantly share code, notes, and snippets.

@Gueka
Last active December 19, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gueka/d228192ad07f4880febe1753cd552df3 to your computer and use it in GitHub Desktop.
Save Gueka/d228192ad07f4880febe1753cd552df3 to your computer and use it in GitHub Desktop.
python3 http-ping.py http://test.com.ar
#! /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