Avatar

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/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 !

027f0a74664a1705494ebb2bd02809d5

macunixgeek

October 2, 2008, October 02, 2008 02:38, permalink

1 rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
4eceaeb8bad76db25745becc2dc30406

Mohit Ranka

November 2, 2008, November 02, 2008 20:35, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)

Your refactoring





Format Copy from initial code

or Cancel