diff --git a/proto/grpcdemo.proto b/proto/grpcdemo.proto index 9cb8339..9a1eb41 100644 --- a/proto/grpcdemo.proto +++ b/proto/grpcdemo.proto @@ -7,19 +7,19 @@ // The date service -service HostDateTime { - rpc GetDateTime (Null) returns (CurrentDateTime) {} +service HostCommand { + rpc runcommand (Command) returns (Results) {} } // Argument placeholder -message Null{ - string null = 1; +message Command{ + string command = 1; } // The response message containing the current date and time -message CurrentDateTime { - string currentdatetime = 1; +message Results { + string results = 1; } diff --git a/python/grpcdemo_client.py b/python/grpcdemo_client.py index fce9853..ff7ed20 100644 --- a/python/grpcdemo_client.py +++ b/python/grpcdemo_client.py @@ -1,17 +1,18 @@ from __future__ import print_function import grpc - import grpcdemo_pb2 import grpcdemo_pb2_grpc +import sys + PORTNO = 6000 def run(): channel = grpc.insecure_channel('localhost:%s' % PORTNO) - stub = grpcdemo_pb2_grpc.HostDateTimeStub(channel) - response = stub.GetDateTime(grpcdemo_pb2.Null(null='')) - print(response.currentdatetime + "\n") + stub = grpcdemo_pb2_grpc.HostCommandStub(channel) + response = stub.runcommand(grpcdemo_pb2.Command(command=sys.argv[1])) + print(response.results) if __name__ == '__main__': diff --git a/python/grpcdemo_server.py b/python/grpcdemo_server.py index 5069b48..0d5bf8e 100644 --- a/python/grpcdemo_server.py +++ b/python/grpcdemo_server.py @@ -7,18 +7,20 @@ import grpcdemo_pb2 import grpcdemo_pb2_grpc +import subprocess + ONE_DAY = 60 * 60 * 24 # Sleep time in seconds PORTNO = 6000 -class HostDateTime(grpcdemo_pb2_grpc.HostDateTimeServicer): +class HostCommand(grpcdemo_pb2_grpc.HostCommandServicer): - def GetDateTime(self, request, context): - return grpcdemo_pb2.CurrentDateTime(currentdatetime='%s: %s' % (socket.getfqdn(), time.strftime("%c"))) + def runcommand(self, request, context): + return grpcdemo_pb2.Results(results=subprocess.check_output(request.command.split())) def serve(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - grpcdemo_pb2_grpc.add_HostDateTimeServicer_to_server(HostDateTime(), server) + grpcdemo_pb2_grpc.add_HostCommandServicer_to_server(HostCommand(), server) server.add_insecure_port('[::]:%s' % PORTNO) server.start() try: