[improvement] internet availability checking code

This commit is contained in:
Vinícius Moreira
2020-04-20 10:29:23 -03:00
parent 3e73797090
commit efd4179b7e
2 changed files with 10 additions and 8 deletions

View File

@@ -1,13 +1,12 @@
import subprocess
import traceback
from subprocess import Popen
import http.client as http_client
def is_available() -> bool:
conn = http_client.HTTPConnection("www.google.com", timeout=5)
try:
res = Popen(['ping', '-q', '-w1', '-c1', 'google.com'], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
res.wait()
return res.returncode == 0
conn.request("HEAD", "/")
conn.close()
return True
except:
traceback.print_exc()
conn.close()
return False