Python script to check network connection

ThaNK YOU !!1 kendy..

[Unknown](blogger.com/profile/10936135565085527199 "") - Jan 5, 2014

ThaNK YOU !!1 kendy..


Thanks MAn!

Anonymous - Dec 1, 2015

Thanks MAn!


#### [Unknown](blogger.com/profile/10065115800713466887 "") - Mar 5, 2016

This comment has been removed by the author.


#!/usr/bin/env python3 #Credits due to Kendy Hikar...

gricks - Mar 5, 2016

#!/usr/bin/env python3

#Credits due to Kendy Hikaru. Adapted from example by Kendy Hikaru dated 18 July 2012.

#import urllib2 #python2
import urllib.request, urllib.parse, urllib.error #python3
import time
REMOTE_SERVER = "google.com"

def is_connected():
^ """Return True if connected to internet else False"""
^ try:
^^ response=urllib.request.urlopen(REMOTE_SERVER,timeout=1)
^^ return True
^ except urllib.error.URLError as e: pass
^ return False

print((str(is_connected())))

"""Testing
$ python3 temp2.py
True
(Disconnect internet)
$ python3 temp2.py
False
(Re-connect internet)
$ python3 temp2.py
True
"""
PS The blog helpfully removes indentation therefore globally replace ^ with e.g. two spaces.