-3

I was wondering how I would check if a users internet connection is on/off in python, if its off I want the program to close. I would prefer to do this by pinging and then looping over 25 minutes.

Leon
  • 1
  • 2
  • 1
    https://stackoverflow.com/questions/3764291/how-can-i-see-if-theres-an-available-and-active-network-connection-in-python – dvr Apr 12 '22 at 21:02

1 Answers1

-1

Try to reach a website with request, if ConnectionError raise means there is no internet connection

Source

import requests

url = "https://www.google.com/"
timeout = 5
try:
    request = requests.get(url, timeout=timeout)
    print("Connected to the Internet")
except (requests.ConnectionError, requests.Timeout) as exception:
    print("No internet connection.")
Sau1707
  • 69
  • 6