src/python/grpcio_tests/tests/interop/server.py
Go to the documentation of this file.
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 
16 import argparse
17 from concurrent import futures
18 import logging
19 
20 import grpc
21 
22 from src.proto.grpc.testing import test_pb2_grpc
23 from tests.interop import resources
24 from tests.interop import service
25 from tests.unit import test_common
26 
27 logging.basicConfig()
28 _LOGGER = logging.getLogger(__name__)
29 
30 
32  parser = argparse.ArgumentParser()
33  parser.add_argument('--port',
34  type=int,
35  required=True,
36  help='the port on which to serve')
37  parser.add_argument('--use_tls',
38  default=False,
39  type=resources.parse_bool,
40  help='require a secure connection')
41  parser.add_argument('--use_alts',
42  default=False,
43  type=resources.parse_bool,
44  help='require an ALTS connection')
45  return parser.parse_args()
46 
47 
49  if use_tls:
50  private_key = resources.private_key()
51  certificate_chain = resources.certificate_chain()
52  return grpc.ssl_server_credentials(((private_key, certificate_chain),))
53  else:
55 
56 
57 def serve():
59 
60  server = test_common.test_server()
61  test_pb2_grpc.add_TestServiceServicer_to_server(service.TestService(),
62  server)
63  if args.use_tls or args.use_alts:
64  credentials = get_server_credentials(args.use_tls)
65  server.add_secure_port('[::]:{}'.format(args.port), credentials)
66  else:
67  server.add_insecure_port('[::]:{}'.format(args.port))
68 
69  server.start()
70  _LOGGER.info('Server serving.')
71  server.wait_for_termination()
72  _LOGGER.info('Server stopped; exiting.')
73 
74 
75 if __name__ == '__main__':
76  serve()
http2_test_server.format
format
Definition: http2_test_server.py:118
grpc.alts_server_credentials
def alts_server_credentials()
Definition: src/python/grpcio/grpc/__init__.py:1914
tests.interop.server.get_server_credentials
def get_server_credentials(use_tls)
Definition: src/python/grpcio_tests/tests/interop/server.py:48
grpc.ssl_server_credentials
def ssl_server_credentials(private_key_certificate_chain_pairs, root_certificates=None, require_client_auth=False)
Definition: src/python/grpcio/grpc/__init__.py:1709
tests.interop.server.parse_interop_server_arguments
def parse_interop_server_arguments()
Definition: src/python/grpcio_tests/tests/interop/server.py:31
tests.interop.service.TestService
Definition: src/python/grpcio_tests/tests/interop/service.py:49
tests.unit
Definition: src/python/grpcio_tests/tests/unit/__init__.py:1
tests.interop
Definition: src/python/grpcio_tests/tests/interop/__init__.py:1
tests.interop.server.serve
def serve()
Definition: src/python/grpcio_tests/tests/interop/server.py:57


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:11