Skip to content

Instantly share code, notes, and snippets.

@nartamonov
Last active May 20, 2016 14:37
Show Gist options
  • Save nartamonov/ce49639d11ef76edc202d22a5144ca67 to your computer and use it in GitHub Desktop.
Save nartamonov/ce49639d11ef76edc202d22a5144ca67 to your computer and use it in GitHub Desktop.
Waiting HTTP 200 from web service
#!/usr/bin/env python
# DEPENDENCIES:
# - requests
# Installation: aptitude install python-requests
import requests
import sys
from time import time, sleep
if len(sys.argv) != 3:
print("Usage: wait-response.py <URL> <TIMEOUT>\n")
print("Example:\n\twait-response.py http://google.com 30")
sys.exit(1)
url = sys.argv[1]
timeout = int(sys.argv[2])
t0 = time()
while True:
try:
resp = requests.get(url, timeout=timeout, verify=False)
if resp.status_code == 200:
break
except requests.exceptions.Timeout:
print("Timeout.")
sys.exit(1)
except requests.exceptions.ConnectionError:
pass
sleep(1)
current_time = time()
if time() - t0 >= timeout:
print("Timeout.")
sys.exit(1)
#!/usr/bin/env bash
T0=$(date +%s)
while true; do
STATUS=$(curl --write-out "%{http_code}\n" -k --silent --output /dev/null https://yandex.ru)
if [ "$STATUS" = "200" ]; then
exit 0
fi
sleep 1
T=$(date +%s)
if [ $(( $T - $T0 )) -gt 30 ]; then
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment