Ejemplo n.º 1
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)

        number = calculator_pb2.Number(value=81)
        response = stub.SquareRoot(number)
        print(response.value)
def run():
    # 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 = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.add(calculator_pb2.AddRequest(number1=3, number2=4))
    print(response.message)
Ejemplo n.º 3
0
def run():
    print("Input numbers 'x' and 'y' to calculate addition:")
    x = float(input("x: "))
    y = float(input("y: "))
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.Add(calculator_pb2.AddRequest(x=x, y=y))
    print("Addition result: " + str(response.result))
Ejemplo n.º 4
0
def run():
    # 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 = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.Add(calculator_pb2.CalculateRequest(num1=10, num2=15))
    print("Result = " + str(response.result))
Ejemplo n.º 5
0
def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = calculator_pb2_grpc.CalculatorStub(channel)
    a = int(input("Enter first number: "))
    b = int(input("Enter second number: "))
    response = stub.addNumbers(calculator_pb2.calculatorRequest(a=a, b=b))
    print("Addition of " + str(a) + " and " + str(b) + " is : " +
          str(response.sum))
Ejemplo n.º 6
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        a = int(input("Enter first value  :"))
        b = int(input("Enter second value :"))
        response = stub.add(calculator_pb2.addRequest(a=a, b=b))

    print("Addition of first value & second value :" + response.result)
Ejemplo n.º 7
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        print("This program will call add(x,y) as remote procedure")
        x = input("x=")
        y = input("y=")
        print("Calculator client sending x={} and y={}".format(x,y))
        response = stub.add(calculator_pb2.AddRequest(x=x, y=y))
        print("Calculator client received: " + str(response.result))
Ejemplo n.º 8
0
def get_call(loop, number):
    # open a gRPC channel
    channel = grpc.insecure_channel('localhost:50051')
    # create a stub (client)
    stub = calculator_pb2_grpc.CalculatorStub(channel)
    number = calculator_pb2.Number(value=number)
    response = stub.SquareRoot(number)
    print(response)
    return response
Ejemplo n.º 9
0
def run():

    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        value1 = input("Enter value 1:")
        value2 = input("Enter value 2:")
        response = stub.addition(
            calculator_pb2.AddRequest(val1=int(value1), val2=int(value2)))
    print("Sum of the two numbers is " + str(response.result))
Ejemplo n.º 10
0
def run():
    # 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:
    #channel: grpc.insecure_channel('localhost:50051')
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.Add(calculator_pb2.AddRequest(x=4,y=2))
    print("Addition request received and sum: {}".format(response.z))
Ejemplo n.º 11
0
    def __init__(self, channel=None):

        if not channel:
            # open a gRPC channel
            channel = grpc.insecure_channel('localhost:50051')
        else:
            channel = grpc.insecure_channel(channel)

        # create a stub (client)
        self.stub = calculator_pb2_grpc.CalculatorStub(channel)
Ejemplo n.º 12
0
def run():
    # 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 = calculator_pb2_grpc.CalculatorStub(channel)
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))
        response = stub.Add(calculator_pb2.AddRequest(a=num1, b=num2))
    print("Sum: %d" % response.sum)
Ejemplo n.º 13
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        number = calculator_pb2.Number(value1=99, value2=88)
        response = stub.Add(number)
    print(
        str(response.value1) + ' + ' + str(response.value2) + " = " +
        str(response.result))
    while True:
        time.sleep(_ONE_DAY_IN_SECONDS)
Ejemplo n.º 14
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        while 1:
            num_1 = int(input())
            num_2 = int(input())
            response = stub.CalculateSum(
                calculator_pb2.SumRequest(first_num=num_1, sec_num=num_2))
            print(response.sum)
            print('>>>>>')
Ejemplo n.º 15
0
def run():
    # 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 = calculator_pb2_grpc.CalculatorStub(channel)
        print("Insert first number : ")
        x = int(input())
        print("Insert second number : ")
        y = int(input())
        response = stub.add(
            calculator_pb2.CalculationRequest(input1=x, input2=y))
        print("Addition of " + str(x) + " and " + str(y) + " is : " +
              str(response.result))
Ejemplo n.º 16
0
def _calculate(server_address: Text, operation: calculator_pb2.Operation,
               a: float, b: float, plaintext: bool) -> float:
    if plaintext:
        channel = grpc.insecure_channel(server_address)
    else:
        channel = grpc.secure_channel(server_address,
                                      grpc.ssl_channel_credentials())
    try:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        request = calculator_pb2.BinaryOperation(first_operand=a,
                                                 second_operand=b,
                                                 operation=operation)
        return stub.Calculate(request).result
    finally:
        channel.close()
Ejemplo n.º 17
0
def square_root(val: float):

    # open a gRPC channel
    channel = grpc.insecure_channel('localhost:50051')

    # create a stub (client)
    stub = calculator_pb2_grpc.CalculatorStub(channel)

    # create a valid request message
    number = calculator_pb2.Number(value=val)

    # make the call
    response = stub.SquareRoot(number)

    # et voilà
    print(response.value)
Ejemplo n.º 18
0
def run():
    channel = grpc.insecure_channel('calculator_server:50051')
    stub = calculator_pb2_grpc.CalculatorStub(channel)

    # asks server to calculate 10 + 5
    response = stub.Calc(
        calculator_pb2.CalcRequest(number_1=10, number_2=5, operation='+'))
    print("Result: ", response.res)

    # asks server to calculate wrong operation
    response1 = stub.Calc(
        calculator_pb2.CalcRequest(number_1=10, number_2=5, operation='[+]'))
    print("Result: ", response1.res)

    # asks server to calculate 10 - 5
    response2 = stub.Calc(
        calculator_pb2.CalcRequest(number_1=10, number_2=5, operation='-'))
    print("Result: ", response2.res)
    channel.close()
Ejemplo n.º 19
0
def run():
    stop = False
    while not stop:
        usr_in = input("Do a calculation? [y/n]\n")
        if usr_in.lower() == 'n' or usr_in.lower() == 'no':
            stop = True
        else:
            usr_in = input("Please enter term to calculate!\n")

            with grpc.insecure_channel(ADDRESS_SERVER_MASTER) as channel:
                stub = calculator_pb2_grpc.CalculatorStub(channel)
                response = stub.Calculate(
                    calculator_pb2.CalculationRequest(expression=usr_in))

                if response.status == calculator_pb2.CalculationResponse.SUCCESS:
                    print("The result is : " + str(response.result) + '\n')
                elif response.status == calculator_pb2.CalculationResponse.ZERO_DIVISION_ERROR:
                    print("Error: Divide by zero in expression")
                else:
                    print("Invalid format of expression")
def run():
    channel = grpc.insecure_channel('localhost:50051')
    calc = calculator_pb2_grpc.CalculatorStub(channel)

    # Get inputs for operands
    while True:
        fstOperand = str(input('Please insert first operand:\n'))  # Get value
        if fstOperand.isnumeric():  # if valid, break
            fstOperand = float(fstOperand)
            break
        print('Invalid entry: %s, please enter a number' %
              (fstOperand))  # Err mssg
    while True:
        scdOperand = str(input('Please insert second operand:\n'))  # Get value
        if scdOperand.isnumeric():  # If valid, break
            scdOperand = float(scdOperand)
            break
        print('Invalid entry: %s, please enter a number' %
              (fstOperand))  # Err mssg

    print('1st OPERAND = %f\n2nd OPERAND = %f' % (fstOperand, scdOperand))

    print(
        '======================================================================'
    )

    # Addition
    tm_start = time.time()  # Get current time before calling RPC
    result = calc.Addition(
        calculator_pb2.AdditionRequest(fstOperandAdd=fstOperand,
                                       scdOperandAdd=scdOperand)).resultAdd
    tm_end = time.time()  # Get current time after calling RPC
    print('%f + %f = %f [%s]' %
          (fstOperand, scdOperand, result, tm_end - tm_start))  # print results

    # Subtraction
    tm_start = time.time()
    result = calc.Subtraction(
        calculator_pb2.SubtractionRequest(fstOperandSub=fstOperand,
                                          scdOperandSub=scdOperand)).resultSub
    tm_end = time.time()
    print('%f - %f = %f [%s]' %
          (fstOperand, scdOperand, result, tm_end - tm_start))

    # Multiplication
    tm_start = time.time()
    result = calc.Multiplication(
        calculator_pb2.MultiplicationRequest(
            fstOperandMul=fstOperand, scdOperandMul=scdOperand)).resultMul
    tm_end = time.time()
    print('%f * %f = %f [%s]' %
          (fstOperand, scdOperand, result, tm_end - tm_start))

    # Division
    tm_start = time.time()
    result = calc.Division(
        calculator_pb2.DivisionRequest(fstOperandDiv=fstOperand,
                                       scdOperandDiv=scdOperand)).resultDiv
    tm_end = time.time()
    print('%f / %f = %f [%s]' %
          (fstOperand, scdOperand, result, tm_end - tm_start))
Ejemplo n.º 21
0
import grpc

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')

# create a stub (client)
stub = calculator_pb2_grpc.CalculatorStub(channel)

# create a valid request message
msg = calculator_pb2.Message(msg="Alan")

# create a valid request message
number = calculator_pb2.Number(value=16)

# make the call
response = stub.HelloWorld(msg)

print(response.msg)

response = stub.SquareRoot(number)

print(response.value)


number2 = calculator_pb2.Number2(x=16, y=4)

# make the call
Ejemplo n.º 22
0
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.Add(calculator_pb2.AddRequest(x=n1, y=n2))
    print("Calculation result: " + str(response.result))
Ejemplo n.º 23
0
 def __init__(self, channel):
     self.stub = calculator_pb2_grpc.CalculatorStub(channel)
Ejemplo n.º 24
0
 def __init__(self):
     try:
         channel = grpc.insecure_channel(CONN_TARGET)
         self._stub = calculator_pb2_grpc.CalculatorStub(channel)
     except grpc.RpcError as e:
         logging.error(e)
Ejemplo n.º 25
0
def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = calculator_pb2_grpc.CalculatorStub(channel)
    response = stub.Add(calculator_pb2.AddRequest(n1=1, n2=2))
    print("1 + 2 = %d" % response.message)
Ejemplo n.º 26
0
def run():
	with grpc.insecure_channel('localhost:50052') as channel:
		#stub = calculator_pb2_grpc.CalculatorStub(channel)
		#response = stub.AddAB(calculator_pb2.NumberRequest(a=12,b=13))
		#print(response.value)
		print("Sum of a=12 and b =13 is : ", calculator_pb2_grpc.CalculatorStub(channel).AddAB(calculator_pb2.NumberRequest(a=12,b=13)).value)
Ejemplo n.º 27
0
# import the generated classes
import time

import calculator_pb2
import calculator_pb2_grpc
import grpc

clients = []

# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')

for i in range(500):
    # create a stub (client)
    clients.append(calculator_pb2_grpc.CalculatorStub(channel))

# create a valid request message
number = calculator_pb2.Number(value=16)

begintime = time.time()
for client in clients:
    # make the call
    response = client.SquareRoot(number)
    # et voilà
    # print(response.value)
duration = time.time() - begintime
print("took: %s sec." % duration)
def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = calculator_pb2_grpc.CalculatorStub(channel)
        response = stub.add(calculator_pb2.AddRequest(a=4, b=8))
        print(f"The sum of 4 and 8 is {response.c}")
Ejemplo n.º 29
0
import calculator_pb2
import calculator_pb2_grpc
import grpc

comm_line = grpc.insecure_channel('localhost:50051')

stub = calculator_pb2_grpc.CalculatorStub(comm_line)

inputs = calculator_pb2.InputNumber(num_one=10, num_two=15)

output = stub.AddNumber(inputs)

print(output.res_num)