#!/usr/bin/python
import socket,time
testdns = 'google.com'
print "Testing connection with",testdns
while(True):
try:
connected = True
socket.gethostbyname(testdns)
except socket.gaierror:
connected = False
if(not connected):
print "Not Connected..."
print "\a", ; time.sleep(.1); print "\a",
else:
print "Connected..."
print "\a",
time.sleep(2)
Refactorings
No refactoring yet !
macunixgeek
October 2, 2008, October 02, 2008 02:38, permalink
from socket import gaierror, gethostbyname
from time import sleep
def testConnection(testDNS):
try:
gethostbyname(testDNS)
except gaierror:
return False
return True
while(True):
if(testConnection('google.com')):
print "Connected\a"
else:
print "Not Connected\a", ; sleep(.1); print "\a"
sleep(2)
Mohit Ranka
November 2, 2008, November 02, 2008 20:35, permalink
from socket import gethostbyname
from time import sleep
def testConnection(testDNS):
try:
gethostbyname(testDNS)
return True
except:
return False
while(True):
if(testConnection('google.com')):
print "Connected\a"
else:
print "Not Connected\a", ; sleep(.1); print "\a"
sleep(2)
nosklo
January 20, 2009, January 20, 2009 14:41, permalink
You don't need all those parenthesis.
from socket import gethostbyname
from time import sleep
import sys
def beep(times=1):
for time in xrange(times):
sys.stdout.write('\a')
sys.stdout.flush()
sleep(.1)
def test_connection(test_host='google.com'):
try:
gethostbyname(test_host)
return True
except socket.gaierror:
return False
while True:
if test_connection():
print "Connected"
beep()
else:
print "Not Connected"
beep(2)
sleep(2)
This is a Quick and Dirty script that beeps once when it is connected to the internet and twice when it fails. The idea is to let me know if the computer is still connected while I am bent over backward, under the desk across the room, screwing with the cables.