How to run grpc-gateway for python

5k views Asked by At

I am grpc rookie and wanted to create a REST endpoint for health check of the grpc server. For this I decided to use grpc gateway.

However, the example in docs for grpc-REST gateway for proxying grpc to json are only given for Golang. But I have a backend of python and want to use Google Cloud Endpoints to make a gRPC based api that can transcode incoming REST requests.

I did find a manual method of annotation generation in this stackoverflow answer . But I was wondering what is the best method of doing it. This is the sample .proto file I want to generate into a class.

 syntax = "proto3";
 package example;
+
+import "google/api/annotations.proto";
+
 message StringMessage {
   string value = 1;
 }

 service YourService {
-  rpc Echo(StringMessage) returns (StringMessage) {}
+  rpc Echo(StringMessage) returns (StringMessage) {
+    option (google.api.http) = {
+      post: "/v1/example/echo"
+      body: "*"
+    };
+  }
 }

How can I generate .proto files and create the grpc-gateway using python backend?

3

There are 3 answers

0
Lidi Zheng On

For proto generation, please take a look at our quick start guide for gRPC Python. grpc-gateway project also has a nice tutorial for how to setup the gateway process.

0
Richard Belleville On

grpc-gateway is a project that generates a reverse proxy that translates incoming JSON requests to gRPC. To do so, it generates Go code which is then compiled into the reverse proxy binary. You then deploy the reverse proxy binary alongside your intended backend , which would be written in Python in your case. The language the reverse proxy is written in should have no bearing on the language your backend is written in.

0
Angel Kjos On

Here's my example repo on how you can run Python gRPC Server and a grpc-gateway proxy for transcoding your api also to REST.

The server is implemented in python, and the gateway is a simple Go proxy (just one class).

Check it out here: https://github.com/angelkjos/grpc-gateway-python-example