Created
February 2, 2020 13:54
-
-
Save FloPinguin/8224acfb1e49a4346448f038b9e2afe4 to your computer and use it in GitHub Desktop.
Wait for internet connection in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
def is_connected(): | |
try: | |
socket.create_connection(("www.google.com", 80)) | |
return True | |
except OSError: | |
pass | |
return False | |
while True: | |
print("Checking connection") | |
if is_connected(): | |
print ("Connected!") | |
raise SystemExit | |
else: | |
print("No connection - Waiting 15s") | |
time.sleep(15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment