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.
Asked
Active
Viewed 164 times
1 Answers
-1
Try to reach a website with request, if ConnectionError raise means there is no internet connection
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