-
-
Save doubleyou/41f0828e4b9b50a38f7db815feed0a6c to your computer and use it in GitHub Desktop.
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() |
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
Is there no way to use python server.py to start the gateway server?
I thought this example can do it...
gw:
protoc $(GATEWAY_FLAGS)
--go_out=plugins=grpc:.
--grpc-gateway_out=logtostderr=true:.
*.protomake gw
u can got helloworld.pb.go helloworld.pb.gw.go
then use golang to start the gateway server
you are right.
got the same issue, and it could be found in
grpc-ecosystem/grpc-gateway#338
philips/grpc-gateway-example@1491225
Can you give me a working Example. How Can we run?
@fcharmy you can't. Actually it's just a reverse proxy, you could do that by yourself.
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
After execute all of above, how can i use http api?