Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 1 | # Copyright 2015 gRPC authors. |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 2 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 6 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 14 | """The Python implementation of the GRPC helloworld.Greeter server.""" |
| 15 | |
Nathaniel Manista | 46585e2 | 2016-07-15 22:33:50 +0000 | [diff] [blame] | 16 | from concurrent import futures |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 17 | import time |
| 18 | |
Nathaniel Manista | 46585e2 | 2016-07-15 22:33:50 +0000 | [diff] [blame] | 19 | import grpc |
| 20 | |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 21 | import helloworld_pb2 |
Nathaniel Manista | c15ee83 | 2016-12-15 22:58:29 +0000 | [diff] [blame] | 22 | import helloworld_pb2_grpc |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 23 | |
| 24 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
| 25 | |
| 26 | |
Nathaniel Manista | c15ee83 | 2016-12-15 22:58:29 +0000 | [diff] [blame] | 27 | class Greeter(helloworld_pb2_grpc.GreeterServicer): |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 28 | |
ncteisen | 848a749 | 2017-12-12 10:31:47 -0800 | [diff] [blame] | 29 | def SayHello(self, request, context): |
| 30 | return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def serve(): |
ncteisen | 848a749 | 2017-12-12 10:31:47 -0800 | [diff] [blame] | 34 | server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) |
| 35 | helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) |
| 36 | server.add_insecure_port('[::]:50051') |
| 37 | server.start() |
| 38 | try: |
| 39 | while True: |
| 40 | time.sleep(_ONE_DAY_IN_SECONDS) |
| 41 | except KeyboardInterrupt: |
| 42 | server.stop(0) |
| 43 | |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 44 | |
| 45 | if __name__ == '__main__': |
ncteisen | 848a749 | 2017-12-12 10:31:47 -0800 | [diff] [blame] | 46 | serve() |