Skip to content

Instantly share code, notes, and snippets.

@7h3rAm
Last active April 26, 2021 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 7h3rAm/a4c3de8e502f755a7253 to your computer and use it in GitHub Desktop.
Save 7h3rAm/a4c3de8e502f755a7253 to your computer and use it in GitHub Desktop.
Quick Internet Connectivity Test: Invokes a connect on Google's public DNS server: 8.8.8.8:53/TCP with a socket timeout of 1 second.
import socket
def internet(host="8.8.8.8", port=53, timeout=3):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
print ex.message
return False
@7h3rAm
Copy link
Author

7h3rAm commented Oct 14, 2015

@acrolink
Copy link

acrolink commented Jan 1, 2020

Won't checking internet connection by accessing port 53 on 8.8.8.8 every 5 seconds lead possibly to getting client's IP blocked by Google (get marked as abuse)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment