1

I know there are ways to check if there currently is internet, but I couldn't find anyway to actually call a function when internet is found. does something like this exist?

I'm using Ubuntu, and don't need to support any other operating system. preferably python 3. I'm fine installing external libraries to do this.

QxQ
  • 675
  • 7
  • 18
  • 3
    You could handle one of NetworkManager's dbus signals: http://stackoverflow.com/questions/4401851/dbus-signal-handler-for-e-networkmanager-vpn-connection – Blender Feb 26 '13 at 05:03
  • The pythonic way would be to just `try:` it, and handle the case where you can't in an `except:` block. – wim Feb 26 '13 at 05:03
  • it needs to call this function when internet is found, I don't care if there is internet or not when the program first runs, just every time internet is connected while the program is running, also I don't want to check for internet every minuet or so, because that can miss things like quickly switching to different modum, etc, and it doesn't work with the program I want to make. – QxQ Feb 26 '13 at 05:05
  • the dbus that blender suggestion seems to be useful, it looks like there should be a way to do this using it, but I haven't figured out anything yet. I'll look into this more later. – QxQ Feb 26 '13 at 05:37

2 Answers2

1

The poor man's way would be using polling with either a ping module

import ping, time

def found():
    print("FOUND INTERNET! :)")
def lost():
    print("LOST INTERNET! :(")

def wait_and_notify_connection(found, lost, already_found=False):
    while True:
        # Ping Google DNS Server (99.999% Uptime)
        if ping.do_one('8.8.8.8', timeout=2, psize=64) is not None:                
            if not already_found:
                found()
                already_found = True
        else:
            if already_found:
                lost()
                already_found = False
        time.sleep(1)

wait_and_notify_connection(found, lost)

Or a subprocess call

import subprocess, time

def found():
    print("FOUND INTERNET! :)")
def lost():
    print("LOST INTERNET! :(")

def ping(target):
    return True if subprocess.call(['ping', '-c 1', target]) == 0 else False

def wait_and_notify_connection(found, lost, already_found=False):
    while True:
        # Ping Google DNS Server (99.999% Uptime)
        # and check return code
        if ping('8.8.8.8'):
            if not already_found:
                found()
                already_found = True
        else:
            if already_found:
                lost()
                already_found = False
        time.sleep(1)

wait_and_notify_connection(found, lost)

But as @Blender mentioned, D-Bus notification could work better. Something like NetworkManager D-Bus Python client and reading some specs could help.

You can use Python's threading interface for background polling as well

Ric
  • 8,615
  • 3
  • 17
  • 21
-2

Previously answered here.

import urllib2

def internet_on(url):
    try:
        response=urllib2.urlopen(url,timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False
Community
  • 1
  • 1
Brandon Romano
  • 1,022
  • 1
  • 13
  • 21
  • 1
    This function returns `False` for me. – Blender Feb 26 '13 at 05:05
  • I edited my post, I shouldn't have blindly copied the post, I'm pretty sure the post used an old google i.p. address. Now pass in any URL you wish to test. – Brandon Romano Feb 26 '13 at 05:11
  • not the answer I was looking for because I need something like an event listener, however, for googlers, the python 3 way of doing this answer would be to use "from urllib.request import urlopen" instead of "import urllib2", and use urlopen() instead of urllib2.urlopen() – QxQ Feb 26 '13 at 05:11