syntax = "proto3";
package helloworld;
// The greeting service definition. service Greeter { // Sends a greeting. rpc SayHello (HelloRequest) returns (HelloReply) {} }
// The request message containing the user's name. message HelloRequest { string name = 1; }
// The response message containing the greetings. message HelloReply { string message = 1; repeated string snippets = 2; } from __future__ import absolute_import from __future__ import division from __future__ import print_function
from concurrent import futures import time
import grpc
import grpc_demo.lib.protos.helloworld_pb2 as helloworld_pb2 import grpc_demo.lib.protos.helloworld_pb2_grpc as helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context): reply = helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) reply.snippets.append('test-snippets') # print('reply:' + reply) return reply
def main(): """Main entrance."""
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0)
if __name__ == '__main__': main()
from __future__ import absolute_import from __future__ import division from __future__ import print_function
import grpc
import grpc_demo.lib.protos.helloworld_pb2 as helloworld_pb2 import grpc_demo.lib.protos.helloworld_pb2_grpc as helloworld_pb2_grpc
def main(): """Main entrance."""
# NOTE(gRPC Python Team): .close() is possible on a channel and should be # used in circumstances in which the with statement does not fit the needs # of the code. with grpc.insecure_channel('localhost:50051') as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message + ', '.join(response.snippets) + str(len(response.snippets)))
if __name__ == '__main__': main()killall python