How to check the internet connection in Python?

Nowadays, the Internet is an essential part of our day-to-day lives. If the server is down even for a minute, then we check the Internet connectivity in various ways. Can Python help us check the connectivity? Yes, we can use Python language for checking the internet connection. In this tutorial, we will find out whether the computer is connected to the internet or not.

Checking Internet Connection in Python

Below we have described two methods of checking the internet connection in Python.

  1. using urllib package
  2. using an IP address/socket package:

By using an urllib package

To fetch URLs, we use urllib.request module in Python. This can fetch URLs using a variety of different protocols.

One of the functions present in the package are urllib.request.urlopen().
Syntax of urllib.request.urlopen() is

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

Here, we will specify only the URL. And the rest of the parameters to their default values.

Let’s see how to use it for checking the internet-

import urllib.request

def connect(host=''):
        urllib.request.urlopen(host) #Python 3.x
        return True
        return False

# test
print( ‘connected’ if connect() else ‘no internet!’ )



Initially, we import the urllib package. Then we used a function ‘connected()‘, whose parameter is the URL. It is unnecessary to use the URL as  ’’, it can be any URL. Inside the try block, we implemented the above method to check internet connectivity. As you can observe, instead of passing the URL, we have kept host over there which has the value of URL.
If the Internet connection is present, then it returns True else the control goes to except block and it returns False.
With the help of the print statement, if the returned value is true then ‘Connected’ else ‘no internet!’ is printed.

Let’s see the above program by not giving any parameter to the function-

import urllib.request

def connect():
        urllib.request.urlopen('') #Python 3.x
        return True
        return False

print( 'connected' if connect() else 'no internet!' )


no internet!

Note: In Python 2.x, we replace urllib.request.urlopen() by urllib.urlopen() .

By using an IP address/By using socket package:

Here, we will use an indirect way when compared to the previous method. is a special-purpose IPv4 address also known as localhost. All computers use this address as their own, however, it will not allow them to communicate with different devices as a real IP address does. The computer you’re on only uses the loopback address.

We import socket to check the connectivity. Socket programming is a method of connecting 2 nodes on a network to speak with one another.
You can find the IP using socket.gethostbyname() methodBelow is the program to check the internet connection:

import socket
if IPaddress=="":
    print("No internet, your localhost is "+ IPaddress)
    print("Connected, with the IP address: "+ IPaddress )


No internet, your localhost is

Initially, we import the socket package. Then fetch the IP address using socket.gethostbyname() method. When the Internet connection is absent, then it fetches the IP address as Now, If-Else conditional statement is used to check whether or not the system is connected to the internet.


The above specified are the two ways to check the internet connection.

Also read:

Introduction to SQLite 3 with example in Python

2 responses to “How to check the internet connection in Python?”

  1. Tuhin says:

    The content was really helpful and also quite unique on the INTERNET, saved my 30 mins of googling (at least).

  2. Thomas Philip says:

    Thanks a lot.. really precise and clear.

Leave a Reply

Your email address will not be published. Required fields are marked *