4
The idea is to build a small function to check if there is an internet connection. I have a draft of a function that uses the library socket
, but I do not believe that this is the most efficient way or one that best respects good programming practice.
from socket import gethostbyname,create_connection
# Verifica se esta conectado a internet
def conectadoInternet():
tentativas = 0
servidorRemoto = 'www.google.com'
while tentativas < 3:
if tentativas == 1:
servidorRemoto = 'www.lds.org'
elif tentativas == 2:
servidorRemoto = 'www.msn.com'
try:
host = gethostbyname(servidorRemoto)
s = create_connection((host, 80), 2)
return True
except: tentativas += 1
return False
What would be other better ways?