Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 19, 2018

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

@laoqiu

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.