summaryrefslogtreecommitdiffstats
path: root/Code/Server-Code/PingClass.py
blob: e13b32b85b7be5701ec3024f0f4e3e2aec6e1759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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