14 """Client of the Python AsyncIO example of customizing authentication mechanism."""
23 helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services(
26 _LOGGER = logging.getLogger(__name__)
27 _LOGGER.setLevel(logging.INFO)
29 _SERVER_ADDR_TEMPLATE =
'localhost:%d'
30 _SIGNATURE_HEADER_KEY =
'x-signature'
35 def __call__(self, context: grpc.AuthMetadataContext,
37 """Implements authentication by passing metadata to a callback.
39 Implementations of this method must not block.
42 context: An AuthMetadataContext providing information on the RPC that
43 the plugin is being called to authenticate.
44 callback: An AuthMetadataPluginCallback to be invoked either
45 synchronously or asynchronously.
51 signature = context.method_name[::-1]
52 callback(((_SIGNATURE_HEADER_KEY, signature),),
None)
61 _credentials.ROOT_CERTIFICATE)
67 channel = grpc.aio.secure_channel(addr, composite_credentials)
71 async
def send_rpc(channel: grpc.aio.Channel) -> helloworld_pb2.HelloReply:
75 response = await stub.SayHello(request)
77 _LOGGER.error(
'Received error: %s', rpc_error)
80 _LOGGER.info(
'Received message: %s', response)
85 parser = argparse.ArgumentParser()
86 parser.add_argument(
'--port',
90 help=
'the address of server')
91 args = parser.parse_args()
98 if __name__ ==
'__main__':
99 logging.basicConfig(level=logging.INFO)