添加自定义传输协议编解码
This commit is contained in:
82
mysql/prot_cmd.py
Normal file
82
mysql/prot_cmd.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import json
|
||||
|
||||
import mysql as sql
|
||||
import prot_codec as pr
|
||||
|
||||
|
||||
|
||||
SQL=sql.sql()
|
||||
SQL.init("test_data")
|
||||
|
||||
|
||||
_DATA=None
|
||||
|
||||
# 获取文件夹目录,参数:无
|
||||
def get_dirs_list():
|
||||
r=SQL.show_tables()
|
||||
j=[]
|
||||
for item in r:
|
||||
j.append(item[0])
|
||||
js=json.dumps(j)
|
||||
return js
|
||||
|
||||
|
||||
# 获取文件列表,参数:json{文件的目录}
|
||||
def get_files_list(j:bytearray):
|
||||
SQL.table_name=json.loads(j)["dir"]
|
||||
r=SQL.show()
|
||||
js=json.dumps(r)
|
||||
print(js)
|
||||
return js
|
||||
|
||||
# 获取文件,参数:json{文件目录,文件序号}
|
||||
def get_file_data(j:bytearray):
|
||||
js=json.loads(j)
|
||||
name,data=SQL.download_data(js["dir"],js["id"])
|
||||
print(name,len(data))
|
||||
global _DATA
|
||||
_DATA=data
|
||||
js=json.dumps({'name':name,"size":len(data)})
|
||||
return js
|
||||
|
||||
# 获取指定长度的数据,参数:json{偏移,长度}
|
||||
def get_data(j:bytearray):
|
||||
js=json.loads(j)
|
||||
off=js["off"]
|
||||
length=js["len"]
|
||||
if(off<len(_DATA)):
|
||||
data=_DATA[off:]
|
||||
if(length<len(data)):
|
||||
return data[:length]
|
||||
else:
|
||||
return data
|
||||
return bytearray()
|
||||
|
||||
_cmd_list={
|
||||
"get_dirs":get_dirs_list,
|
||||
"get_files":get_files_list,
|
||||
"get_file":get_file_data,
|
||||
"get_data":get_data,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# dirs_str=get_dirs()
|
||||
# data=pr.encode(b"dirs",dirs_str.encode("utf-8"))
|
||||
# print(data.hex(' '))
|
||||
# print(pr.decode(data))
|
||||
# files=_cmd_list["get_files"](b"checker_host_app")
|
||||
# print(json.loads(files))
|
||||
# print(list(_cmd_list.keys()))
|
||||
data=_cmd_list["get_file"](json.dumps({"dir":"checker_host_app","id":42}))
|
||||
print(data)
|
||||
ret=bytearray()
|
||||
off=0
|
||||
while off<len(_DATA):
|
||||
ret+=_cmd_list["get_data"](json.dumps({"off":off,"len":200}))
|
||||
off+=200
|
||||
if(ret==_DATA):
|
||||
print("check success")
|
||||
else:
|
||||
print("check failed")
|
Reference in New Issue
Block a user