您好,登錄后才能下訂單哦!
這篇文章主要講解了“python使用protobufde的過程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“python使用protobufde的過程”吧!
Protobuf(Google Protocol Buffers)是google開發(fā)的的一套用于數(shù)據(jù)存儲,網(wǎng)絡(luò)通信時用于協(xié)議編解碼的工具庫.它和XML和Json數(shù)據(jù)差不多,把數(shù)據(jù)已某種形式保存起來.Protobuf相對與XML和Json的不同之處,它是一種二進制的數(shù)據(jù)格式,具有更高的傳輸,打包和解包效率
1:序列化后體積相比Json和XML很小,適合網(wǎng)絡(luò)傳輸
2:支持跨平臺多語言
3:消息格式升級和兼容性還不錯
4:序列化反序列化速度很快,快于Json的處理速度
1、以二進制的方式存儲,除非你有 .proto 定義,否則你沒法直接讀出 Protobuf 的任何內(nèi)容。
2、功能簡單,無法用來表示復(fù)雜的概念。
一個標(biāo)量消息字段可以含有一個如下的類型——該表格展示了定義于.proto文件中的類型,以及與之對應(yīng)的、在自動生成的訪問類中定義的類型
基于序號的協(xié)議字段映射(類似key-value結(jié)構(gòu))
在消息中承載的數(shù)據(jù)分別對應(yīng)于每一個字段都有一個名字和一種類型。
syntax = "proto3"; package WeightEstimationUpdate; option java_package = "com.muyuan.platform.bar.patrol.pro"; // 請求包基類(沒有附加數(shù)據(jù),通信包不重新定義直接使用基類包) message BaseRequestCommon { string DeviceId = 1; // 設(shè)備編號 string MsgID = 2; // 消息ID,用UUID string Timestamp = 3; // unix時間戳(秒) uint32 Cmd = 4; // 指令信息 bytes payLoad = 5; // 消息體 } // 上報 message DeviceRegist { string version = 1; // string macAddr = 2; // } // 下發(fā) message PushUpgradeInfo { string version = 1; // 版本號 string packageName = 2; // string packageMd5 = 3; // string packageUrl = 4; // } // 上報 message ReportWeightEstimationStatus { string version = 1; // string state = 2; // } // 指令列表 enum EmCmd { CMD_NONE = 0x0000; // 指令開始范圍 //-----------------服務(wù)器端主動下發(fā)到設(shè)備端信令定義開始------------------ CMD_S2C_PUSH_UPGRADE_INFO = 0x0013; // 下發(fā)(協(xié)議包:PushUpgradeInfo) //-----------------服務(wù)器端主動下發(fā)到設(shè)備端信令定義結(jié)束----------------- //-----------------設(shè)備端主動上報到服務(wù)端信令定義開始------------------- CMD_C2S_REPORT_REGIST = 0x0060; // 注冊(協(xié)議包:WeightEstimationRegist) CMD_C2S_REPORT_FAULT = 0x0061; // 上報故障(協(xié)議包:ReportFault) CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS = 0x0063; // 上報狀態(tài)信息(協(xié)議包:WeightEstimationStatus) //-----------------設(shè)備端主動上報到服務(wù)端信令定義結(jié)束----------------- CMD_END = 0xFFFF; // 指令結(jié)束范圍 }
情況1: 收到通信信息
import test_pb2 as weight_pd base_request_common_obj = weight_pd.BaseRequestCommon() base_request_common_obj.ParseFromString(msg) payload = base_request_common_obj.payLoad push_upgrade_info_obj = weight_pd.PushUpgradeInfo() push_upgrade_info_obj.ParseFromString(payload) update_version = push_upgrade_info_obj.version update_zip_filename = push_upgrade_info_obj.packageName # 反向解析即可
情況2:發(fā)送通信信息
import test_pb2 as weight_pd base_request_common = weight_pd.BaseRequestCommon() base_request_common.DeviceId = deviceId base_request_common.MsgID = str(uuid.uuid4()) base_request_common.Timestamp = str(int(time.time())) # change item_list = weight_pd.EmCmd.items() #此為 protobuf 3.0.0 版本的 weight_dict = listtuple_dict(item_list) base_request_common.Cmd = weight_dict.get("CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS") #此為 protobuf 最新版本 # base_request_common.Cmd = weight_pd.EmCmd.CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS report_weight_estimation_status = weight_pd.ReportWeightEstimationStatus() report_weight_estimation_status.version = self.version report_weight_estimation_status.state = state base_request_common.payLoad = report_weight_estimation_status.SerializeToString() serializeToString = base_request_common.SerializeToString() # serializeToString 即為 二進制數(shù)據(jù)流 def listtuple_dict(item_list): weight_cmd_dict = {} for k, v in item_list: weight_cmd_dict.setdefault(k, v) return weight_cmd_dict```
感謝各位的閱讀,以上就是“python使用protobufde的過程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對python使用protobufde的過程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。