From 9b8ca51b29eb2c9552f89bf1cb735e4ac9f648b4 Mon Sep 17 00:00:00 2001 From: Bigsk Date: Sun, 27 Dec 2020 15:42:19 +0800 Subject: [PATCH] LOG SYSTEM UPDATE --- Server.py | 54 ++++++++++++++++-- .../__pycache__/Astronomy.cpython-36.pyc | Bin 0 -> 7984 bytes 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 libraries/__pycache__/Astronomy.cpython-36.pyc diff --git a/Server.py b/Server.py index 16bdea9..fe15728 100644 --- a/Server.py +++ b/Server.py @@ -1,4 +1,4 @@ -import socket,threading,os,time,math,json,sys,urllib,requests +import socket,threading,os,time,math,json,sys,urllib,requests,ctypes,inspect,signal sys.path.append('libraries\\') from Astronomy import * #-------------------------------------# @@ -15,6 +15,20 @@ class APICore(object): Time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) self.__Log.append((Time,IP,Port,Type,Text)) print("["+Type+"]["+Time+"]["+IP+"]["+str(Port)+"]"+str(Text)) + def MakeDir(self,Path): + if(not os.path.isdir(Path)): + os.makedirs(Path) + def LogOutput(self): + self.MakeDir("logs") + with open(time.strftime("logs\\%Y-%m-%d-%H-%M-%S.txt",time.localtime()),"w+") as FileObject: + FileObject.write("GHINK API Logs "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"\n") + for text in self.__Log: + FileObject.write("["+str(text[0])+"]["+str(text[1])+"]["+str(text[2])+"]["+str(text[3])+"]"+str(text[4])+"\n") + self.__Log=[] + def AutoLogOutput(self): + while(True): + time.sleep(3600) + self.LogOutput() def IsJson(self,text): try: json.loads(text) @@ -37,6 +51,11 @@ RequestRecord={} server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.bind(("0.0.0.0",8000)) server.listen() +def listen(): + while True: + sock,addr=server.accept() + client_thread=threading.Thread(target=socket,args=(sock,addr)) + client_thread.start() def socket(sock,addr): while True: try: @@ -164,9 +183,32 @@ def socket(sock,addr): API.Log(addr[0],addr[1],"STATUS","Wrong code mode,must be utf-8.") API.Log(addr[0],addr[1],"STATUS","Lost connection.") break +def _async_raise(tid, exctype): + """raises the exception, performs cleanup if needed""" + tid = ctypes.c_long(tid) + if not inspect.isclass(exctype): + exctype = type(exctype) + res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) + if res == 0: + raise ValueError("invalid thread id") + elif res != 1: + # """if it returns a number greater than one, you're in trouble, + # and you should call it again with exc=NULL to revert the effect""" + ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) + raise SystemError("PyThreadState_SetAsyncExc failed") +def stop_thread(thread): + _async_raise(thread.ident, SystemExit) def main(): - sock,addr=server.accept() - client_thread=threading.Thread(target=socket,args=(sock,addr)) - client_thread.start() -while True: - main() + LogOutput=API.AutoLogOutput + log_thread=threading.Thread(target=LogOutput,args=()) + log_thread.start() + listen_thread=threading.Thread(target=listen,args=()) + listen_thread.start() + while(True): + ManagerCommand=input("") + if(ManagerCommand=="exit"): + API.LogOutput() + os.kill(os.getpid(),signal.SIGTERM) + elif(ManagerCommand=="savelog"): + API.LogOutput() +main() \ No newline at end of file diff --git a/libraries/__pycache__/Astronomy.cpython-36.pyc b/libraries/__pycache__/Astronomy.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..01ce12fde3e5475af3a59463d274622593591547 GIT binary patch literal 7984 zcmeHMYiu0V6`t3=A9n1G?RXvHBzEEjllQ|U!Ml#hLnw9uLm^wJnfOk|o2+M(yX%0h z*~--Pp+c*)3ZxWDBUB;L2Wl&-R;YrgswyGT{ync_n!MYbMM`IzVCQ*A`$I>^Ofr3N=!A>c{Q&wc}&V^HS~~1CrJ8F0GS+8`AP^wq7m{6i!+sJ_sV&dC0LM! zkkeR_MOh5FfEcUEi42u1ykwQ8W=3mabe>Ql=!qjOK^jIXe9j(O#{_fB~Fq*re8%FS-sj8|0~h96b-tH>xTYzd@l zYxDBFGA~yHp0=ey8}ziT4cd^WO*Lr4o_1-2HsWd98njV6z}nj+)^SuhDVN$%S|&>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