Windows 10 搭建Python3 安装使用 protobuf
生活随笔
收集整理的這篇文章主要介紹了
Windows 10 搭建Python3 安装使用 protobuf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Windows 10 搭建Python3 安裝使用 protobuf
Protobuf對比XML、Json等其他序列化的優勢
?
| protobuf | jackson | xstream | Serializable | hessian2 | hessian2壓縮 | hessian1 | |
| 序列化(單位ns) | 1154 | 5421? | 92406? | 10189 | 26794 | 100766 | 29027 |
| 反序列化(單位ns) | 1334 | 8743? | 117329? | 64027 | 37871 | 188432 | 37596 |
| bytes | 97 | 311? | 664? | 824 | 374 | 283 | 495 |
準備環境:
1,Python版本3.5.4
2,Protobuf版本3.7.0
3,Protobuf安裝包:protoc-3.7.0-rc1-win64.zip
4,Win10 64位系統?
?
步驟:
【下載protoc】
https://github.com/google/protobuf/releases
根據自己的平臺下載對應的編譯器,我的是win10-64位,所以下載??protoc-3.7.0-rc1-win64.zip
?
?
設置環境變量:這一步使你在本地任何地方使用protoc這個指令
(右擊“此電腦”。。。)
?
?
測試protoc:
新打開一個命令行:輸入protoc --version,如果將輸出版本號,說明protoc安裝好了
?
?
【編寫.proto協議文件】
新建一個protobuf文件夾,手動創建test2.proto文件:
?
并在test2.proto中輸入:
syntax = "proto2"; message testinfo { required int32 devtype = 1; required int32 devid = 2; required int32 unitid = 3; required int32 chlid = 4; optional int32 testid = 5 [default = 0]; required bytes stepdata = 6; }【編譯】:
打開命令行,切換到protobuf文件夾下下面,執行protoc --python_out=./?test2.proto
然后會生成一個python文件
在目錄下新建文件?test.py,寫入代碼
import test2_pb2testinfo = test2_pb2.testinfo() testinfo.devtype = 100 testinfo.devid = 2 testinfo.unitid = 3 testinfo.chlid = 4 testinfo.testid = 250 testinfo.stepdata = b'abd'print(testinfo, testinfo.devtype) # 打印 protobuf 結構的內容 out = testinfo.SerializeToString() print(out) # 打印 Protobuf 序列字符串decode = test2_pb2.testinfo() decode.ParseFromString(out) print(decode) # 打印 解析Protobuf后的內容運行python代碼,得到以下結果,證明實驗成功!
?
posted @ 2019-02-20 17:10 luyanjie 閱讀(...) 評論(...) 編輯 收藏 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Windows 10 搭建Python3 安装使用 protobuf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 故障error: failed to r
- 下一篇: Jquery 获取select选中的va