├── .idea ├── .gitignore ├── Google-remote-procedure-call.iml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── README.md ├── __pycache__ ├── calculator_pb2.cpython-36.pyc ├── calculator_pb2.cpython-37.pyc ├── calculator_pb2_grpc.cpython-36.pyc └── calculator_pb2_grpc.cpython-37.pyc ├── calculator.proto ├── calculator.py ├── calculator_pb2.py ├── calculator_pb2_grpc.py ├── client.py └── server.py /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/Google-remote-procedure-call.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 42 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /__pycache__/calculator_pb2.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishkumar30/Microservices/3bc297749976e26477d77fc9b7ffb6d285c198fe/__pycache__/calculator_pb2.cpython-36.pyc -------------------------------------------------------------------------------- /__pycache__/calculator_pb2.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishkumar30/Microservices/3bc297749976e26477d77fc9b7ffb6d285c198fe/__pycache__/calculator_pb2.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/calculator_pb2_grpc.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishkumar30/Microservices/3bc297749976e26477d77fc9b7ffb6d285c198fe/__pycache__/calculator_pb2_grpc.cpython-36.pyc -------------------------------------------------------------------------------- /__pycache__/calculator_pb2_grpc.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishkumar30/Microservices/3bc297749976e26477d77fc9b7ffb6d285c198fe/__pycache__/calculator_pb2_grpc.cpython-37.pyc -------------------------------------------------------------------------------- /calculator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | message Number { 4 | float value = 1; 5 | } 6 | 7 | service Calculator { 8 | rpc SquareRoot(Number) returns (Number) {} 9 | } -------------------------------------------------------------------------------- /calculator.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def square_root(x): 5 | output = math.sqrt(x) 6 | return output 7 | 8 | # python3 -m grpc_tools.protoc -I.python_out=. --grpc_python_out=. Calculator.proto 9 | -------------------------------------------------------------------------------- /calculator_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: calculator.proto 4 | """Generated protocol buffer code.""" 5 | from google.protobuf import descriptor as _descriptor 6 | from google.protobuf import message as _message 7 | from google.protobuf import reflection as _reflection 8 | from google.protobuf import symbol_database as _symbol_database 9 | # @@protoc_insertion_point(imports) 10 | 11 | _sym_db = _symbol_database.Default() 12 | 13 | 14 | 15 | 16 | DESCRIPTOR = _descriptor.FileDescriptor( 17 | name='calculator.proto', 18 | package='', 19 | syntax='proto3', 20 | serialized_options=None, 21 | create_key=_descriptor._internal_create_key, 22 | serialized_pb=b'\n\x10\x63\x61lculator.proto\"\x17\n\x06Number\x12\r\n\x05value\x18\x01 \x01(\x02\x32.\n\nCalculator\x12 \n\nSquareRoot\x12\x07.Number\x1a\x07.Number\"\x00\x62\x06proto3' 23 | ) 24 | 25 | 26 | 27 | 28 | _NUMBER = _descriptor.Descriptor( 29 | name='Number', 30 | full_name='Number', 31 | filename=None, 32 | file=DESCRIPTOR, 33 | containing_type=None, 34 | create_key=_descriptor._internal_create_key, 35 | fields=[ 36 | _descriptor.FieldDescriptor( 37 | name='value', full_name='Number.value', index=0, 38 | number=1, type=2, cpp_type=6, label=1, 39 | has_default_value=False, default_value=float(0), 40 | message_type=None, enum_type=None, containing_type=None, 41 | is_extension=False, extension_scope=None, 42 | serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), 43 | ], 44 | extensions=[ 45 | ], 46 | nested_types=[], 47 | enum_types=[ 48 | ], 49 | serialized_options=None, 50 | is_extendable=False, 51 | syntax='proto3', 52 | extension_ranges=[], 53 | oneofs=[ 54 | ], 55 | serialized_start=20, 56 | serialized_end=43, 57 | ) 58 | 59 | DESCRIPTOR.message_types_by_name['Number'] = _NUMBER 60 | _sym_db.RegisterFileDescriptor(DESCRIPTOR) 61 | 62 | Number = _reflection.GeneratedProtocolMessageType('Number', (_message.Message,), { 63 | 'DESCRIPTOR' : _NUMBER, 64 | '__module__' : 'calculator_pb2' 65 | # @@protoc_insertion_point(class_scope:Number) 66 | }) 67 | _sym_db.RegisterMessage(Number) 68 | 69 | 70 | 71 | _CALCULATOR = _descriptor.ServiceDescriptor( 72 | name='Calculator', 73 | full_name='Calculator', 74 | file=DESCRIPTOR, 75 | index=0, 76 | serialized_options=None, 77 | create_key=_descriptor._internal_create_key, 78 | serialized_start=45, 79 | serialized_end=91, 80 | methods=[ 81 | _descriptor.MethodDescriptor( 82 | name='SquareRoot', 83 | full_name='Calculator.SquareRoot', 84 | index=0, 85 | containing_service=None, 86 | input_type=_NUMBER, 87 | output_type=_NUMBER, 88 | serialized_options=None, 89 | create_key=_descriptor._internal_create_key, 90 | ), 91 | ]) 92 | _sym_db.RegisterServiceDescriptor(_CALCULATOR) 93 | 94 | DESCRIPTOR.services_by_name['Calculator'] = _CALCULATOR 95 | 96 | # @@protoc_insertion_point(module_scope) 97 | -------------------------------------------------------------------------------- /calculator_pb2_grpc.py: -------------------------------------------------------------------------------- 1 | # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! 2 | """Client and server classes corresponding to protobuf-defined services.""" 3 | import grpc 4 | 5 | import calculator_pb2 as calculator__pb2 6 | 7 | 8 | class CalculatorStub(object): 9 | """Missing associated documentation comment in .proto file.""" 10 | 11 | def __init__(self, channel): 12 | """Constructor. 13 | 14 | Args: 15 | channel: A grpc.Channel. 16 | """ 17 | self.SquareRoot = channel.unary_unary( 18 | '/Calculator/SquareRoot', 19 | request_serializer=calculator__pb2.Number.SerializeToString, 20 | response_deserializer=calculator__pb2.Number.FromString, 21 | ) 22 | 23 | 24 | class CalculatorServicer(object): 25 | """Missing associated documentation comment in .proto file.""" 26 | 27 | def SquareRoot(self, request, context): 28 | """Missing associated documentation comment in .proto file.""" 29 | context.set_code(grpc.StatusCode.UNIMPLEMENTED) 30 | context.set_details('Method not implemented!') 31 | raise NotImplementedError('Method not implemented!') 32 | 33 | 34 | def add_CalculatorServicer_to_server(servicer, server): 35 | rpc_method_handlers = { 36 | 'SquareRoot': grpc.unary_unary_rpc_method_handler( 37 | servicer.SquareRoot, 38 | request_deserializer=calculator__pb2.Number.FromString, 39 | response_serializer=calculator__pb2.Number.SerializeToString, 40 | ), 41 | } 42 | generic_handler = grpc.method_handlers_generic_handler( 43 | 'Calculator', rpc_method_handlers) 44 | server.add_generic_rpc_handlers((generic_handler,)) 45 | 46 | 47 | # This class is part of an EXPERIMENTAL API. 48 | class Calculator(object): 49 | """Missing associated documentation comment in .proto file.""" 50 | 51 | @staticmethod 52 | def SquareRoot(request, 53 | target, 54 | options=(), 55 | channel_credentials=None, 56 | call_credentials=None, 57 | insecure=False, 58 | compression=None, 59 | wait_for_ready=None, 60 | timeout=None, 61 | metadata=None): 62 | return grpc.experimental.unary_unary(request, target, '/Calculator/SquareRoot', 63 | calculator__pb2.Number.SerializeToString, 64 | calculator__pb2.Number.FromString, 65 | options, channel_credentials, 66 | insecure, call_credentials, compression, wait_for_ready, timeout, metadata) 67 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | import grpc 2 | 3 | # import the generated classes 4 | import calculator_pb2 5 | import calculator_pb2_grpc 6 | 7 | 8 | # open a gRPC channel 9 | channel = grpc.insecure_channel('localhost:50051') 10 | 11 | # create a stub (client) 12 | stub = calculator_pb2_grpc.CalculatorStub(channel) 13 | 14 | # create a valid request message 15 | number = calculator_pb2.Number(value=16) 16 | 17 | # make the call 18 | response = stub.SquareRoot(number) 19 | 20 | # et voilà 21 | print(response.value) -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | import grpc 2 | from concurrent import futures 3 | import time 4 | 5 | # import the generated classes 6 | import calculator_pb2 7 | import calculator_pb2_grpc 8 | 9 | # import the original calculator.py 10 | import calculator 11 | 12 | # create a class to define the server functions, derived from 13 | # calculator_pb2_grpc.CalculatorServicer 14 | class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer): 15 | 16 | # calculator.square_root is exposed here 17 | # the request and response are of the data type 18 | # calculator_pb2.Number 19 | def SquareRoot(self, request, context): 20 | response = calculator_pb2.Number() 21 | response.value = calculator.square_root(request.value) 22 | return response 23 | 24 | 25 | # create a gRPC server 26 | server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 27 | 28 | # use the generated function `add_CalculatorServicer_to_server` 29 | # to add the defined class to the server 30 | calculator_pb2_grpc.add_CalculatorServicer_to_server( 31 | CalculatorServicer(), server) 32 | 33 | # listen on port 50051 34 | print('Starting server. Listening on port 50051.') 35 | server.add_insecure_port('[::]:50051') 36 | server.start() 37 | 38 | # since server.start() will not block, 39 | # a sleep-loop is added to keep alive 40 | try: 41 | while True: 42 | time.sleep(86400) 43 | except KeyboardInterrupt: 44 | server.stop(0) --------------------------------------------------------------------------------