summaryrefslogtreecommitdiffstats
path: root/Code/Server-Code/PingClass.py
diff options
context:
space:
mode:
authorgsmselftest2011-11-09 16:41:00 +0100
committergsmselftest2011-11-09 16:41:00 +0100
commit1c009430af80260d67c47652783931414989c75d (patch)
tree204540af3ff3d8e23892986ad7585080900b7463 /Code/Server-Code/PingClass.py
parentReport relocating! (diff)
downloadgsm-selftest-1c009430af80260d67c47652783931414989c75d.tar.gz
gsm-selftest-1c009430af80260d67c47652783931414989c75d.tar.xz
gsm-selftest-1c009430af80260d67c47652783931414989c75d.zip
Final folder team project
Diffstat (limited to 'Code/Server-Code/PingClass.py')
-rwxr-xr-xCode/Server-Code/PingClass.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Code/Server-Code/PingClass.py b/Code/Server-Code/PingClass.py
new file mode 100755
index 0000000..e13b32b
--- /dev/null
+++ b/Code/Server-Code/PingClass.py
@@ -0,0 +1,28 @@
+import subprocess
+import string
+
+class Ping:
+
+ def __init__(self, pingAddress):
+ self.pingAddress = pingAddress
+
+ def ping(self,numberTries):
+ tried = 1
+ while numberTries >= tried:
+ tried += 1
+ #the parameter c 1 means only one ping to be sent, parameter W 3 means how many seconds the time out should be, 3 seconds
+ ping_cmd = subprocess.Popen(['ping', self.pingAddress, '-c', '1', '-W', '2'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]
+
+ pingAlive = int(string.find(ping_cmd, '1 received'))
+ unknownHost = int(string.find(ping_cmd, 'unknown host'))
+
+
+ if pingAlive != -1:
+ break
+
+ if unknownHost != -1:
+ return 2 #unknown host
+ if pingAlive != -1:
+ return 1 #ping works fine
+ else:
+ return 0 #no ping response