Last active
September 23, 2024 11:45
-
-
Save doubleyou/41f0828e4b9b50a38f7db815feed0a6c to your computer and use it in GitHub Desktop.
grpc-gateway python example
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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() |
Thanks, this was really helpful.
How do I call the REST API? On which port is it running?
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
@fcharmy you can't. Actually it's just a reverse proxy, you could do that by yourself.
https://grpc-ecosystem.github.io/grpc-gateway/docs/faq/#my-grpc-server-is-written-in-scala-or-c-or-ruby-or-haskell-etc-is-there-a-scala-or-c-or-ruby-or-haskell-etc-version-of-grpc-gateway