使用black格式化

This commit is contained in:
murmur 2023-03-11 21:25:34 +08:00
parent 0212f884c6
commit ac02bc50db

View File

@ -5,7 +5,6 @@ import subprocess
import pickle import pickle
offs = 0 # 偏移量 offs = 0 # 偏移量
p = {} p = {}
# 保存终端信息ccid与地址对 # 保存终端信息ccid与地址对
@ -14,34 +13,37 @@ p ={}
pp = {} pp = {}
# cache={} # cache={}
from collections import defaultdict from collections import defaultdict
cache = defaultdict(list) cache = defaultdict(list)
def save_cache(dic): def save_cache(dic):
'''缓存未发送成功的消息 """缓存未发送成功的消息
仅在未发送成功时缓存''' 仅在未发送成功时缓存"""
# f=open("./cache.txt",mode='w+') # f=open("./cache.txt",mode='w+')
# f.write(str(dic)) # f.write(str(dic))
# f.close # f.close
pickle.dump(dic,open("./cache.txt",'wb')) pickle.dump(dic, open("./cache.txt", "wb"))
def load_cache(): def load_cache():
'''程序启动时加载缓存''' """程序启动时加载缓存"""
try: try:
# f=open("./cache.txt") # f=open("./cache.txt")
# c = eval(f.read()) # c = eval(f.read())
# f.close() # f.close()
c=pickle.load(open("./cache.txt",'rb')) c = pickle.load(open("./cache.txt", "rb"))
return c return c
except: except:
return None return None
def update_pairs(addr, ccid, clear=0): def update_pairs(addr, ccid, clear=0):
'''更新终端库 """更新终端库
addr:绑定tcp地址 addr:绑定tcp地址
ccid:终端号 ccid:终端号
clear:清空掉线终端''' clear:清空掉线终端"""
if ccid and not clear: if ccid and not clear:
# 添加或更新客户端 # 添加或更新客户端
@ -57,8 +59,9 @@ def update_pairs(addr,ccid,clear=0):
# for i in pp: # for i in pp:
# print(i,"-",pp[i]) # print(i,"-",pp[i])
def get_addr(ccid): def get_addr(ccid):
'''获取ccid对应的地址''' """获取ccid对应的地址"""
try: try:
# 如果有直接返回 # 如果有直接返回
@ -67,6 +70,7 @@ def get_addr(ccid):
# 没有就返回None # 没有就返回None
return None return None
def get_ccid(addr): def get_ccid(addr):
try: try:
# 如果有直接返回 # 如果有直接返回
@ -75,7 +79,8 @@ def get_ccid(addr):
# 没有就返回None # 没有就返回None
return None return None
'''
"""
+--------+--------------+----------+----------+--------------+------------------------+--------+ +--------+--------------+----------+----------+--------------+------------------------+--------+
| ff | 长度 | 地址信息 | 信令 | 内容 | 校验位 | | ff | 长度 | 地址信息 | 信令 | 内容 | 校验位 |
+--------+--------------+----------+----------+--------------+------------------------+--------+ +--------+--------------+----------+----------+--------------+------------------------+--------+
@ -83,7 +88,8 @@ def get_ccid(addr):
+ $TTMS +--------------+----------+----------+--------------+------------------------+--------+ + $TTMS +--------------+----------+----------+--------------+------------------------+--------+
| | 2Bytes | 4Bytes | 传输数据 | 0xaa | 载荷 | 2Bytes | | | 2Bytes | 4Bytes | 传输数据 | 0xaa | 载荷 | 2Bytes |
+--------+--------------+----------+----------+--------------+------------------------+--------+ +--------+--------------+----------+----------+--------------+------------------------+--------+
''' """
def send(fd, data): def send(fd, data):
try: try:
@ -92,8 +98,9 @@ def send(fd,data):
except: except:
return -1 return -1
def tt_hh(addr, data): def tt_hh(addr, data):
'''处理心跳''' """处理心跳"""
# 更新pairs # 更新pairs
ccid = data[7 + offs : 11 + offs] ccid = data[7 + offs : 11 + offs]
@ -112,22 +119,22 @@ def tt_hh(addr,data):
# 异常时退出循环不用再尝试发送后续msgs # 异常时退出循环不用再尝试发送后续msgs
# 退出前清空已发送成功的缓存数据 # 退出前清空已发送成功的缓存数据
# del方法会改变list大小 # del方法会改变list大小
while(i): while i:
cache[ccid].pop(0) cache[ccid].pop(0)
i = i - 1 i = i - 1
break break
return 0 return 0
def tt_trans(addr, data): def tt_trans(addr, data):
'''发送数据到指定ccid''' """发送数据到指定ccid"""
tccid = data[7 + offs : 11 + offs] tccid = data[7 + offs : 11 + offs]
taddr = get_addr(tccid) taddr = get_addr(tccid)
# 组帧 # 组帧
msg = bytearray(data[:]) msg = bytearray(data[:])
msg[:5]=b'$TTMX' msg[:5] = b"$TTMX"
msg[7 + offs : 11 + offs] = get_ccid(addr) msg[7 + offs : 11 + offs] = get_ccid(addr)
# 计算校验和 # 计算校验和
@ -148,7 +155,6 @@ def tt_trans(addr,data):
return 0 return 0
print("匹配终端:", tccid, "<--->", taddr) print("匹配终端:", tccid, "<--->", taddr)
# 发送 # 发送
try: try:
taddr.send(msg) taddr.send(msg)
@ -165,34 +171,32 @@ def tt_trans(addr,data):
print("发送失败,终端可能已掉线。", taddr) print("发送失败,终端可能已掉线。", taddr)
return 0 return 0
def err_handle(flag, addr): def err_handle(flag, addr):
'''错误处理 """错误处理
默认打印错误信息 默认打印错误信息
1-非法连接关闭连接 1-非法连接关闭连接
''' """
return 0 return 0
def tt_decode(addr, data): def tt_decode(addr, data):
'''处理收到的数据 """处理收到的数据
数据正常返回0 数据正常返回0
数据异常返回-1''' 数据异常返回-1"""
if data[:5] != b"$TTMS" or len(data) < 12 or len(data) > 200:
if data[:5] != b'$TTMS' or len(data) <12 or len(data)>200:
# 数据长度不可能小于12考虑非法接入的可能主动断开连接 # 数据长度不可能小于12考虑非法接入的可能主动断开连接
return -1 return -1
cmd = data[11 + offs] cmd = data[11 + offs]
if cmd == 0x01: if cmd == 0x01:
# 处理心跳 # 处理心跳
tt_hh(addr, data) tt_hh(addr, data)
elif cmd==0xaa: elif cmd == 0xAA:
# 处理数据 # 处理数据
tt_trans(addr, data) tt_trans(addr, data)
else: else:
@ -210,7 +214,9 @@ class MyServer(socketserver.BaseRequestHandler):
# update_pairs(conn,data[7+offs:11+offs]) # update_pairs(conn,data[7+offs:11+offs])
if not data: if not data:
break break
print ("接收到新数据",self.client_address,",长度",len(data),"\r\n", data.hex(" ")) print(
"接收到新数据", self.client_address, ",长度", len(data), "\r\n", data.hex(" ")
)
# ack_msg = "got from "+ str(ip) + " to " + str(self.client_address) + data # ack_msg = "got from "+ str(ip) + " to " + str(self.client_address) + data
# conn.send(data[:6]) # conn.send(data[:6])
if tt_decode(conn, data): if tt_decode(conn, data):
@ -219,14 +225,11 @@ class MyServer(socketserver.BaseRequestHandler):
break break
if __name__ == '__main__': if __name__ == "__main__":
# load_cache # load_cache
cache = load_cache() cache = load_cache()
server = socketserver.ThreadingTCPServer(('localhost',8005), MyServer) server = socketserver.ThreadingTCPServer(("localhost", 8005), MyServer)
ip, port = server.server_address ip, port = server.server_address
print("服务端已建立:", ip, port) print("服务端已建立:", ip, port)
server.serve_forever() server.serve_forever()