From 3467a480702f7cf64116f5141dd9ce588b009b05 Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Thu, 25 Jan 2024 10:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E9=99=A4s2f?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.txt | 2 ++ restart.sh | 4 ++-- run_tt_server.sh | 6 +++--- s2f.py | 19 ------------------- server.py | 40 +++++++++++++++++++++++++++++++++------- 5 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 readme.txt delete mode 100644 s2f.py diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..99d2885 --- /dev/null +++ b/readme.txt @@ -0,0 +1,2 @@ +批量删除命令 +ps aux | grep './restart.sh' | grep -v grep | awk '{print $2}' | xargs kill -9 \ No newline at end of file diff --git a/restart.sh b/restart.sh index 786d1fa..a43d20c 100755 --- a/restart.sh +++ b/restart.sh @@ -3,13 +3,13 @@ while true; do monitor=$(ps -ef | grep "./server.py" | grep -v grep | wc -l) - cur_dateTime=$(date +%Y-%m-%d_%H-%M-%s) + # cur_dateTime=$(date +%Y-%m-%d_%H-%M-%s) if [ $monitor -eq 0 ]; then echo $cur_dateTime" --- ./server.py program is not running, restart now" # cd ~/tt/tt-tcp #if= "./log-"$cur_dateTime".txt" # f="./log-"${cur_dateTime}".txt" - mv "./rxlog.txt" "rxlog/rxlog-"${cur_dateTime}".txt" + # mv "./rxlog.txt" "rxlog/rxlog-"${cur_dateTime}".txt" #cd /root/tt/tt-tcp #mv rxlog.txt "./rxlog-"$cur_dateTime".txt" # nohup python3 -u ./server.py >>$f 2>&1 & diff --git a/run_tt_server.sh b/run_tt_server.sh index 18d3df9..4b7fa29 100755 --- a/run_tt_server.sh +++ b/run_tt_server.sh @@ -13,13 +13,13 @@ kill_with_pid() { kill_with_pid echo "Now run new service." # -cur_dateTime=$(date +%Y-%m-%d_%H-%M) +# cur_dateTime=$(date +%Y-%m-%d_%H-%M) #cur_dateTime=$(date +%Y-%m-%d_%H-%m-%s) -echo ${cur_dateTime} +# echo ${cur_dateTime} # cd ~/tt-tcp #mv log.txt "./log-"${cur_dateTime}".txt" # f="./log-"${cur_dateTime}".txt" -mv rxlog.txt "rxlog/rxlog-"${cur_dateTime}".txt" +# mv rxlog.txt "rxlog/rxlog-"${cur_dateTime}".txt" nohup python3 -u ./server.py & #nohup ./restart.sh & nohup ./restart.sh >> reboot.log & diff --git a/s2f.py b/s2f.py deleted file mode 100644 index b4eaece..0000000 --- a/s2f.py +++ /dev/null @@ -1,19 +0,0 @@ -import datetime -import os -def gettm(): - t= datetime.datetime.now() - d = str(t) - return d,d[:10] - -def save2file(data,isR=1): - # pass - t,d = gettm() - - p = '/'.join(['trlog',d]) - if not os.path.exists(p): - os.makedirs(p) - - l=f"{'[R]' if isR else '[T]'} - {t} --- {data}\n" - f=open('/'.join(['trlog',d,'trlog.txt']),'a') - f.write(l) - f.close \ No newline at end of file diff --git a/server.py b/server.py index 535a367..d09cca0 100755 --- a/server.py +++ b/server.py @@ -10,10 +10,36 @@ import requests import socket # import json from threading import Timer -import s2f from loguru import logger import os + + +def gettm(): + """ + 返回时戳和日期 + """ + t= datetime.datetime.now() + d = str(t) + return d,d[:10] + +def save2file(data,isR=1): + """ + 存储数据至文件 + """ + t,d = gettm() + + p = '/'.join(['trlog',d]) + if not os.path.exists(p): + os.makedirs(p) + + l=f"{'[R]' if isR else '[T]'} - {t} --- {data}\n" + f=open('/'.join(['trlog',d,'trlog.txt']),'a') + f.write(l) + f.close + + + SERVER = "http://www.pushplus.plus/send" TOKEN = "ff328cba923a4225bc4acd0086a9014c" # SERVER = "http://push.nmhd.eu.org:12722/push/murmur" @@ -273,7 +299,7 @@ def tt_hh(addr, data): # 组帧再返回 msg = bytearray(data[:]) msg[:5] = b"$TTMX" - val = "%04x" % int(time.time()) + val = "%04x" % int(time.time()) #取Unix时间戳,以s为单位 tmp=[] for i in range(0,len(val),2): tmp.append(int(val[i:i+2],16)) @@ -285,7 +311,7 @@ def tt_hh(addr, data): msg[-1] = crc add_timestamp() addr.send(msg) - s2f.save2file(f"[SERVER --> {get_ccid(addr).decode() if get_ccid(addr) else 'None'}][{len(msg)}] {msg.hex(' ')}",0) + save2file(f"[SERVER --> {get_ccid(addr).decode() if get_ccid(addr) else 'None'}][{len(msg)}] {msg.hex(' ')}",0) logger.debug("服务器回复心跳包。") # 从缓存中匹配到数据才重发 @@ -300,7 +326,7 @@ def tt_hh(addr, data): add_timestamp() # 按缓存先后顺序发送 addr.send(msgs[i]) - s2f.save2file(f"[SERVER --> {get_ccid(addr).decode() if get_ccid(addr) else 'None'}][{len(msg)}] {msg.hex(' ')}") + save2file(f"[SERVER --> {get_ccid(addr).decode() if get_ccid(addr) else 'None'}][{len(msg)}] {msg.hex(' ')}") time.sleep(1) # 成功则清空已发送成功的缓存数据 cache[ccid].pop(0) @@ -378,7 +404,7 @@ def tt_trans(addr, data): try: add_timestamp() taddr.send(msg) - s2f.save2file(f"[SERVER --> {get_ccid(taddr).decode() if get_ccid(taddr) else 'None'}][{len(msg)}] {msg.hex(' ')}",0) + save2file(f"[SERVER --> {get_ccid(taddr).decode() if get_ccid(taddr) else 'None'}][{len(msg)}] {msg.hex(' ')}",0) logger.info(f"数据发送成功。 {sccid} --> {tccid}") s2wx("","",f"匹配到在线终端, 数据发送成功。 \n{sccid.decode()} ---> {tccid.decode()}") except Exception: @@ -512,7 +538,7 @@ class MyServer(socketserver.BaseRequestHandler): add_timestamp() self.recDataBuff.extend(data) # logger.debug("[R] {}",data.hex(" ")) - s2f.save2file(f"[{get_ccid(conn).decode() if get_ccid(conn) else 'None'} --> SERVER][{len(data)}] {data.hex(' ')}") + save2file(f"[{get_ccid(conn).decode() if get_ccid(conn) else 'None'} --> SERVER][{len(data)}] {data.hex(' ')}") t = f"从{str(self.client_address)}<->[{get_ccid(conn).decode() if get_ccid(conn) else 'None'}]接收到新数据,长度{len(data)}字节。" logger.info(t) logger.debug("[R] - {}",data.hex(" ")) @@ -548,7 +574,7 @@ CCID:{pp[self.request].decode()} if __name__ == "__main__": - _,t = s2f.gettm() + _,t = gettm() if not os.path.exists('debug'): os.mkdir('debug') logger.add('/'.join(['debug',t,'/log_{time}.log']))