Skip to content

Commit fe2d057

Browse files
author
Nikos Michalakis
committed
Implement python Greeter server.
(assumes that grpcio is installed already)
1 parent 04d8636 commit fe2d057

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

examples/helloworld/python/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ py_binary(
1414
"//examples/helloworld/proto:py",
1515
],
1616
)
17+
18+
py_binary(
19+
name = "greeter_server",
20+
srcs = [
21+
"greeter_server.py",
22+
"//examples/proto:py",
23+
"//examples/helloworld/proto:py",
24+
],
25+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2015, Google Inc.
2+
# All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are
6+
# met:
7+
#
8+
# * Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above
11+
# copyright notice, this list of conditions and the following disclaimer
12+
# in the documentation and/or other materials provided with the
13+
# distribution.
14+
# * Neither the name of Google Inc. nor the names of its
15+
# contributors may be used to endorse or promote products derived from
16+
# this software without specific prior written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
"""The Python implementation of the GRPC helloworld.Greeter server."""
31+
32+
from __future__ import print_function
33+
34+
import grpc
35+
import time
36+
37+
from concurrent import futures
38+
from examples.helloworld.proto import helloworld_pb2
39+
40+
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
41+
42+
43+
class _GreeterServer(object):
44+
45+
def __init__(self, greeter_service, server_port):
46+
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
47+
helloworld_pb2.add_GreeterServicer_to_server(greeter_service, self.server)
48+
self.server.add_insecure_port('[::]:{server_port}'.format(server_port=server_port))
49+
50+
def start(self):
51+
self.server.start()
52+
53+
def stop(self):
54+
self.server.stop(0)
55+
56+
def await_termination(self):
57+
"""
58+
server.start() doesn't block so we explicitly block here unless someone keyboard-exits us.
59+
:return:
60+
"""
61+
try:
62+
while True:
63+
time.sleep(_ONE_DAY_IN_SECONDS)
64+
except KeyboardInterrupt:
65+
self.server.stop(0)
66+
pass
67+
68+
69+
class _GreeterService(helloworld_pb2.GreeterServicer):
70+
71+
def SayHello(self, hello_request, context):
72+
print("Greeter server received: " + hello_request.name)
73+
hello_reply = helloworld_pb2.HelloReply()
74+
hello_reply.message = 'Hello {name}'.format(name=hello_request.name)
75+
return hello_reply
76+
77+
78+
def main():
79+
greeter_server = _GreeterServer(_GreeterService(), 50051)
80+
greeter_server.start()
81+
greeter_server.await_termination()
82+
83+
if __name__ == '__main__':
84+
main()

0 commit comments

Comments
 (0)