174

I want to see if I can access an online API, but for that, I need to have Internet access.

How can I see if there's a connection available and active using Python?

7
  • if you're in python, it will almost certainly throw an exception in the event of a connection failure or timeout. You can either try/catch around that, or just let it surface.
    – jdizzle
    Sep 21, 2010 at 20:47
  • 1
    @Triptych: I hope that was a joke, because it doesn't work Sep 21, 2010 at 21:48
  • 2
    @inspectorG4dget: easy_install system_of_tubes
    – S.Lott
    Sep 21, 2010 at 23:09
  • 2
    @aF: Look at Unutbu's solution. It shows (in the fastest possible way) how to check if you are able to access google.com. If your problem concerns accessing the API, then why not try accessing it, setting timeout=1? That will tell you exactly if the API that you want to access is accessible. Once you know that, then you can go ahead and access the API or wait for a time when you are able to access it. Sep 22, 2010 at 1:56
  • Like I said, I just needed a simple thing like unutbu said. You don't need to make such a fuss with this..
    – aF.
    Sep 22, 2010 at 8:54

22 Answers 22

152

Perhaps you could use something like this:

from urllib import request

def internet_on():
    try:
        request.urlopen('http://216.58.192.142', timeout=1)
        return True
    except request.URLError as err: 
        return False

For Python 2.x replace the import statement by import urllib2 as request:

Currently, 8.8.8.8 is one of the IP addresses from Google. Change http://8.8.8.8 to whatever site can be expected to respond quickly.

This fixed IP will not map to google.com forever. So this code is not robust -- it will need constant maintenance to keep it working.

The reason why the code above uses a fixed IP address instead of fully qualified domain name (FQDN) is because a FQDN would require a DNS lookup. When the machine does not have a working internet connection, the DNS lookup itself may block the call to urllib_request.urlopen for more than a second. Thanks to @rzetterberg for pointing this out.


If the fixed IP address above is not working, you can find a current IP address for google.com (on unix) by running

% dig google.com  +trace 
...
google.com.     300 IN  A   216.58.192.142
18
  • 1
    Just a note on the "the call to urlopen will not take much longer than 1 second even if the internet is not "on"."-quote. This is not true if the url supplied is invalid, then the DNS lookup will block. It is only true for the actual connection to the web-server. The simplest way to avoid this DNS lookup block is to use the IP-adress instead, then it's guaranteed to only take 1 second :) Dec 31, 2011 at 11:56
  • 9
    THIS NO LONGER WORKS. As of Sep 2013, 74.125.113.99 times out after long time, so this function will always return False. I suppose Google has changed their network is set up.
    – theamk
    Sep 18, 2013 at 12:00
  • 4
    Now the rest is simple. Google for "74.125.113.99 urllib". It will return thousands of open-source projects which incorporated the incorrect code. (for me, just the first page contains at least 5: nvpy, sweekychebot, malteseDict, upy, checkConnection). They are all broken now.
    – theamk
    Sep 19, 2013 at 19:02
  • 4
    If it is not clear, I DO NOT RECOMMEND using this method. Old IP was good for less than 3 years. The new IP works today. Put it in your project, and it might mysteriously break in less than 3 years, again.
    – theamk
    Sep 19, 2013 at 19:04
  • 2
    Er... just open http://google.com and then you solve all the problems everybody keeps talking about here. There's no need to use an ip address...
    – Jason C
    Oct 31, 2016 at 13:30
151

If we can connect to some Internet server, then we indeed have connectivity. However, for the fastest and most reliable approach, all solutions should comply with the following requirements, at the very least:

  • Avoid DNS resolution (we will need an IP that is well-known and guaranteed to be available for most of the time)
  • Avoid application layer connections (connecting to an HTTP/FTP/IMAP service)
  • Avoid calls to external utilities from Python or other language of choice (we need to come up with a language-agnostic solution that doesn't rely on third-party solutions)

To comply with these, one approach could be to, check if one of the Google's public DNS servers is reachable. The IPv4 addresses for these servers are 8.8.8.8 and 8.8.4.4. We can try connecting to any of them.

A quick Nmap of the host 8.8.8.8 gave below result:

$ sudo nmap 8.8.8.8

Starting Nmap 6.40 ( http://nmap.org ) at 2015-10-14 10:17 IST
Nmap scan report for google-public-dns-a.google.com (8.8.8.8)
Host is up (0.0048s latency).
Not shown: 999 filtered ports
PORT   STATE SERVICE
53/tcp open  domain

Nmap done: 1 IP address (1 host up) scanned in 23.81 seconds

As we can see, 53/tcp is open and non-filtered. If you are a non-root user, remember to use sudo or the -Pn argument for Nmap to send crafted probe packets and determine if a host is up.

Before we try with Python, let's test connectivity using an external tool, Netcat:

$ nc 8.8.8.8 53 -zv
Connection to 8.8.8.8 53 port [tcp/domain] succeeded!

Netcat confirms that we can reach 8.8.8.8 over 53/tcp. Now we can set up a socket connection to 8.8.8.8:53/tcp in Python to check connection:

import socket

def internet(host="8.8.8.8", port=53, timeout=3):
    """
    Host: 8.8.8.8 (google-public-dns-a.google.com)
    OpenPort: 53/tcp
    Service: domain (DNS/TCP)
    """
    try:
        socket.setdefaulttimeout(timeout)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
        return True
    except socket.error as ex:
        print(ex)
        return False

internet()

Another approach could be to send a manually crafted DNS probe to one of these servers and wait for a response. But, I assume, it might prove slower in comparison due to packet drops, DNS resolution failure, etc. Please comment if you think otherwise.

UPDATE #4: This listing of public nameservers is a good reference for IPs to test against.

UPDATE #3: Tested again after the exception handling change:

defos.py
True
00:00:00:00.410

iamaziz.py
True
00:00:00:00.240

ivelin.py
True
00:00:00:00.109

jaredb.py
True
00:00:00:00.520

kevinc.py
True
00:00:00:00.317

unutbu.py
True
00:00:00:00.436

7h3rAm.py
True
00:00:00:00.030

UPDATE #2: I did quick tests to identify the fastest and most generic implementation of all valid answers to this question. Here's the summary:

$ ls *.py | sort -n | xargs -I % sh -c 'echo %; ./timeit.sh %; echo'
defos.py
True
00:00:00:00.487

iamaziz.py
True
00:00:00:00.335

ivelin.py
True
00:00:00:00.105

jaredb.py
True
00:00:00:00.533

kevinc.py
True
00:00:00:00.295

unutbu.py
True
00:00:00:00.546

7h3rAm.py
True
00:00:00:00.032

And once more:

$ ls *.py | sort -n | xargs -I % sh -c 'echo %; ./timeit.sh %; echo'
defos.py
True
00:00:00:00.450

iamaziz.py
True
00:00:00:00.358

ivelin.py
True
00:00:00:00.099

jaredb.py
True
00:00:00:00.585

kevinc.py
True
00:00:00:00.492

unutbu.py
True
00:00:00:00.485

7h3rAm.py
True
00:00:00:00.035

True in the above output signifies that all these implementations from respective authors correctly identify connectivity to the Internet. Time is shown with milliseconds resolution.

UPDATE #1: Thanks to @theamk's comment, timeout is now an argument and initialized to 3s by default.

37
  • 8
    This is the best answer, avoiding DNS resolution, and ironically, consulting Google's DNS service on port 53.
    – m3nda
    Dec 20, 2015 at 9:59
  • 1
    @AviMehenwal Google provides this IP as part of its free to use DNS name resolution service. The IP is not supposed to change unless Google decides otherwise. I've been using it as an alternative DNS server for few years now without any issues.
    – 7h3rAm
    Apr 21, 2016 at 15:11
  • 2
    @JasonC Connecting to TCP/53 is different than DNS connection over port 53. Going by your analogy, every TCP port that is used by an application will be classified as application level protocol connection. No, that's not true. Connecting over a TCP socket to a port IS NOT application level connection. I'm not doing a DNS lookup, rather just a half-TCP handshake. It's not the same thing as doing a full DNS lookup.
    – 7h3rAm
    Nov 4, 2016 at 9:46
  • 4
    Shouldn't we call close() on the socket? Jul 5, 2017 at 5:53
  • 2
    Thanks again for this answer. Turned this into a windows taskbar icon: gitlab.com/room150/always-on-indicator
    – snitch182
    Feb 10, 2020 at 14:46
104

It will be faster to just make a HEAD request so no HTML will be fetched.

try:
    import httplib  # python < 3.0
except:
    import http.client as httplib


def have_internet() -> bool:
    conn = httplib.HTTPSConnection("8.8.8.8", timeout=5)
    try:
        conn.request("HEAD", "/")
        return True
    except Exception:
        return False
    finally:
        conn.close()
8
  • 14
    Takes only 20 milli seconds while the top answer takes 1 second.
    – Wally
    Apr 6, 2016 at 14:04
  • 11
    +1 It makes no sense to download the whole page if you only want to check if the server is reachable and responding. Download the content if you need the content
    – thatsIch
    Jan 5, 2017 at 11:25
  • For no reason, using the url '8.8' still work, which obviously, '8.8' or ' 8.8 ' does not work in a real web browser.
    – Polv
    Aug 17, 2018 at 16:40
  • 2 hours looking for a working solution and then I found this... clean and simple Oct 16, 2019 at 23:08
  • 1
    conn.close() can be moved to finally. This part will execute regadless there is return in try or except block. Oct 17, 2021 at 11:18
30

As an alternative to ubutnu's/Kevin C answers, I use the requests package like this:

import requests

def connected_to_internet(url='http://www.google.com/', timeout=5):
    try:
        _ = requests.head(url, timeout=timeout)
        return True
    except requests.ConnectionError:
        print("No internet connection available.")
    return False

Bonus: this can be extended to this function that pings a website.

def web_site_online(url='http://www.google.com/', timeout=5):
    try:
        req = requests.head(url, timeout=timeout)
        # HTTP errors are not raised by default, this statement does that
        req.raise_for_status()
        return True
    except requests.HTTPError as e:
        print("Checking internet connection failed, status code {0}.".format(
        e.response.status_code))
    except requests.ConnectionError:
        print("No internet connection available.")
    return False
5
  • 3
    Request is a great library. However unlike many examples here I would use IANA's example.com or example.org as a more reliable long-term choice since it's fully controlled by ICANN.
    – chirale
    May 6, 2017 at 15:05
  • Is there any other way to check internet running status. Because if you pining google.com again again, then they will block our IP. So Is there any other way.?
    – Sanjiv
    Aug 21, 2019 at 8:32
  • 1
    Very nice. I modified this using requests.head() method instead to only get a HEAD response.
    – howdoicode
    Nov 12, 2020 at 18:42
  • @howdoicode, edited my answer accordingly. much faster, indeed
    – Def_Os
    Nov 12, 2020 at 20:21
  • Is there any reason for the assignment? Can _ = requests.head... be replaced with just requests.head... ? Mar 31, 2022 at 20:34
17

Just to update what unutbu said for new code in Python 3.2

def check_connectivity(reference):
    try:
        urllib.request.urlopen(reference, timeout=1)
        return True
    except urllib.request.URLError:
        return False

And, just to note, the input here (reference) is the url that you want to check: I suggest choosing something that connects fast where you live -- i.e. I live in South Korea, so I would probably set reference to http://www.naver.com.

1
  • 1
    I will wait 30 seconds if there is not a DNS server available. Jan 30, 2019 at 16:53
10

You can just try to download data, and if connection fail you will know that somethings with connection isn't fine.

Basically you can't check if computer is connected to internet. There can be many reasons for failure, like wrong DNS configuration, firewalls, NAT. So even if you make some tests, you can't have guaranteed that you will have connection with your API until you try.

4
  • 2
    "It's easier to ask for forgiveness than permission"
    – cobbal
    Sep 21, 2010 at 20:45
  • this doesn't need to be that good. I simply need to try to connect to a site or ping something and if it gives timeout voilá. How can I do that?
    – aF.
    Sep 21, 2010 at 20:45
  • Is there any reason you can't just try to connect to API? Why you must to check it before real connection? Sep 21, 2010 at 20:51
  • I make an html page using python. The html page deppends on the arguments that I give in the python program. I only need to put some html code if I can access to the api. That's why I need to know before.
    – aF.
    Sep 21, 2010 at 20:58
6
import urllib

def connected(host='http://google.com'):
    try:
        urllib.urlopen(host)
        return True
    except:
        return False

# test
print( 'connected' if connected() else 'no internet!' )

For python 3, use urllib.request.urlopen(host)

5

Try the operation you were attempting to do anyway. If it fails python should throw you an exception to let you know.

To try some trivial operation first to detect a connection will be introducing a race condition. What if the internet connection is valid when you test but goes down before you need to do actual work?

5

Here's my version

import requests

try:
    if requests.get('https://google.com').ok:
        print("You're Online")
except:
    print("You're Offline")
2
  • 1
    I like this one, but it would be better if it excepted a specific Timeout or Connection Error that's raised from requests. As is, a lightning-quick Ctrl-C will print offline. Jun 6, 2019 at 0:29
  • 1
    The if statement is not needed.
    – Michael H.
    May 19, 2021 at 14:04
3

This might not work if the localhost has been changed from 127.0.0.1 Try

import socket
ipaddress=socket.gethostbyname(socket.gethostname())
if ipaddress=="127.0.0.1":
    print("You are not connected to the internet!")
else:
    print("You are connected to the internet with the IP address of "+ ipaddress )

Unless edited , your computers IP will be 127.0.0.1 when not connected to the internet. This code basically gets the IP address and then asks if it is the localhost IP address . Hope that helps

4
  • That's an interesting check, though it will only identify if you are connected to a network. Whether it is the Internet or a NAT'd subnet will still remain a question.
    – 7h3rAm
    Apr 22, 2016 at 5:42
  • @7h3rAm , that's a good point , the DCHP doesn't require internet connection , my mistake.
    – user5779230
    Apr 23, 2016 at 6:16
  • This just requires a network NIC connect that has received been assigned an IP. Unlike all the other answers, it does not require a connection to the LAN, router, firewall, ISP all the way up to another system. It still fails to show that the NIC is connected if it's a non DHCP assigned address (static)
    – user150471
    Mar 5, 2019 at 18:45
  • @user150471, already pointed out, thank you for your contribution.
    – user5779230
    Mar 7, 2019 at 19:43
2

A modern portable solution with requests:

import requests

def internet():
    """Detect an internet connection."""

    connection = None
    try:
        r = requests.get("https://google.com")
        r.raise_for_status()
        print("Internet connection detected.")
        connection = True
    except:
        print("Internet connection not detected.")
        connection = False
    finally:
        return connection

Or, a version that raises an exception:

import requests
from requests.exceptions import ConnectionError

def internet():
    """Detect an internet connection."""

    try:
        r = requests.get("https://google.com")
        r.raise_for_status()
        print("Internet connection detected.")
    except ConnectionError as e:
        print("Internet connection not detected.")
        raise e
1

Best way to do this is to make it check against an IP address that python always gives if it can't find the website. In this case this is my code:

import socket

print("website connection checker")
while True:
    website = input("please input website: ")
    print("")
    print(socket.gethostbyname(website))
    if socket.gethostbyname(website) == "92.242.140.2":
        print("Website could be experiencing an issue/Doesn't exist")
    else:
        socket.gethostbyname(website)
        print("Website is operational!")
        print("")
1

my favorite one, when running scripts on a cluster or not

import subprocess

def online(timeout):
    try:
        return subprocess.run(
            ['wget', '-q', '--spider', 'google.com'],
            timeout=timeout
        ).returncode == 0
    except subprocess.TimeoutExpired:
        return False

this runs wget quietly, not downloading anything but checking that the given remote file exists on the web

0

Taking unutbu's answer as a starting point, and having been burned in the past by a "static" IP address changing, I've made a simple class that checks once using a DNS lookup (i.e., using the URL "https://www.google.com"), and then stores the IP address of the responding server for use on subsequent checks. That way, the IP address is always up to date (assuming the class is re-initialized at least once every few years or so). I also give credit to gawry for this answer, which showed me how to get the server's IP address (after any redirection, etc.). Please disregard the apparent hackiness of this solution, I'm going for a minimal working example here. :)

Here is what I have:

import socket

try:
    from urllib2 import urlopen, URLError
    from urlparse import urlparse
except ImportError:  # Python 3
    from urllib.parse import urlparse
    from urllib.request import urlopen, URLError

class InternetChecker(object):
    conn_url = 'https://www.google.com/'

    def __init__(self):
        pass

    def test_internet(self):
        try:
            data = urlopen(self.conn_url, timeout=5)
        except URLError:
            return False

        try:
            host = data.fp._sock.fp._sock.getpeername()
        except AttributeError:  # Python 3
            host = data.fp.raw._sock.getpeername()

        # Ensure conn_url is an IPv4 address otherwise future queries will fail
        self.conn_url = 'http://' + (host[0] if len(host) == 2 else
                                     socket.gethostbyname(urlparse(data.geturl()).hostname))

        return True

# Usage example
checker = InternetChecker()
checker.test_internet()
0

Taking Six' answer I think we could simplify somehow, an important issue as newcomers are lost in highly technical matters.

Here what I finally will use to wait for my connection (3G, slow) to be established once a day for my PV monitoring.

Works under Pyth3 with Raspbian 3.4.2

from urllib.request import urlopen
from time import sleep
urltotest=http://www.lsdx.eu             # my own web page
nboftrials=0
answer='NO'
while answer=='NO' and nboftrials<10:
    try:
        urlopen(urltotest)
        answer='YES'
    except:
        essai='NO'
        nboftrials+=1
        sleep(30)       

maximum running: 5 minutes if reached I will try in one hour's time but its another bit of script!

1
  • your essai var doesn't get used, does it?
    – kidCoder
    Mar 20, 2017 at 3:04
0

Taking Ivelin's answer and add some extra check as my router delivers its ip address 192.168.0.1 and returns a head if it has no internet connection when querying google.com.

import socket

def haveInternet():
    try:
        # first check if we get the correct IP-Address or just the router's IP-Address
        info = socket.getaddrinfo("www.google.com", None)[0]
        ipAddr = info[4][0]
        if ipAddr == "192.168.0.1" :
            return False
    except:
        return False

    conn = httplib.HTTPConnection("www.google.com", timeout=5)
    try:
        conn.request("HEAD", "/")
        conn.close()
        return True
    except:
        conn.close()
        return False
0

This works for me in Python3.6

import urllib
from urllib.request import urlopen


def is_internet():
    """
    Query internet using python
    :return:
    """
    try:
        urlopen('https://www.google.com', timeout=1)
        return True
    except urllib.error.URLError as Error:
        print(Error)
        return False


if is_internet():
    print("Internet is active")
else:
    print("Internet disconnected")
0

I added a few to Joel's code.

    import socket,time
    mem1 = 0
    while True:
        try:
                host = socket.gethostbyname("www.google.com") #Change to personal choice of site
                s = socket.create_connection((host, 80), 2)
                s.close()
                mem2 = 1
                if (mem2 == mem1):
                    pass #Add commands to be executed on every check
                else:
                    mem1 = mem2
                    print ("Internet is working") #Will be executed on state change

        except Exception as e:
                mem2 = 0
                if (mem2 == mem1):
                    pass
                else:
                    mem1 = mem2
                    print ("Internet is down")
        time.sleep(10) #timeInterval for checking
0

For my projects I use script modified to ping the google public DNS server 8.8.8.8. Using a timeout of 1 second and core python libraries with no external dependencies:

import struct
import socket
import select


def send_one_ping(to='8.8.8.8'):
   ping_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp'))
   checksum = 49410
   header = struct.pack('!BBHHH', 8, 0, checksum, 0x123, 1)
   data = b'BCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwx'
   header = struct.pack(
      '!BBHHH', 8, 0, checksum, 0x123, 1
   )
   packet = header + data
   ping_socket.sendto(packet, (to, 1))
   inputready, _, _ = select.select([ping_socket], [], [], 1.0)
   if inputready == []:
      raise Exception('No internet') ## or return False
   _, address = ping_socket.recvfrom(2048)
   print(address) ## or return True


send_one_ping()

The select timeout value is 1, but can be a floating point number of choice to fail more readily than the 1 second in this example.

0

Make sure your pip is up to date by running

pip install --upgrade pip

Install the requests package using

pip install requests  
    import requests
    import webbrowser
    url = "http://www.youtube.com"

    timeout = 6
    try:
        request = requests.get(url, timeout=timeout)
        print("Connected to the Internet")
        print("browser is loading url")
        webbrowser.open(url)
    except (requests.ConnectionError, requests.Timeout) as exception:
       print("poor or no internet connection.")
0

import requests and try this simple python code.

def check_internet():
    url = 'http://www.google.com/'
    timeout = 5
    try:
        _ = requests.get(url, timeout=timeout)
        return True
    except requests.ConnectionError:
        return False
1
  • This code assumes you are connected to the internet and your DNS is responding.
    – Git.Coach
    Sep 10, 2020 at 22:32
0

I just want to refer to Ivelin's solution, because I can't comment there.

In python 2.7 with an old SSL certificate (in my case, not possible to update, which is another story), there is a possibility of a Certificate Error. In that case, replacing '8.8.8.8' with 'dns.google' or '8888.google' can help.

Hope this will someone helps too.

try:
    import httplib  # python < 3.0
except:
    import http.client as httplib


def have_internet():
    conn = httplib.HTTPSConnection("8888.google", timeout=5)
    try:
        conn.request("HEAD", "/")
        return True
    except Exception:
        return False
    finally:
        conn.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.