Advertisement
steve-shambles-2109

193-Check For Internet Connection

Oct 31st, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. """
  2. Python code snippets vol 39:
  3. stevepython.wordpress.com
  4.  
  5. 193-Check For Internet Connection
  6. requirements: None
  7.  
  8. https://gist.github.com/jarvisrob/dbf6a6ad098102c0a290a567625b9634
  9. """
  10. import socket
  11.  
  12. def check_internet(host="8.8.8.8", port=53, timeout=3):
  13.     # Checks for internet connection by testing to see if it can
  14.     # make a socket connetion to (host, port)
  15.     # Host: 8.8.8.8 (google-public-dns-a.google.com)
  16.     # Port: 53/tcp
  17.     try:
  18.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.         sock.settimeout(timeout)
  20.         sock.connect((host, port))
  21.         return True
  22.     except:
  23.         return False
  24. print(check_internet())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement