59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
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":
|
|
with threadsLock:
|
|
global close
|
|
close = True
|
|
|
|
self.closeThread()
|
|
return True
|
|
|
|
response = textCommandRun(self,textCommandToList(text))
|
|
print("response: " +textListToCommand(response))
|
|
self.sendResponse(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:
|
|
self.sendResponse(textListToCommand(["error","fatal","timeout"]).encode("utf-8"))
|
|
return
|
|
|
|
self.sendResponse(textListToCommand(["error","fatal","unhandled",str(exc)]).encode("utf-8"))
|
|
addEventHandler("onException",textOnException) |