Skip to content

Instantly share code, notes, and snippets.

@Makistos
Created February 1, 2017 14:21
Show Gist options
  • Save Makistos/451e5724825f68f4b0caa82cbff2aba3 to your computer and use it in GitHub Desktop.
Save Makistos/451e5724825f68f4b0caa82cbff2aba3 to your computer and use it in GitHub Desktop.
Run commands in shell with error handling. #python #subprocess
#!/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