commit 959be5996a9e0cbf01b62603f87f755991301367 Author: Michael Mandl Date: Mon Mar 9 21:15:48 2020 +0100 Initial working version diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..df5b0f7 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,3 @@ +python3 -m grpc_tools.protoc -I protos --python_out=python-server --grpc_python_out=python-server protos/hello.proto +python3 -m grpc_tools.protoc -I protos --python_out=python-client --grpc_python_out=python-client protos/hello.proto + diff --git a/protos/hello.proto b/protos/hello.proto new file mode 100644 index 0000000..27441bf --- /dev/null +++ b/protos/hello.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +message GreetRequest { + string message = 1; +} + +message GreetReply { + string message = 1; +} + +service HelloService { + rpc greet(GreetRequest) returns (GreetReply); +} diff --git a/python-client/greet_client.py b/python-client/greet_client.py new file mode 100755 index 0000000..e2b03c0 --- /dev/null +++ b/python-client/greet_client.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import grpc + +import hello_pb2 +import hello_pb2_grpc + +channel = grpc.insecure_channel("localhost:50505") +stub = hello_pb2_grpc.HelloServiceStub(channel) + +reply = stub.greet(hello_pb2.GreetRequest(message="Hello service")) + +print(f"service replied \"{reply.message}\"") diff --git a/python-server/greet_server.py b/python-server/greet_server.py new file mode 100755 index 0000000..606b22f --- /dev/null +++ b/python-server/greet_server.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import grpc + +import hello_pb2 +import hello_pb2_grpc + +from concurrent import futures + +import time + +class GreetServer(hello_pb2_grpc.HelloServiceServicer): + def greet(self, request, context): + print(f"received \"{request.message}\"") + return hello_pb2.GreetReply(message="Hello back") + + +def serve(): + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + hello_pb2_grpc.add_HelloServiceServicer_to_server(GreetServer(), server) + server.add_insecure_port("[::]:50505") + server.start() + server.wait_for_termination() + +if __name__ == "__main__": + print("serving...") + serve()