Flask sample project to demonstrate working with protocol buffers in request and response.
Runs currently on python 2. To run it on Python 3, just change add parentheses of print statements in each file.
Study more about Protocol buffer here.
- First create a .proto file with the schema of your request/response classes. An example is given in the file addressbook.proto
- Now, when you have created the .proto file, you have to generate python classes/descriptors for each and everything in your .proto file. To help automate this, Google provides protoc compiler and can be downloaded with help of this blog as the documentation is not quite good.
- After installing the compiler, run the command
protoc -I=. --python_out=. ./addressbook.proto
. A file addressbook_pb2.py will be created. - Now, you can use this file to serialise/deserialise your protobuf files. You can learn more about these files here.
- I have provided 2 files make_proto_file.py to generate a sample protobuf file (saved as a binary file) and read_display_proto_file.py to display the contents of the binary protobuf file on stdout.
- To run any of these files, provide a system argument as the name of binary file to be created. As example -
python make_proto_file.py protofile1.pb
. - Now, you can run the Flask app by
python flask_test.py
. - Use curl with the following command to test -
curl -X POST -H "Accept: application/x-protobuf" -H "Content-type: application/x-protobuf" http://127.0.0.1:5000/load --data-binary @protofile1.pb > outputfile.pb
. The contents of the file will be printed as a json object after deserialisation of the protobuf file. - At last, you can check the output of outputfile.pb. In this case, it will be the same as we are uploading the same file, and returning the same.
Issues, contributions and additional examples are welcome.