-
-
Save caimaoy/df329225e9942d16586f12ad13c48990 to your computer and use it in GitHub Desktop.
grpc-gateway python example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment