Пример #1
0
def run_example():
    print("hello world from grpcio service_client!")
    with grpc.insecure_channel("localhost:50051") as channel:
        stub = service_pb2_grpc.ExampleServiceStub(channel)
        try:
            hello_response = stub.Hello(service_pb2.HelloRequest(action="hello"))
            bye_response = stub.Hello(service_pb2.HelloRequest(action="bye"))
        except grpc.RpcError as error:
            if error.code() == grpc.StatusCode.UNAVAILABLE:
                print(
                    "[ERROR] Connection to server is unavailable. You should create a server instance first."
                )
                print(
                    "To start a gRPC server, execute: `./pants run examples/src/python/example/grpcio/server`"
                )
            else:
                print(
                    "An error occured! Error code: [{}] Error details: [{}]".format(
                        error.code(), error.details()
                    )
                )
        else:
            print(hello_response)
            print(bye_response)
            print("[SUCCESS]")
Пример #2
0
def run_example():
    print('hello world from grpcio imports_client!')
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = imports_pb2_grpc.ImportsServiceStub(channel)
        try:

            hello_request = service_pb2.HelloRequest(
                action='hello with imports')
            request = imports_pb2.HelloImportsRequest(
                hello_request=hello_request)
            reply = stub.HelloImports(request)
        except grpc.RpcError as error:
            if error.code() == grpc.StatusCode.UNAVAILABLE:
                print(
                    "[ERROR] Connection to server is unavailable. You should create a server instance first."
                )
                print(
                    "To start a gRPC server, execute: `./pants run examples/src/python/example/grpcio/server`"
                )
            else:
                print('An error occured! Error code: [{}] Error details: [{}]'.
                      format(error.code(), error.details()))
        else:
            print(reply.hello_reply.response)
            print('[SUCCESS]')