diff --git a/server.py b/server.py index fd8dcf3..bce130d 100755 --- a/server.py +++ b/server.py @@ -15,8 +15,9 @@ p = {} # 地址 ==ccid pp = {} + def add_timestamp(): - print(datetime.datetime.now(),":") + print(datetime.datetime.now(), ":") def save_cache(dic): @@ -95,6 +96,7 @@ def get_ccid(addr): +--------+--------------+----------+----------+--------------+------------------------+--------+ """ + def tt_hh(addr, data): """处理心跳""" @@ -102,8 +104,8 @@ def tt_hh(addr, data): ccid = data[7 + offs : 11 + offs] update_pairs(addr, ccid) # 组帧再返回 - msg=data[:] - msg[12:-1]=val = '%04x'%int(time.time()) + msg = data[:] + msg[12:-1] = val = "%04x" % int(time.time()) # 计算校验和 crc = 0 for i in msg[:-1]: @@ -116,9 +118,9 @@ def tt_hh(addr, data): # 从缓存中匹配到数据才重发 # 仅有新终端上线时才重发 if ccid in cache: - msgs = cache[ccid][:] # 浅拷贝 - sccid=msgs[0][7 + offs : 11 + offs] - print("有",len(msgs),"包缓存数据待发。",sccid,"-->",ccid) + msgs = cache[ccid][:] # 浅拷贝 + sccid = msgs[0][7 + offs : 11 + offs] + print("有", len(msgs), "包缓存数据待发。", sccid, "-->", ccid) for i in range(len(msgs)): try: add_timestamp() @@ -126,24 +128,25 @@ def tt_hh(addr, data): addr.send(msgs[i]) # 成功则清空已发送成功的缓存数据 cache[ccid].pop(0) - print("第",i+1,"包缓存数据发送完成。") + print("第", i + 1, "包缓存数据发送完成。") except: # 异常时退出循环,不用再尝试发送后续msgs - print("第",i+1,"包发送失败,停止尝试") + print("第", i + 1, "包发送失败,停止尝试") break if cache[ccid] == []: - print("缓存数据全部发送完成。",sccid,"-->",ccid) + print("缓存数据全部发送完成。", sccid, "-->", ccid) del cache[ccid] save_cache(cache) - return 0 + def check_valid(data): # 检查数据有效性 return bytearray(data[:]) + def tt_trans(addr, data): """发送数据到指定ccid""" @@ -171,7 +174,7 @@ def tt_trans(addr, data): cache[tccid].append(msg) save_cache(cache) - print("终端未在线或ccid",tccid,"错误。数据已缓存,待目标终端上线后重发。") + print("终端未在线或ccid", tccid, "错误。数据已缓存,待目标终端上线后重发。") return 0 print("匹配在线终端为:", tccid, "<--->", taddr) @@ -179,7 +182,7 @@ def tt_trans(addr, data): try: add_timestamp() taddr.send(msg) - print("数据发送成功。",sccid,"-->",tccid) + print("数据发送成功。", sccid, "-->", tccid) except Exception: # 发送失败 taddr.close() @@ -197,11 +200,11 @@ def tt_trans(addr, data): def data_split(data): # 按帧头分割长数据 - h= b"$TTMS" #b'\x24\x54\x54\x4d\x53' - dlist= data.split(h)[1:] + h = b"$TTMS" # b'\x24\x54\x54\x4d\x53' + dlist = data.split(h)[1:] for i in range(len(dlist)): dlist[i] = h + dlist[i] - print("解析为",len(dlist),"包数据。") + print("解析为", len(dlist), "包数据。") return dlist @@ -215,7 +218,7 @@ def tt_decode(addr, data): # 依据帧头和最小长度初判数据有效性 # 只要接收数据给的缓存足够大,即使是多条数据组成的长数据也默认帧格式是完整的,前几个字节必须是帧头 - if data[:5] != b"$TTMS" or len(data) < 12 : + if data[:5] != b"$TTMS" or len(data) < 12: return -1 dlist = data_split(data) @@ -231,18 +234,16 @@ def tt_decode(addr, data): flag = -1 break else: - flag=0 + flag = 0 else: break return flag - class MyServer(socketserver.BaseRequestHandler): - def handle(self): # 回调 add_timestamp() - print("终端", self.client_address,"已上线,等待上报心跳注册ccid。") + print("终端", self.client_address, "已上线,等待上报心跳注册ccid。") conn = self.request # print(type(conn),conn.fd) @@ -266,7 +267,7 @@ if __name__ == "__main__": server = socketserver.ThreadingTCPServer(("", 7222), MyServer) add_timestamp() - print("服务端初始化成功。",server.server_address) + print("服务端初始化成功。", server.server_address) server.serve_forever() @@ -275,4 +276,4 @@ if __name__ == "__main__": 单次数据不以帧头开始 单次数据长度不足11 未上报心跳(注册ccid)而直接发数据 -""" \ No newline at end of file +"""