def run_should_fail(): with grpc.insecure_channel('localhost:50051') as channel: stub = echo_pb2_grpc.EchoStub(channel) print("================= should fail with 'Access denied!' ====================") try: expand(stub) except: print(sys.exc_info())
def run_should_pass(): header_adder_interceptor = client_interceptor.header_adder_interceptor( 'one-time-password', '42') with grpc.insecure_channel('localhost:50051') as channel: intercept_channel = grpc.intercept_channel(channel, header_adder_interceptor) stub = echo_pb2_grpc.EchoStub(intercept_channel) print("================= should pass ====================") expand(stub)
def run(): with grpc.insecure_channel('localhost:50051') as channel: stub = echo_pb2_grpc.EchoStub(channel) print("-------------- Echo --------------") echo(stub) print("-------------- Expand --------------") expand(stub) print("-------------- Collect --------------") collect(stub) print("-------------- Chat --------------") chat(stub)
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 = echo_pb2_grpc.EchoStub(channel) # response = stub.SayHello(echo_pb2.HelloRequest(name='you')) # print("Greeter client received: " + response.message) value = "payload (test message)" request = echo_pb2.EchoRequest(message=value) response = stub.Reply(request) print(f"Response from the server: '{response.message}'")
def __init__(self, host='0.0.0.0', port=PORT): self.channel = grpc.insecure_channel('%s:%d' % (host, port)) self.stub = echo_pb2_grpc.EchoStub(self.channel)
import grpc # import the generated classes import echo_pb2 import echo_pb2_grpc # open a gRPC channel channel = grpc.insecure_channel('localhost:50051') # create a stub (client) stub = echo_pb2_grpc.EchoStub(channel) # create a valid request message text = echo_pb2.Set(value='Hello, world!', number=12) # make the call response = stub.Response(text) print(response.value) print(response.number)
def __init__(self, username, host='localhost', port=5000): self.username = username self.host = host self.port = port channel = grpc.insecure_channel('{}:{}'.format(self.host, self.port)) self.stub = echo_pb2_grpc.EchoStub(channel)