summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/20-07-2011/classClient.py
diff options
context:
space:
mode:
authortriatmoko2011-07-18 18:39:09 +0200
committertriatmoko2011-07-18 18:39:09 +0200
commit5e6ff41619b70b9a3c7a1eaeb989c295e22943f1 (patch)
tree364cedfa81e7044b53ce8d9e643377930fadf2d3 /For Weekly Test/20-07-2011/classClient.py
parenttesting purpose. (diff)
downloadgsm-selftest-5e6ff41619b70b9a3c7a1eaeb989c295e22943f1.tar.gz
gsm-selftest-5e6ff41619b70b9a3c7a1eaeb989c295e22943f1.tar.xz
gsm-selftest-5e6ff41619b70b9a3c7a1eaeb989c295e22943f1.zip
modified for test weekly
Diffstat (limited to 'For Weekly Test/20-07-2011/classClient.py')
-rw-r--r--For Weekly Test/20-07-2011/classClient.py97
1 files changed, 77 insertions, 20 deletions
diff --git a/For Weekly Test/20-07-2011/classClient.py b/For Weekly Test/20-07-2011/classClient.py
index ce02d19..46c3b00 100644
--- a/For Weekly Test/20-07-2011/classClient.py
+++ b/For Weekly Test/20-07-2011/classClient.py
@@ -2,6 +2,10 @@ import socket
import sys
import os
import string
+import signal
+
+class TimeoutException(Exception):
+ pass
class Connection:
def __init__(self, h, p):
@@ -10,12 +14,11 @@ class Connection:
self.s = None
self.connected = 0
+ self.debugMode = 0
+
def connect(self):
self.s = None
- alive = self.ping()
- if alive == 0:
- return 'The machine is not alive'
for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
@@ -35,32 +38,86 @@ class Connection:
break
if self.s is None:
self.connected = 0
- return 'Could not open socket'
+ return 0 #couldn't connect to the server
else:
self.connected = 1
- return 'Connected'
+ return 1 #successfully connected to the server
def sendData(self, data):
if self.connected == 1:
- self.s.send(data)
+ try:
+ self.s.send(data)
+ return 1
+ except Exception, e:
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ self.connected = 0
+ return 2
+
+ else:
+ return 0
- def receiveData(self):
+ def receiveData(self, timeout):
if self.connected == 1:
- return self.s.recv(1024)
+
+ def timeout_handler(signum, frame):
+ raise TimeoutException()
+
+ old_handler = signal.signal(signal.SIGALRM, timeout_handler)
+ signal.alarm(timeout) #start the timeout alarm, for timeout seconds
+ try:
+ data = self.s.recv(1024)
+
+ #stop the timeout function
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
+ return data
+
+
+ except TimeoutException:
+ #timeout happened
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'TIMEOUT'
+
+ except Exception, e:
+
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
+ error = str(e)
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ self.connected = 0
+ if error[0:11] == '[Errno 104]':
+ return 3 #the other side reset the connection,[Errno 104] Connection reset by peer
+
+ return 2
else:
- return 'Not connected'
+ return 0
def closeConnection(self):
if self.connected == 1:
- self.s.close()
- self.connected = 0
- return 'Closed'
-
- def ping(self):
- ping_cmd = os.popen('ping '+ self.host + ' -c 1 -W 1').read()
- pingAlive = int(string.find(ping_cmd, '1 received'))
- if pingAlive != -1:
- return 1
- else:
+ try:
+ self.connected = 0
+ SHUT_RDWR = 2
+ self.s.shutdown(SHUT_RDWR)
+ self.s.close()
+ return 1
+ except Exception, e:
+ self.connected = 0
+ error = str(e)
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ if error[0:11] == '[Errno 107]':
+ return 3 #the other side closed the connection before us, [Errno 107] Transport endpoint is not connected
+ return 2
+ else:
return 0
-