Python script to check network connection
I need one script to check my network, because it usually down. So code little python script :)
import winsound
import urllib2
import time
def internet_on():
try:
response=urllib2.urlopen('http://74.125.113.99',timeout=1)
return True
except urllib2.URLError as err: pass
return False
while 1:
if internet_on() == 1:
print "Network on"
else:
print "Network off"
winsound.Beep(500,500)
time.sleep(5)
pass
------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.
import winsound
import urllib2
import time
def internet_on():
try:
response=urllib2.urlopen('http://74.125.113.99',timeout=1)
return True
except urllib2.URLError as err: pass
return False
while 1:
if internet_on() == 1:
print "Network on"
else:
print "Network off"
winsound.Beep(500,500)
time.sleep(5)
pass
------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.
Comments
#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 = "http://www.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.