summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Server-Code-New/WebsiteCommClass.py
diff options
context:
space:
mode:
Diffstat (limited to 'Under-Testing/Server-Code-New/WebsiteCommClass.py')
-rw-r--r--Under-Testing/Server-Code-New/WebsiteCommClass.py162
1 files changed, 0 insertions, 162 deletions
diff --git a/Under-Testing/Server-Code-New/WebsiteCommClass.py b/Under-Testing/Server-Code-New/WebsiteCommClass.py
deleted file mode 100644
index 206411e..0000000
--- a/Under-Testing/Server-Code-New/WebsiteCommClass.py
+++ /dev/null
@@ -1,162 +0,0 @@
-import socket
-import sys
-import os
-import string
-import signal
-
-class TimeoutException(Exception):
- pass
-
-class ServerHandlerSoftware:
-
- def __init__(self,p):
- self.port = p
- self.host = None #symbolic name meaning all available interfaces
- self.s = None
- self.connected = 0
- self.address = "127.0.0.1" #address of the main controller
- self.onceConnected = 0
- self.error = 'No error'
-
- self.debugMode = 0
-
- def openSocket(self, timeoutVar):
- self.error = 'No error'
- for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC,
- socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
- af, socktype, proto, canonname, sa = res
-
-
- try:
-
- self.s = socket.socket(af, socktype, proto)
- self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #this resolves the bug with live packets
- self.s.settimeout(timeoutVar)
-
- except socket.error, msg:
- self.s = None
- self.connected = 0
- self.error = str(msg)
- continue
-
- try:
- self.s.bind(sa)
- self.s.listen(1)
-
- except socket.error, msg:
- self.s.close()
- self.s = None
- self.connected = 0
- self.error = str(msg)
- continue
-
- break
-
- if self.s is None:
- self.connected = 0
- return 0
- else: #accept the connection
- try:
- self.connection, self.address = self.s.accept()
- self.s.settimeout(0)
- self.connected = 1
- self.onceConnected = 1
- return 1
- except socket.timeout:
- return 'TIMEOUT'
-
- def connectedTo(self):
- return self.address
-
- def receiveData(self, timeout):
- if self.connected == 1:
-
- def timeout_handler(signum, frame):
- raise TimeoutException()
-
- try:
-
- old_handler = signal.signal(signal.SIGALRM, timeout_handler)
- signal.alarm(timeout) #start the timeout alarm, for timeout seconds
-
- data = self.connection.recv(1024)
-
- #stop the timeout function
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
-
- if not data:
- self.connected = 0
- return 'NO DATA'
- else:
- return data
-
- except TimeoutException:
- #timeout happened
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'TIMEOUT'
-
- except Exception, e:
- #stop the timeout timer
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
-
- 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 0
-
- def sendData(self, data):
- if self.connected == 1:
- try:
- self.connection.send(data)
- return 1
-
- except Exception, e:
- if self.debugMode == 1:
- import traceback
- print traceback.format_exc()
- print e
- self.connecected = 0
- return 2
- else:
- return 0
-
- def closeConnection(self):
- if self.onceConnected == 1:
- try:
- self.connected = 0
- SHUT_RDWR = 2
- self.connection.shutdown(SHUT_RDWR)
- self.connection.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
-
- def killPort(self):
- killResult = os.popen('lsof -i tcp:' + str(self.port) + ' | grep "python " | awk -F" " ' + "'{print $2}'").read()
- killResult = killResult.replace('\n','')
- print killResult
- if killResult!='':
- print killResult
- killPort = os.popen("kill -9 " + killResult).read()
- return 1
- return 0