1# Copyright 2015 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"""The Python implementation of the GRPC interoperability test server.""" 15 16import argparse 17from concurrent import futures 18import logging 19import time 20 21import grpc 22from src.proto.grpc.testing import test_pb2_grpc 23 24from tests.interop import methods 25from tests.interop import resources 26from tests.unit import test_common 27 28logging.basicConfig() 29_ONE_DAY_IN_SECONDS = 60 * 60 * 24 30_LOGGER = logging.getLogger(__name__) 31 32 33def serve(): 34 parser = argparse.ArgumentParser() 35 parser.add_argument( 36 '--port', type=int, required=True, help='the port on which to serve') 37 parser.add_argument( 38 '--use_tls', 39 default=False, 40 type=resources.parse_bool, 41 help='require a secure connection') 42 args = parser.parse_args() 43 44 server = test_common.test_server() 45 test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(), 46 server) 47 if args.use_tls: 48 private_key = resources.private_key() 49 certificate_chain = resources.certificate_chain() 50 credentials = grpc.ssl_server_credentials(((private_key, 51 certificate_chain),)) 52 server.add_secure_port('[::]:{}'.format(args.port), credentials) 53 else: 54 server.add_insecure_port('[::]:{}'.format(args.port)) 55 56 server.start() 57 _LOGGER.info('Server serving.') 58 try: 59 while True: 60 time.sleep(_ONE_DAY_IN_SECONDS) 61 except BaseException as e: 62 _LOGGER.info('Caught exception "%s"; stopping server...', e) 63 server.stop(None) 64 _LOGGER.info('Server stopped; exiting.') 65 66 67if __name__ == '__main__': 68 serve() 69