增加掉线判别

This commit is contained in:
murmur 2023-03-19 10:27:02 +08:00
parent 825e32fec3
commit 76b9167423

View File

@ -34,7 +34,7 @@ def s2wx(title,description, content):
"""通过自建msgpusher发送至微信 """通过自建msgpusher发送至微信
当前title值无效 当前title值无效
""" """
# pass
# GET 方式 # GET 方式
# res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}" # res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}"
# f"&description={description}&content={content}&token={TOKEN}") # f"&description={description}&content={content}&token={TOKEN}")
@ -43,7 +43,10 @@ def s2wx(title,description, content):
res = requests.post("http://push.nmhd.eu.org:12722/push/murmur", json={ res = requests.post("http://push.nmhd.eu.org:12722/push/murmur", json={
"title": title, "title": title,
"description": description, "description": description,
"content": content, "content": f"""
### 时间戳:
{datetime.datetime.now()}
{content}""",
"token": "tt" "token": "tt"
}) })
res = res.json() res = res.json()
@ -63,7 +66,7 @@ pp = {}
def add_timestamp(): def add_timestamp():
print(datetime.datetime.now(), ":") print("-------------------------↓",datetime.datetime.now(), "↓-------------------------")
def save_cache(dic): def save_cache(dic):
@ -103,14 +106,14 @@ def update_pairs(addr, ccid, clear=0):
CCID:{ccid.decode()} CCID:{ccid.decode()}
地址:{str(addr.getpeername())} 地址:{str(addr.getpeername())}
""" """
s2wx("","天通终端上线",t) s2wx("","天通终端上线",t)
p[ccid] = addr p[ccid] = addr
pp[addr] = ccid pp[addr] = ccid
else: else:
del p[ccid] del p[ccid]
del pp[addr] del pp[addr]
print("ccid已更新注册") print("在线终端已更新")
# 打印在线终端 # 打印在线终端
for i in p: for i in p:
print(i, "<--->", p[i]) print(i, "<--->", p[i])
@ -170,7 +173,7 @@ def tt_hh(addr, data):
msg[-1] = crc msg[-1] = crc
add_timestamp() add_timestamp()
addr.send(msg) addr.send(msg)
print("回复心跳。") print("服务器回复心跳")
# 从缓存中匹配到数据才重发 # 从缓存中匹配到数据才重发
# 仅有新终端上线时才重发 # 仅有新终端上线时才重发
@ -298,10 +301,13 @@ def tt_decode(addr, data):
class MyServer(socketserver.BaseRequestHandler): class MyServer(socketserver.BaseRequestHandler):
def setup(self):
self.server.timeout=3
def handle(self): # 回调 def handle(self): # 回调
add_timestamp() add_timestamp()
print("客户端", self.client_address, "已上线等待上报心跳注册ccid。") print("客户端", self.client_address, "连接等待上报心跳注册ccid。")
s2wx("天通消息","TCP客户端上线",str(self.client_address) + "上线等待上报心跳注册ccid。") s2wx("天通消息","TCP客户端已连接",str(self.client_address) + "连接等待上报心跳注册ccid。")
conn = self.request conn = self.request
# print(type(conn),conn.fd) # print(type(conn),conn.fd)
@ -318,6 +324,23 @@ class MyServer(socketserver.BaseRequestHandler):
print(self.client_address, "疑是非法连接,已切断。") print(self.client_address, "疑是非法连接,已切断。")
break break
def finish(self):
if self.request in pp:
t = f"""
### **终端信息:**
CCID:{get_ccid(self.request)}
地址:{str(self.client_address)}
"""
s2wx("","天通终端下线了",t)
print("终端",self.client_address,"下线了。")
update_pairs(self.request,get_ccid(self.request),1)
else:
print("客户端",self.client_address,"已断开连接。")
s2wx("天通消息","TCP客户端断开",str(self.client_address) + "已断开连接。")
if __name__ == "__main__": if __name__ == "__main__":
# load_cache # load_cache
@ -326,6 +349,7 @@ if __name__ == "__main__":
server = socketserver.ThreadingTCPServer(("", 7222), MyServer) server = socketserver.ThreadingTCPServer(("", 7222), MyServer)
add_timestamp() add_timestamp()
print("服务端初始化成功。", server.server_address) print("服务端初始化成功。", server.server_address)
server.timeout = 10
server.serve_forever() server.serve_forever()