walterfaye
walterfaye

Reputation: 869

How to programmatically check if PC has internet connectivity using Python and/or Ruby?

I have scripts in both Python and Ruby that run for days at a time and rely on the internet to go to certain domains and collect data. Is there a way to implement a network connectivity check into my script so that I could pause/retry iterations of a loop if there is no connectivity and only restart when there is connectivity?

Upvotes: 0

Views: 2331

Answers (5)

kindall
kindall

Reputation: 184270

In Python you can do something like this:

def get_with_retry(url, tries=5, wait=1, backoff=2, ceil=60):
    while True:
        try:
            return requests.get(url)
        except requests.exceptions.ConnectionError:
            tries -= 1
            if not tries:
                raise
            time.sleep(wait)
            wait = min(ceil, wait * backoff)

This tries each request up to tries times, initially delaying wait seconds between attempts, but increasing the delay by a factor of backoff for each attempt up to a maximum of ceil seconds. (The default values mean it will wait 1 second, then 2, then 4, then 8, then fail.) By setting these values, you can set the maximum amount of time you want to wait for the network to come back, before your main program has to worry about it. For infinite retries, use a negative value for tries since that'll never reach 0 by subtracting 1.

At some point you want the program to tell you if it can't get on the network, and you can do that by wrapping the whole program in a try/except that notifies you in some way if ConnectionError occurs.

Upvotes: 0

Shannarra
Shannarra

Reputation: 559

Inline way of doing it:

require 'open-uri'

def internet_access?; begin open('http://google.com'); true; rescue => e; false; end; end

puts internet_access?

Upvotes: 0

Cameron Sparr
Cameron Sparr

Reputation: 3981

here's a unix-specific solution:

In [18]: import subprocess

In [19]: subprocess.call(['/bin/ping', '-c1', 'blahblahblah.com'])

Out[19]: 1

In [20]: subprocess.call(['/bin/ping', '-c1', 'google.com'])

Out[20]: 0

ie, ping will return 0 if the ping is successful

Upvotes: 1

dliggat
dliggat

Reputation: 458

There may be a more elegant solution, but I'd do this:

require 'open-uri'

def internet_connectivity?
  open('http://google.com')
  true
rescue => ex
  false
end

Upvotes: 5

PepperoniPizza
PepperoniPizza

Reputation: 9112

Well in Python I do something similar with a try except block like the following:

import requests

try:
    response = requests.get(URL)
except Exception as e:
    print "Something went wrong:"
    print e

this is just a sample of what you could do, you can check for error_code or some information on the exception and according to that you can define what to do. I usually put the script to sleep for 10 minutes when something goes wrong on the request.

import time
time.sleep(600)

Upvotes: 2

Related Questions