forked from ramananbalakrishnan/basic-grpc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
68 lines (49 loc) · 1.81 KB
/
Copy pathserver.py
File metadata and controls
68 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import grpc
from concurrent import futures
import time
from proto import calculator_pb2
from proto import calculator_pb2_grpc
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):
def SquareRoot(self, request, context):
response = calculator_pb2.Number()
response.value = (request.value) * (request.value)
return response
def ReceiveEvents(self, request, context):
for x in range(10):
yield calculator_pb2.Number(value=x)
time.sleep(0.3)
def SendEvents(self, request_iterator, context):
count = 0
for req in request_iterator:
print("Receive: %s, context: %s" % (req.value, context))
count += 1
return calculator_pb2.Number(value=count)
def Chat(self, request_iterator, context):
for req in request_iterator:
yield calculator_pb2.Number(value=(req.value + 11))
def get_credentials():
try:
with open('server.key', 'rb') as f:
private_key = f.read()
with open('server.crt', 'rb') as f:
public_key = f.read()
except IOError:
return None
return grpc.ssl_server_credentials(((private_key, public_key, ), ))
def main():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
print('Starting server. Listening on insecure port 50051.')
server.add_insecure_port('[::]:50051')
credentials = get_credentials()
if credentials:
print('Starting server. Listening on secure port 50050.')
server.add_secure_port('[::]:50050', credentials)
server.start()
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
main()