LOG SYSTEM UPDATE
This commit is contained in:
parent
cb036b7cb8
commit
9b8ca51b29
54
Server.py
54
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\\')
|
sys.path.append('libraries\\')
|
||||||
from Astronomy import *
|
from Astronomy import *
|
||||||
#-------------------------------------#
|
#-------------------------------------#
|
||||||
@ -15,6 +15,20 @@ class APICore(object):
|
|||||||
Time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
|
Time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
|
||||||
self.__Log.append((Time,IP,Port,Type,Text))
|
self.__Log.append((Time,IP,Port,Type,Text))
|
||||||
print("["+Type+"]["+Time+"]["+IP+"]["+str(Port)+"]"+str(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):
|
def IsJson(self,text):
|
||||||
try:
|
try:
|
||||||
json.loads(text)
|
json.loads(text)
|
||||||
@ -37,6 +51,11 @@ RequestRecord={}
|
|||||||
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
server.bind(("0.0.0.0",8000))
|
server.bind(("0.0.0.0",8000))
|
||||||
server.listen()
|
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):
|
def socket(sock,addr):
|
||||||
while True:
|
while True:
|
||||||
try:
|
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","Wrong code mode,must be utf-8.")
|
||||||
API.Log(addr[0],addr[1],"STATUS","Lost connection.")
|
API.Log(addr[0],addr[1],"STATUS","Lost connection.")
|
||||||
break
|
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():
|
def main():
|
||||||
sock,addr=server.accept()
|
LogOutput=API.AutoLogOutput
|
||||||
client_thread=threading.Thread(target=socket,args=(sock,addr))
|
log_thread=threading.Thread(target=LogOutput,args=())
|
||||||
client_thread.start()
|
log_thread.start()
|
||||||
while True:
|
listen_thread=threading.Thread(target=listen,args=())
|
||||||
main()
|
listen_thread.start()
|
||||||
|
while(True):
|
||||||
|
ManagerCommand=input("")
|
||||||
|
if(ManagerCommand=="exit"):
|
||||||
|
API.LogOutput()
|
||||||
|
os.kill(os.getpid(),signal.SIGTERM)
|
||||||
|
elif(ManagerCommand=="savelog"):
|
||||||
|
API.LogOutput()
|
||||||
|
main()
|
BIN
libraries/__pycache__/Astronomy.cpython-36.pyc
Normal file
BIN
libraries/__pycache__/Astronomy.cpython-36.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user