Open In App

How to check whether users internet is on or off using Python?

Improve
Improve
Like Article
Like
Save
Share
Report

Many times while developing our projects we require a solution for checking whether the user system’s internet is on or off below are some simple solutions for checking that using Python.

Using httplib to check whether users internet is on or off

We imported http.client library. Initialized URL as www.geeksforgeeks.org We tried to establish a connection with the given URL. Requested only the header of Web Page for fast operations. True is returned if the connection is on and the message is displayed. An exception is caught if it’s not working and the error message is displayed.

Python3




# importing required module
import http.client as httplib
 
 
# function to check internet connectivity
def checkInternetHttplib(url="www.geeksforgeeks.org",
                         timeout=3):
    connection = httplib.HTTPConnection(url,
                                        timeout=timeout)
    try:
        # only header requested for fast operation
        connection.request("HEAD", "/")
        connection.close()  # connection closed
        print("Internet On")
        return True
    except Exception as exep:
        print(exep)
        return False
 
 
checkInternetHttplib("www.geeksforgeeks.org", 3)


Output:

Internet On

Using requests.get() to check whether users internet is on or off

Importing the required requests module. Initializing URL to geeksforgeeks.org. Initializing timeout to be 10. Requesting the given URL. Printing “Internet is on” or going to generate exceptions. Catching exception and printing “Internet is off”.

Python3




# importing requests module
import requests
 
# initializing URL
timeout = 10
try:
    # requesting URL
    request = requests.get(url,
                           timeout=timeout)
    print("Internet is on")
 
# catching exception
except (requests.ConnectionError,
        requests.Timeout) as exception:
    print("Internet is off")


Output:

Internet is off

Using socket module to check whether users internet is on or off

Import the socket module and try establishing the connection. Use the create_connection() method to establish the connection. Connect to the host and tell whether the host is actually reachable or not. Create_connection only connects to TCS sockets. If connection is established then return True otherwise return False.

Python3




import socket
def isConnect():
    try:
        s = socket.create_connection(
              ("www.geeksforgeeks.org", 80))
        if s is not None:
            s.close
        return True
    except OSError:
        pass
    return False
print(isConnect())


Output:

True

Using lambda function one-liner to check whether users internet is on or off

In this, we will import the os module within the lambda function and we ping using os module the default gateway IP of our network in our code here

For example, 192.168.0.1 is the gateway for My network

Note: (The gateway IP may be different than mine)

Python3




# code
def internet_on():
    return (lambda a: True if 0 == a.system('ping 192.168.0.1 -n 3 -l 32 -w 3 > clear') else False)(__import__('os'))
 
 
print(internet_on())


Output:

True


Last Updated : 10 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads