Created
February 1, 2017 14:21
-
-
Save Makistos/451e5724825f68f4b0caa82cbff2aba3 to your computer and use it in GitHub Desktop.
Run commands in shell with error handling. #python #subprocess
This file contains hidden or 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/python | |
def tryUrl(url): | |
try: | |
# check_output returns output from command so it could be printed here | |
subprocess.check_output('ping ' + url, shell=True) | |
return 1 | |
except subprocess.CalledProcessError, e: | |
print 'Connection failed, error: %s' % e.output | |
return 0 | |
def connectivityTestWithFail(): | |
required = 6 | |
attempts = 5 | |
urls = ['http://www.google.com', 'http://www.amazon.com'] | |
working = 0 | |
for url in urls * attempts: | |
working += tryUrl(url) | |
if working >= required: | |
break | |
print str(working) + "\n" | |
#connectivityTestWithFail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment