Created
March 3, 2022 20:28
-
-
Save ZamanOof/9bbea68a0da83d6c3e16cf0f23776d39 to your computer and use it in GitHub Desktop.
to check connection
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
# check internet connection | |
# @hostname str domain name | |
# todo initial function | |
#startup | |
def is_connected(hostname='w.wiki'): | |
logger.info(f'"trying to check connection"') | |
while True: | |
try: | |
# see if we can resolve the host name -- tells us if there is | |
# a DNS listening | |
host = socket.gethostbyname(hostname) | |
# connect to the host -- tells us if the host is actually | |
# reachable | |
s = socket.create_connection((host, 80), 2) | |
s.close() | |
return True | |
logger.info(f'"There is Net"') | |
# config["isnet"]=True | |
except Exception as e: | |
logger.info(f'"There is no Net,will try after 60S {e}"') | |
time.sleep(60) | |
# config["isnet"] = False | |
pass | |
# def is_connected(host='9.9.9.9'): | |
# try: | |
# # connect to the host -- tells us if the host is actually | |
# # reachable | |
# socket.create_connection((host, 53)) | |
# return True | |
# except OSError: | |
# pass | |
# return False | |
# https://stackoverflow.com/a/60468117/4870357 | |
# import requests | |
# while True: | |
# try: | |
# requests.get('https://www.google.com/').status_code | |
# break | |
# except: | |
# time.sleep(5) | |
# pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment