增加掉线判别
This commit is contained in:
parent
825e32fec3
commit
76b9167423
40
server.py
40
server.py
@ -34,7 +34,7 @@ def s2wx(title,description, content):
|
||||
"""通过自建msgpusher发送至微信
|
||||
当前title值无效
|
||||
"""
|
||||
|
||||
# pass
|
||||
# GET 方式
|
||||
# res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}"
|
||||
# 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={
|
||||
"title": title,
|
||||
"description": description,
|
||||
"content": content,
|
||||
"content": f"""
|
||||
### 时间戳:
|
||||
{datetime.datetime.now()}
|
||||
{content}""",
|
||||
"token": "tt"
|
||||
})
|
||||
res = res.json()
|
||||
@ -63,7 +66,7 @@ pp = {}
|
||||
|
||||
|
||||
def add_timestamp():
|
||||
print(datetime.datetime.now(), ":")
|
||||
print("-------------------------↓",datetime.datetime.now(), "↓-------------------------")
|
||||
|
||||
|
||||
def save_cache(dic):
|
||||
@ -103,14 +106,14 @@ def update_pairs(addr, ccid, clear=0):
|
||||
CCID:{ccid.decode()}
|
||||
地址:{str(addr.getpeername())}
|
||||
"""
|
||||
s2wx("","天通终端上线",t)
|
||||
s2wx("","天通终端上线了",t)
|
||||
p[ccid] = addr
|
||||
pp[addr] = ccid
|
||||
else:
|
||||
del p[ccid]
|
||||
del pp[addr]
|
||||
|
||||
print("ccid已更新注册。")
|
||||
print("在线终端已更新。")
|
||||
# 打印在线终端
|
||||
for i in p:
|
||||
print(i, "<--->", p[i])
|
||||
@ -170,7 +173,7 @@ def tt_hh(addr, data):
|
||||
msg[-1] = crc
|
||||
add_timestamp()
|
||||
addr.send(msg)
|
||||
print("回复心跳。")
|
||||
print("服务器回复心跳包。")
|
||||
|
||||
# 从缓存中匹配到数据才重发
|
||||
# 仅有新终端上线时才重发
|
||||
@ -298,10 +301,13 @@ def tt_decode(addr, data):
|
||||
|
||||
|
||||
class MyServer(socketserver.BaseRequestHandler):
|
||||
def setup(self):
|
||||
self.server.timeout=3
|
||||
|
||||
def handle(self): # 回调
|
||||
add_timestamp()
|
||||
print("客户端", self.client_address, "已上线,等待上报心跳注册ccid。")
|
||||
s2wx("天通消息","TCP客户端上线",str(self.client_address) + "已上线,等待上报心跳注册ccid。")
|
||||
print("客户端", self.client_address, "已连接,等待上报心跳注册ccid。")
|
||||
s2wx("天通消息","TCP客户端已连接",str(self.client_address) + "已连接,等待上报心跳注册ccid。")
|
||||
conn = self.request
|
||||
# print(type(conn),conn.fd)
|
||||
|
||||
@ -317,6 +323,23 @@ class MyServer(socketserver.BaseRequestHandler):
|
||||
conn.close()
|
||||
print(self.client_address, "疑是非法连接,已切断。")
|
||||
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__":
|
||||
@ -326,6 +349,7 @@ if __name__ == "__main__":
|
||||
server = socketserver.ThreadingTCPServer(("", 7222), MyServer)
|
||||
add_timestamp()
|
||||
print("服务端初始化成功。", server.server_address)
|
||||
server.timeout = 10
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user