How to check whether users internet is on or off using Python? Last Updated : 10 Nov, 2022 Comments Improve Suggest changes Like Article Like 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 OnUsing 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 url = "https://www.geeksforgeeks.org" 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 offUsing 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: TrueUsing 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 Comment More infoAdvertise with us Next Article How to check whether users internet is on or off using Python? P parthbanathia Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads How to Check Loading Time of Website using Python In this article, we will discuss how we can check the website's loading time. Do you want to calculate how much time it will take to load your website? Then, what you must need to exactly do is subtract the time obtained passed since the epoch from the time obtained after reading the whole website. 3 min read Test the given page is found or not on the server Using Python In this article, we are going to write a Python script to test the given page is found or not on the server. We will see various methods to do the same. Method 1: Using Urllib. Urllib is a package that allows you to access the webpage with the program. Installation: pip install urllib Approach: Impo 2 min read Python PRAW â Check whether a redditor has Reddit premium or not In Reddit, a redditor is the term given to a user. Reddit allows redditors to avail premium access by paying a fee. Here we will see how to check whether a redditor has the premium access or not. We will be using the is_gold attribute of the Redditor class to check whether a redditor has the premium 2 min read How to connect WiFi using Python? Seeing a computer without an active internet connection today is next to impossible. The Internet has been of the utmost importance in the 21st Century. There are multiple ways one can connect their machine to the Internet. The first being, the traditional cables, i.e. the Ethernet, and the other be 5 min read How to Disconnect Devices from Wi-Fi using Scapy in Python? Without permission, disconnecting a device from a Wi-Fi network is against the law and immoral. Sometimes, you might need to check your network's security or address network problems. You may send a de-authentication packet to disconnect devices from Wi-Fi using Scapy, a potent Python packet manipul 5 min read View Computer's Important Network Information Using Python While working in a network, we do some troubleshooting with the network or Internet problem. This time we need to check your own system network connection information. We can find the network connection in the Control Panel in Windows. The best way to find this information we can use ipconfig comman 1 min read How to Automate VPN to change IP location on Ubuntu using Python? To Protect Our system from unauthorized users Access you can spoof our system's IP Address using VPN service provided by different organizations. You can set up a VPN on your system for free.  After you set up and log in to the VPN over the Ubuntu system you need to manually connect with different 3 min read Python Tweepy â Checking whether a tweet has been retweeted or not In this article we will see how we can check whether a tweet/status has been retweeted by the authenticated user or not. The retweeted attribute of the Status object indicates whether the status has been retweeted by the authenticated user or not. Identifying whether the status has been retweeted by 2 min read Building CLI to check status of URL using Python In this article, we will build a CLI(command-line interface) program to verify the status of a URL using Python. The python CLI takes one or more URLs as arguments and checks whether the URL is accessible (or)not. Stepwise ImplementationStep 1: Setting up files and Installing requirements First, cr 4 min read How to find available WiFi networks using Python? WiFi (Wireless Fidelity) is a wireless technology that allows devices such as computers (laptops and desktops), mobile devices (smartphones and wearables), and other equipment (printers and video cameras) to interface with the Internet. We can find out the names of the WiFi names with the help of Py 2 min read Like