I'm trying to do a ping test and if the ping is successful, call a function and run that function until the ping becomes unsuccessful. If the ping becomes unsuccessful after calling the function, stop the function from running until ping ping becomes successful.
So far, this is what I've tried. The ping test works well, however the while loop exits out the automation function (closes the browser) after running all of the code in the function. I'd like to continue running function and stop it only when until the ping fails.
import urllib
from selenium import webdriver
def ping(host="8.8.8.8", port=53, timeout=3):
from urllib import request
try:
urllib.request.urlopen('http://google.com', timeout=5)
return True
except:
return False
def automation():
url1 = "https://google.com"
driver=webdriver.Chrome()
driver.get(url1)
run_once = 0
while True:
if ping():
if run_once == 0:
automation()
run_once = 1
else:
print("ping unsuccessful")
run_once = 0
UPDATE
This is the solution that worked for me thanks to user @questioning answer. I also used 'drive.refersh' in order to save battery life because my use case involves a camera, and calling driver before the while-loop keeps the browser and camera running even after the connection is lost.
import urllib
from selenium import webdriver
Global driver
driver=webdriver.Chrome()
def ping(host="8.8.8.8", port=53, timeout=3):
from urllib import request
try:
urllib.request.urlopen('http://google.com', timeout=5)
return True
except:
return False
def automation():
url1 = "https://google.com"
driver=webdriver.Chrome()
driver.get(url1)
run_once = 0
reload_once = 0
while True:
if ping():
if run_once == 0:
automation()
run_once = 1
else:
print("ping unsuccessful")
run_once = 0
if reload_once == 0:
drive.refersh()
reload_once == 1
