Skip to main content

A Python 3 package which allows you to monitor and observe online reachability, and react to these status changes as necessary.

Project description

Reachability

This is a work-in-progress package. Any contributions and suggestions, especially for a more Pythonic solution are welcome.

Reachability is a Python 3 package which allows you to monitor and observe online reachability, and react to these status changes as necessary.

This is done by periodically pinging the Cloudflare DNS IP (1.1.1.1). Reachability is customisable, so you can change the interval and host to ping as necessary.

To start with, we can easily check the current status from an instance of the reachability class:

from reachability import Reachability

reachability = Reachability()
print(reachability.isonline())

The above show True if connected to the internet, or False if not.

To set up Reachability, instead you can use:

reachability = Reachability(host='8.8.4.4', port=53, timeout=3, status_check_interval=4)

The above will instead ping 8.8.4.4 (Google's DNS Service), on port 53 with a timeout of 3 seconds. status_check_interval is used in more advanced use cases to monitor any status changes as shown below.

Observing changes

Functions and Lambdas

reachability.attach_func(lambda x: print(x))
reachability.start_notify()

This will print True or False on each status change. If for example, we are initially connected to the internet, True will be printed. If the Internet connection is then interrupted, the lambda will be executed again this time printing False.

This will continue until

reachability.stop_notify()

is called. Note however that the lambda will still be attached. To detach it, we must call

reachability.detach_func(x)

where x is a reference to the lambda or function.

Obviously, you can also use something like

def f(isonline):
    if isonline:
        print("FunctionA: We are online")
    else:
        print("FunctionA: We are offline")

reachability.attach_func(f)

Observers

Another option is to use an Observer Class as demonstrated below:

from reachability import ReachabilityObserver, Reachability


class ObserverA(ReachabilityObserver):
    def reachability_update(self, isonline: bool) -> None:
        if isonline:
            print("ObserverA: We are online")
        else:
            print("ObserverA: We are offline")

reachability = Reachability()
observer_a = ObserverA()
reachability.attach(observer_a)
reachability.start_notify()

ReachabiltyObserver is a simple interface. Classes which use it will implement

def reachability_update(self, is_online: bool) -> None

This method will then be called on Online Reachability changes.

You can have multiple observers attached to a reachability instance. To detach them, simply call

reachability.attach(observer_a) #Detach ObserverA
reachability.stop_notify() #Stop any further notifications

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reachability-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

reachability-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file reachability-0.1.0.tar.gz.

File metadata

  • Download URL: reachability-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for reachability-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1012e630687ab33ddf508f9343e37126802c3db5ba352738a51028c3b065ab0e
MD5 8bb1120947a55c5e2153065c498ab30f
BLAKE2b-256 1fceab5292c6afc4093dd7a46ec0a8e000cbb247c611f749819e5dbd38b8f03f

See more details on using hashes here.

File details

Details for the file reachability-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: reachability-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for reachability-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a308ee5395d0159fb6a6259daf08eaea5c4b98a8085e9068b2bbc06afb41a506
MD5 a0a8d47d13827b5d4ca5d15f207ef3cd
BLAKE2b-256 733e00abed1093376501fa73027e884905017aac959ad707161451563e7cf493

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page