{"id":1047,"date":"2016-09-29T19:31:19","date_gmt":"2016-09-29T11:31:19","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=1047"},"modified":"2020-04-30T18:42:33","modified_gmt":"2020-04-30T10:42:33","slug":"python%e8%af%bb%e5%86%99protobuf%e7%8e%af%e5%a2%83%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=1047","title":{"rendered":"python\u89e3\u6790\u548c\u6253\u5305protobuf"},"content":{"rendered":"<h1><strong>\u6982\u8ff0<\/strong><\/h1>\n<p>Protocol Buffers\uff0c\u7b80\u79f0protobuf\u6216pb\uff0c\u662fGoogle\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u79cd\u6570\u636e\u63cf\u8ff0\u8bed\u8a00\uff0c\u7c7b\u4f3c\u4e8eXML\u548cJSON\u80fd\u591f\u5c06\u7ed3\u6784\u5316\u6570\u636e\u5e8f\u5217\u5316\u7528\u4e8e\u6570\u636e\u5b58\u50a8\u3001\u901a\u4fe1\u534f\u8bae\u7b49\u65b9\u9762\u3002\u76ee\u524d\u5b98\u65b9\u652f\u6301C++\u3001JAVA\u3001Python\u7b49\u4e09\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<p>\u4e0eXML\u548cJSON\u76f8\u6bd4\uff0cprotobuf\u5728\u914d\u7f6e\u65f6\u5c06\u6570\u636ekey\u6620\u5c04\u4e3a\u9012\u589e\u6570\u5b57\uff0c\u4f7f\u7528\u4e8c\u8fdb\u5236\u4f20\u8f93\uff0c\u5e76\u80fd\u591f\u6839\u636eschema\u81ea\u52a8\u751f\u6210\u5404\u8bed\u8a00\u7684\u6253\u5305\u3001\u89e3\u6790\u56dekv\u7684\u4ee3\u7801\uff0c\u975e\u5e38\u65b9\u4fbf\u5404\u8bed\u8a00\u6570\u636e\u4e92\u901a\uff0c\u4e14\u6709\u4ee5\u4e0b<span style=\"color: #ff0000;\">\u4f18\u70b9<\/span>\uff1a<\/p>\n<div class=\"para\">1. \u6570\u636e\u6587\u4ef6\u53ea\u9700\u539f\u6765\u76841\/10\u81f31\/3 \u00a0#\u76f8\u540c\u7684\u6570\u636e\uff0c\u5927\u5c0f\u53ea\u6709json\u76841\/10<\/div>\n<div class=\"para\">2. \u89e3\u6790\u901f\u5ea6\u662f\u539f\u6765\u768420\u500d\u81f3100\u500d<\/div>\n<div class=\"para\">3. \u901a\u8fc7\u9012\u589e\u6570\u5b57\u505akey\u4f20\u8f93value\uff0c\u51cf\u5c11\u4e86\u4e8c\u4e49\u6027\uff0c\u5728\u63a5\u53e3\u53d1\u751f\u53d8\u5316\u65f6\u5b8c\u5168\u50cf\u540e\u517c\u5bb9<\/div>\n<div class=\"para\">4. \u975e\u5e38\u5bb9\u6613\u81ea\u52a8\u751f\u6210\u5404\u8bed\u8a00\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u8bbf\u95ee\u7c7b\uff0c\u5c06\u6570\u636ekey\u6839\u636eschema\u81ea\u52a8\u8f6c\u56de\u6613\u7406\u89e3\u7684\u82f1\u6587key<\/div>\n<div class=\"para\"><\/div>\n<div class=\"para\">protobuf\u4e0d\u662f\u7528\u6765\u66ff\u4ee3json\u548cxml\u7684\uff0c\u5b83\u6700\u521d\u7684<span style=\"color: #ff0000;\">\u76ee\u7684\u662f\u4e3a\u4e86\u66f4\u5c0f\u7684\u5e26\u5bbd\u5360\u7528\u3001\u66f4\u5feb\u7684\u4f20\u8f93\u901f\u5ea6<\/span>\uff0c\u6240\u4ee5\u4f7f\u7528\u6570\u636ekey+\u4e8c\u8fdb\u5236\u4f20\u8f93\uff0c\u4e5f\u4ea7\u751f\u4e86\u4e00\u4e9b<span style=\"color: #ff0000;\">\u7f3a\u70b9<\/span>\uff1a<\/div>\n<div class=\"para\"><\/div>\n<div class=\"para\">\n<div class=\"para\">1. \u5bf9\u4eba\u4e0d\u53ef\u8bfb\u3002(\u9002\u5408\u52a0\u5bc6\u6570\u636e\u4f20\u8f93)<\/div>\n<div class=\"para\">2. \u4e0d\u80fd\u53d1\u7ed9web\u6d4f\u89c8\u5668\uff0c\u6bd4\u5982js\u65e0\u6cd5\u5904\u7406\u8fd9\u79cd\u6570\u636e\u3002\uff08\u9002\u5408c\u3001java\u3001python\u8bed\u8a00\u5f00\u53d1\u7684\u7a0b\u5e8f\u95f4\u5171\u4eab\u6570\u636e\uff09<\/div>\n<div class=\"para\">3. \u5f00\u53d1\u9002\u914d\u6210\u672c\u9ad8\uff0c\u4e0d\u9002\u5408schema\u7ecf\u5e38\u53d8\u7684\u573a\u666f\u3002\uff08\u9002\u5408\u516c\u53f8\u5185\u90e8\u5404\u4ea7\u54c1\u95f4\u76f8\u5bf9\u901a\u7528\u7a33\u5b9a\u7684\u534f\u8bae\u901a\u4fe1\uff09<\/div>\n<div class=\"para\"><\/div>\n<h1 class=\"para\"><strong>\u5b89\u88c5\u548c\u4f7f\u7528<\/strong><\/h1>\n<div class=\"para\"><\/div>\n<\/div>\n<p>\u524d\u63d0\uff1a\u9700\u8981gcc, gcc-c++, make, build\u7b49\u7f16\u8bd1\u73af\u5883<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">yum -y install gcc gcc-c++ libstdc++-devel make build<\/code><\/pre>\n<h4>1. \u4e0b\u8f7dprotobuf\u6e90\u4ee3\u7801<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\"># proto2\u7528\u4e0b\u8fb9\u8fd9\u4e2a\u5305\r\nwget https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2016\/09\/protobuf-2.5.0.tar.gz\r\n# proto3\u76f4\u63a5\u7528\u6700\u65b0release\u7248\u5373\u53ef\uff1ahttps:\/\/github.com\/protocolbuffers\/protobuf\/releases\r\n\u00a0wget https:\/\/github.com\/protocolbuffers\/protobuf\/releases\/download\/v3.11.4\/protobuf-all-3.11.4.tar.gz<\/code><\/pre>\n<p>*\u6ce8\uff1a\u4f7f\u7528python3+proto2\u65f6\uff0cpython\u7684protobuf\u5e93\u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898\u80fd\u628a\u4eba\u6298\u9ebd\u6b7b\uff08proto2\u4e3b\u8981\u4e3apython2\u5f00\u53d1\uff0c\u6700\u540e\u4e00\u6b21\u53d1\u5e03\u662f14\u5e74\uff0c\u5bf9python3\u7684\u517c\u5bb9\u6027\u6781\u5dee\uff09\uff0c\u5efa\u8bae\u522b\u5728\u8fd9\u4e2a\u4e8b\u60c5\u4e0a\u6d6a\u8d39\u65f6\u95f4\u4e86\uff0c\u5982\u679c\u5f53\u524d\u7528\u7684proto2\uff0c\u8981\u4e48\u5347\u7ea7proto\u7248\u672c\u52303\uff0c\u8981\u4e48\u964dpython\u7248\u672c\u52302\u3002<\/p>\n<h4>2. \u89e3\u538b\uff0c\u7f16\u8bd1\uff0c\u5b89\u88c5<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">tar zxvf protobuf-3.11.4.tar.gz\r\ncd protobuf-3.11.4\r\n.\/configure\r\nmake\r\nmake check\r\nmake install<\/code><\/pre>\n<h4>3. \u5b89\u88c5\u5b8c\u6210\uff0c\u9a8c\u8bc1Linux\u547d\u4ee4<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">protoc --version\r\n&gt;&gt;libprotoc 3.11.4<\/code><\/pre>\n<h4>4. \u7ee7\u7eed\u5b89\u88c5protobuf\u7684python\u6a21\u5757(\u5982\u679c\u4e0d\u7528python\uff0c\u53ef\u8df3\u8fc7\u8fd9\u4e00\u6b65)<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">cd .\/python  \r\npython setup.py build\r\npython setup.py test\r\npython setup.py install<\/code><code><\/code><\/pre>\n<h4>5. \u9a8c\u8bc1Python\u6a21\u5757\u662f\u5426\u88ab\u6b63\u786e\u5b89\u88c5<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">python\r\n&gt;&gt;&gt;import google.protobuf<\/code><\/pre>\n<h4 class=\"p2\">6. \u6839\u636etest.proto\u751f\u6210python\u7c7btest_pb2.py<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\"># \u914d\u7f6etest.proto\r\nsyntax=\"proto3\";\r\npackage test.abc;\r\n\r\n\/\/Test\r\nmessage Test {\r\n    string name = 1;\r\n    uint32 speed = 2;\r\n    State state = 3; \r\n    float time = 5;\r\n    bytes content = 6;\r\n    \r\n    enum State { \r\n        STATE_OFF               = 0;\r\n        STATE_STANDBY           = 1; \r\n        STATE_READY             = 2;\r\n    }\r\n};\r\n\r\n# \u751f\u6210pythhon\u7c7b\r\n<\/code><code class=\"\">protoc --python_out=.\/ .\/test.proto  #--python_out=\u751f\u6210\u7684python\u6587\u4ef6\u8f93\u51fa\u8def\u5f84\u00a0 \u00a0\u5bf9\u5e94\u7684proto\u6587\u4ef6\r\n<\/code><\/pre>\n<h4>7.\u89e3\u6790\u6d4b\u8bd5<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">from test_pb2 import Test\r\ntest = Test()\r\ntest.name='A'\r\n# \u8f6cpb\r\nmsg = test.SerializeToString()    #\u5e8f\u5217\u5316\u8fd9\u4e2amessage\u548c\u4ee5\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u8fd4\u56de\u3002 \u6ce8\u610f\uff0c\u8fd9\u662f\u4e8c\u8fdb\u884c\u5b57\u8282\uff0c\u4e0d\u662f\u4e00\u4e2a\u6587\u672c\uff1b \u6211\u4eec\u53ea\u4f7f\u7528str\u7c7b\u578b\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u4fbf\u7684\u5bb9\u5668\u3002\r\nprint(msg)\r\n# \u89e3\u6790pb\r\ntest.ParseFromString(msg)    #\u4ece\u7ed9\u51fa\u7684\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u4e00\u6761message\r\nprint(test)<\/code><\/pre>\n<h4>8.\u5176\u4ed6<\/h4>\n<p>\u5982\u6709\u524d\u7aefjs\u89e3\u6790proto\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 <a href=\"https:\/\/github.com\/protobufjs\/protobuf.js\" target=\"_blank\" rel=\"noopener noreferrer\">protobuf.js<\/a>\u00a0\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #808080;\">\u53c2\u8003\uff1a<\/span><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/losophy\/article\/details\/17006573\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728Python\u4e2d\u4f7f\u7528protocol buffers\u53c2\u8003\u6307\u5357<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\/tree\/master\/src\">https:\/\/github.com\/protocolbuffers\/protobuf\/tree\/master\/src<\/a><\/p>\n<p><a href=\"https:\/\/googleapis.dev\/python\/protobuf\/latest\/google\/protobuf\/message.html\" target=\"_blank\" rel=\"noopener noreferrer\">google.protobuf.message<\/a><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/caimouse\/article\/details\/45339861\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728Python 3.4\u91cc\u4f7f\u7528Protobuf 2.6<\/a><\/p>\n<p><a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto3\" target=\"_blank\" rel=\"noopener noreferrer\">Language Guide (proto3)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 Protocol Buffers\uff0c\u7b80\u79f0protobuf\u6216pb\uff0c\u662fGoogle\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u79cd\u6570\u636e\u63cf\u8ff0\u8bed\u8a00\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[255,241],"tags":[442,439,441,434],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1047"}],"collection":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1047"}],"version-history":[{"count":0,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}