Esempio n. 1
0
def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = hello_pb2_grpc.HelloServiceStub(channel)
    # ideally, you should have try catch block here too
    response = stub.SayHello(hello_pb2.HelloReq(Name='Euler'))
    print(response.Result)

    try:
        response = stub.SayHelloStrict(
            hello_pb2.HelloReq(Name='Leonhard Euler'))
    except grpc.RpcError as e:
        # ouch!
        # lets print the gRPC error message
        # which is "Length of `Name` cannot be more than 10 characters"
        print(e.details())
        # lets access the error code, which is `INVALID_ARGUMENT`
        # `type` of `status_code` is `grpc.StatusCode`
        status_code = e.code()
        # should print `INVALID_ARGUMENT`
        print(status_code.name)
        # should print `(3, 'invalid argument')`
        print(status_code.value)
        # want to do some specific action based on the error?
        if grpc.StatusCode.INVALID_ARGUMENT == status_code:
            # do your stuff here
            pass
    else:
        print(response.Result)
Esempio n. 2
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:1234') as channel:
        stub = hello_pb2_grpc.HelloServiceStub(channel)
        response = stub.Hello(hello_pb2.String(value='you'))
        print("Hello Service client received: " + response.value)
Esempio n. 3
0
#!/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}\"")
Esempio n. 4
0
def do_request():
    msg = "bikibiki"
    with grpc.insecure_channel("localhost:50051") as channel:
        stub = hello_pb2_grpc.HelloServiceStub(channel)
        stub.Hello(hello_pb2.MsgRequest(message=msg))
Esempio n. 5
0
 def __init__(self):
     channel = grpc.insecure_channel(address + ':' + str(port))
     self.stub = hello_grpc.HelloServiceStub(channel)
     t = threading.Thread(target=self.__listen_for_messages)
     t.start()
Esempio n. 6
0
def run():

    with grpc.insecure_channel('localhost:50051') as channel:
        stub = hello_grpc.HelloServiceStub(channel)
        print("-------------- BidirectionalHello --------------")
        getBidiHelloResponse(stub)