Python to Check Internet Connection with Source Code

A small python script to check internet connectivity.


  • Python3

Run the Script:

Code language: CSS (css)

Source Code:

import requests from requests.exceptions import ConnectionError def internet_connection_test(): url = '' print(f'Attempting to connect to {url} to determine internet connection status.') try: print(url) resp = requests.get(url, timeout = 10) resp.text resp.status_code print(f'Connection to {url} was successful.') return True except ConnectionError as e: requests.ConnectionError print(f'Failed to connect to {url}.') return False except: print(f'Failed with unparsed reason.') return False internet_connection_test()
Code language: PHP (php)


Leave a Comment