2021-04-09 13:16:29 +00:00
|
|
|
global textTimeout
|
|
|
|
textTimeout = 30
|
|
|
|
global textKeepAliveTimeout
|
|
|
|
textKeepAliveTimeout = 600 # set to 0 for infinite time (not recommended)
|
|
|
|
|
|
|
|
global textOnConnect
|
|
|
|
def textOnConnect(event,connection,address):
|
|
|
|
global textKeepAliveTimeout
|
|
|
|
connection.settimeout(textKeepAliveTimeout)
|
|
|
|
addEventHandler("onConnect",textOnConnect)
|
|
|
|
|
|
|
|
global textOnPreRequest
|
|
|
|
def textOnPreRequest(event,self,requestLength):
|
|
|
|
global textTimeout
|
|
|
|
self.connection.settimeout(textTimeout)
|
|
|
|
if requestLength <= 128: return
|
|
|
|
try:
|
|
|
|
sendResponse(self.connection,textListToCommand(["error","fatal","request_too_long"]).encode("utf-8"))
|
|
|
|
except threading.timeout:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.closeThread()
|
|
|
|
return True
|
|
|
|
addEventHandler("onPreRequest",textOnPreRequest)
|
|
|
|
|
|
|
|
global textOnRequest
|
|
|
|
def textOnRequest(event,self,requestLength):
|
|
|
|
global textTimeout
|
|
|
|
global textKeepAliveTimeout
|
|
|
|
|
|
|
|
self.connection.settimeout(textTimeout)
|
|
|
|
data = self.connection.recv(requestLength)
|
|
|
|
|
|
|
|
text = data.decode("utf-8")
|
|
|
|
print(":".join(map(str,self.address))+ " > " +text)
|
|
|
|
if text == "close":
|
2021-04-09 14:45:32 +00:00
|
|
|
with threadsLock:
|
|
|
|
global close
|
|
|
|
close = True
|
2021-04-09 13:16:29 +00:00
|
|
|
|
|
|
|
self.closeThread()
|
|
|
|
return True
|
|
|
|
|
|
|
|
response = textCommandRun(self,textCommandToList(text))
|
|
|
|
print("response: " +textListToCommand(response))
|
|
|
|
sendResponse(self.connection,textListToCommand(response).encode("utf-8"))
|
|
|
|
|
|
|
|
self.connection.settimeout(textKeepAliveTimeout)
|
|
|
|
addEventHandler("onRequest",textOnRequest)
|
|
|
|
|
|
|
|
global textOnException
|
|
|
|
def textOnException(event,self,exc):
|
|
|
|
self.connection.settimeout(textTimeout)
|
|
|
|
if type(exc) == socket.timeout:
|
|
|
|
sendResponse(self.connection,textListToCommand(["error","fatal","timeout"]).encode("utf-8"))
|
|
|
|
return
|
|
|
|
|
|
|
|
sendResponse(self.connection,textListToCommand(["error","fatal","unhandled",str(exc)]).encode("utf-8"))
|
|
|
|
addEventHandler("onException",textOnException)
|