14 """The Python implementation of the GRPC helloworld.Greeter server."""
17 from concurrent
import futures
27 import helloworld_pb2_grpc
29 _DESCRIPTION =
"A general purpose phony server."
31 _LISTEN_HOST =
"0.0.0.0"
33 _THREAD_POOL_SIZE = 256
35 logger = logging.getLogger()
36 console_handler = logging.StreamHandler()
37 formatter = logging.Formatter(fmt=
'%(asctime)s: %(levelname)-8s %(message)s')
38 console_handler.setFormatter(formatter)
39 logger.addHandler(console_handler)
45 self.
_hostname = hostname
if hostname
else socket.gethostname()
47 def SayHello(self, request: helloworld_pb2.HelloRequest,
50 message=f
"Hello {request.name} from {self._hostname}!")
54 maintenance_port: int) ->
None:
55 listen_address = f
"{_LISTEN_HOST}:{maintenance_port}"
56 server.add_insecure_port(listen_address)
60 health_servicer = health.HealthServicer(
61 experimental_non_blocking=
True,
62 experimental_thread_pool=futures.ThreadPoolExecutor(
63 max_workers=_THREAD_POOL_SIZE))
68 for service
in helloworld_pb2.DESCRIPTOR.services_by_name.values()) + (
69 reflection.SERVICE_NAME, health.SERVICE_NAME)
72 health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
73 for service
in services:
74 health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
75 reflection.enable_server_reflection(services, server)
82 listen_address = f
"{_LISTEN_HOST}:{port}"
84 server.add_insecure_port(listen_address)
87 logger.info(
"Running with xDS Server credentials")
92 server.add_secure_port(listen_address, server_creds)
95 def serve(port: int, hostname: str, maintenance_port: int,
96 secure_mode: bool) ->
None:
97 if port == maintenance_port:
100 futures.ThreadPoolExecutor(max_workers=_THREAD_POOL_SIZE))
104 logger.info(
"Greeter server listening on port %d", port)
105 logger.info(
"Maintenance server listening on port %d", maintenance_port)
106 server.wait_for_termination()
110 futures.ThreadPoolExecutor(max_workers=_THREAD_POOL_SIZE),
113 greeter_server.start()
114 logger.info(
"Greeter server listening on port %d", port)
116 futures.ThreadPoolExecutor(max_workers=_THREAD_POOL_SIZE))
118 maintenance_server.start()
119 logger.info(
"Maintenance server listening on port %d", maintenance_port)
120 greeter_server.wait_for_termination()
121 maintenance_server.wait_for_termination()
124 if __name__ ==
'__main__':
125 parser = argparse.ArgumentParser(description=_DESCRIPTION)
126 parser.add_argument(
"port",
130 help=
"The port on which to listen.")
131 parser.add_argument(
"hostname",
135 help=
"The name clients will see in responses.")
139 help=
"If specified, uses xDS credentials to connect to the server.")
140 args = parser.parse_args()
141 logging.basicConfig()
142 logger.setLevel(logging.INFO)
143 serve(args.port, args.hostname, args.port + 1, args.xds_creds)