Install protoc on CentOS 6.7
Clone the repo
git clone https://github.com/google/protobuf.git
Below is to follow here
https://github.com/google/protobuf/blob/master/src/README.md
Install tools
sudo yum install -y autoconf automake libtool curl make g++ unzip
Generate configure script
cd protobuf
./autogen.sh
Build C++ runtime and protoc
./configure --prefix=/usr
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
Test
protoc --version
libprotoc 3.0.0
Install Protocol Buffers for Python on Centos 6.7
Download and install it
wget https://github.com/google/protobuf/releases/download/v3.0.0-beta-4/protobuf-python-3.0.0-beta-4.tar.gz
tar -zxvf protobuf-python-3.0.0-beta-4.tar.gz
cd protobuf-3.0.0-beta-4/
cd python
python setup.py build
python setup.py test
python setup.py install (as root)
Install protoc.exe on Windows 7
Download protoc-3.0.0-beta-4-win32.zip
Extract and place protoc.exe to C:\Windows\
Test
Test
protoc --version
libprotoc 3.0.0
Install Protocol Buffers for Python on Windows 7
Protobuf for Python
Download protobuf-python-3.0.0-beta-4.zip
Extract, go to ./python folder
python setup.py build
python setup.py test
python setup.py install (as root)
Test Protocol Buffers for Python on CentOS 6.7 and Windows 7
Create proto file
Create a file called Order.proto with following content
syntax="proto2";
message Order
{
required int32 time = 1;
required int32 userid = 2;
required float price = 3;
optional string desc = 4;
}
Generate .py file
protoc --python_out=. Order.proto
Create main.py
Create a file called main.py with following content
from Order_pb2 import *
if __name__=='__main__':
order = Order()
order.time = 123
order.userid = 456
order.price = 78.9
order.desc = "here"
print ('order', order)
print ('time', order.time)
print ('userid', order.userid)
print ('price', order.price)
print ('desc', order.desc)
Run main.py
D:\Lab\ProtocolBuffers>python main.py
order time: 123
userid: 456
price: 78.9
desc: "here"
time 123
userid 456
price 78.9
desc here
沒有留言:
張貼留言