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 0000000..01ce12f Binary files /dev/null and b/libraries/__pycache__/Astronomy.cpython-36.pyc differ