Page 1 of 1

Recovering from Lost Internet

PostPosted: Thu Mar 01, 2018 5:20 pm
by DaveL17
I'm getting results that I don't understand. Using this construct in terminal works as expected:
Code: Select all
import os
from time import sleep

response = '1'

while response != 0:
    response = os.system("ping -c 1 google.com")
    sleep(2)
    print('.')

On the first trip through, the result with Internet is instant:
Code: Select all
PING google.com (172.217.4.78): 56 data bytes
64 bytes from 172.217.4.78: icmp_seq=0 ttl=49 time=29.679 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 29.679/29.679/29.679/0.000 ms
.

Remove Internet, run, and then restore Internet:
Code: Select all
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
PING google.com (172.217.4.78): 56 data bytes
64 bytes from 172.217.4.78: icmp_seq=0 ttl=49 time=47.809 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 47.809/47.809/47.809/0.000 ms
.

Same code, modified to run in a plugin:
Code: Select all
except requests.exceptions.ConnectionError:

    while response != 0:
        response = os.system("ping -c 1 google.com")
        self.sleep(2)
        indigo.server.log(u"{0}".format(response))

When Internet is removed, requests throws the exception and the while loop is called. However, when Internet is restored, the while loop is never broken, the ping response never reverts to 0 (zero) and instead just keeps reporting the non-zero number.

What am I missing?

Re: Recovering from Lost Internet

PostPosted: Thu Mar 01, 2018 6:10 pm
by Colorado4Wheeler
How about:

Code: Select all
while int(response) != 0:

or
Code: Select all
while unicode(response) != '0':

or
Code: Select all
while not response == '0'

Re: Recovering from Lost Internet

PostPosted: Thu Mar 01, 2018 7:30 pm
by FlyingDiver
Are you sure it's actually calling ping in that loop? Change it to "/sbin/ping" and see what happens. You might be getting an OS or shell error rather than a ping error.

Re: Recovering from Lost Internet

PostPosted: Thu Mar 01, 2018 8:01 pm
by DaveL17
FlyingDiver wrote:
Are you sure it's actually calling ping in that loop? Change it to "/sbin/ping" and see what happens. You might be getting an OS or shell error rather than a ping error.

Thanks Joe - calling it from /sbin/ was the answer. Now it's working perfectly. Appreciate it.

I would've probably figured it out on my own if Matt made the font bigger. :D