Skip to content

Instantly share code, notes, and snippets.

@doubleyou
Last active August 15, 2023 10:30
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save doubleyou/41f0828e4b9b50a38f7db815feed0a6c to your computer and use it in GitHub Desktop.
Save doubleyou/41f0828e4b9b50a38f7db815feed0a6c to your computer and use it in GitHub Desktop.
grpc-gateway python example
from __future__ import print_function
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
def run():
channel = grpc.insecure_channel('localhost:8000')
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
if __name__ == '__main__':
run()
syntax = "proto3";
import "google/api/annotations.proto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
post: "/v1/hello"
body: "*"
};
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
GATEWAY_FLAGS := -I. -I/usr/local/include -I$(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis -I/usr/local/include
GRPC_FLAGS := --python_out=. --grpc_python_out=.
code:
python -m grpc_tools.protoc $(GRPC_FLAGS) $(GATEWAY_FLAGS) *.proto
gw:
protoc $(GATEWAY_FLAGS) \
--go_out=Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:. \
--grpc-gateway_out=logtostderr=true:. \
*.proto
deps:
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
from concurrent import futures
import time
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, {}'.format(request.name))
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:8000')
server.start()
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()
@ciiiii
Copy link

ciiiii commented Jul 19, 2018

After execute all of above, how can i use http api?

@laoqiu
Copy link

laoqiu commented Aug 30, 2018

@ciiii

gw:
protoc $(GATEWAY_FLAGS)
--go_out=plugins=grpc:.
--grpc-gateway_out=logtostderr=true:.
*.proto

make gw
u can got helloworld.pb.go helloworld.pb.gw.go
then use golang to start the gateway server

look this docs

@fcharmy
Copy link

fcharmy commented Dec 10, 2018

Is there no way to use python server.py to start the gateway server?
I thought this example can do it...

@gaozhidf
Copy link

gaozhidf commented Jan 21, 2019

@ciiii

gw:
protoc $(GATEWAY_FLAGS)
--go_out=plugins=grpc:.
--grpc-gateway_out=logtostderr=true:.
*.proto

make gw
u can got helloworld.pb.go helloworld.pb.gw.go
then use golang to start the gateway server

look this docs

you are right.

got the same issue, and it could be found in
grpc-ecosystem/grpc-gateway#338
philips/grpc-gateway-example@1491225

@Uditmittal
Copy link

Can you give me a working Example. How Can we run?

@montenegrodr
Copy link

Thanks, this was really helpful.

@sattwikb13
Copy link

How do I call the REST API? On which port is it running?

@angelkjos
Copy link

I created an example repo on how to run a python grpc server with grpc-gateway for REST. Check it out here:
https://github.com/angelkjos/grpc-gateway-python-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment