-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connection.py
35 lines (31 loc) · 993 Bytes
/
Connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
import time
try:
import requests
except:
sys.exit("please install requests python package")
def waitUntilConnection(url, maxTries=None, interval=1, verbose=False):
tries = 0
success = False
while not success:
if maxTries and tries > maxTries:
if verbose:
print "Max tries reached. Connection unsuccessful."
break
if verbose:
print "Trying to connect to url", url
tries += 1
try:
requests.get(url)
except:
if verbose:
print "Failed, trying again in %d seconds" % interval
time.sleep(interval)
continue
if verbose:
print "Success!"
success = True
return success
if __name__ == "__main__":
success = waitUntilConnection("http://127.0.0.1:3000",maxTries=10,interval=1,verbose=True)
print "Script finished with return value", success