Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active February 4, 2019 22:05
Show Gist options
  • Save spookyahell/b11585ba814b05f27d6aba4f662a57b7 to your computer and use it in GitHub Desktop.
Save spookyahell/b11585ba814b05f27d6aba4f662a57b7 to your computer and use it in GitHub Desktop.
Check the internet connection reliably... firefox is faster due to transferring less data and as we know is also more safe... Google is maybe even more reliable (never seen detectportal down though either)
import requests
def CheckNet(source = 'F'):
sources = {'G':'https://www.google.com','F':'http://detectportal.firefox.com/success.txt'}
find_in_text = {'G':'maxlength="2048" name="q"','F':'success'}
if source in sources:
src = sources[source]
else:
raise NotImplementedError('Source unavailable')
try:
r = requests.get(src, timeout = 10)
if find_in_text[source] in r.text:
return True
else:
return False
except:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment