1# Copyright 2023 The gRPC Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Client of the Python example of TLS.""" 15 16import logging 17 18import _credentials 19import grpc 20 21helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services( 22 "helloworld.proto" 23) 24 25_LOGGER = logging.getLogger(__name__) 26_LOGGER.setLevel(logging.INFO) 27 28_PORT = 50051 29_SERVER_ADDR_TEMPLATE = "localhost:%d" 30 31 32def send_rpc(stub): 33 request = helloworld_pb2.HelloRequest(name="you") 34 try: 35 response = stub.SayHello(request) 36 except grpc.RpcError as rpc_error: 37 _LOGGER.error("Received error: %s", rpc_error) 38 return rpc_error 39 else: 40 _LOGGER.info("Received message: %s", response) 41 return response 42 43 44def main(): 45 channel_credential = grpc.ssl_channel_credentials( 46 _credentials.ROOT_CERTIFICATE 47 ) 48 with grpc.secure_channel( 49 _SERVER_ADDR_TEMPLATE % _PORT, channel_credential 50 ) as channel: 51 stub = helloworld_pb2_grpc.GreeterStub(channel) 52 send_rpc(stub) 53 54 55if __name__ == "__main__": 56 logging.basicConfig(level=logging.INFO) 57 main() 58