-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
84 lines (72 loc) · 2.48 KB
/
server.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from concurrent import futures
import grpc
import calc_pb2
import calc_pb2_grpc
import hashlib
class CalcerServicer(calc_pb2_grpc.CalcerServicer):
def Add(self, request, context):
print("ADD received:")
print(request)
res = request.a + request.b
response = calc_pb2.CalcReply()
response.c = res
return response
def Subtract(self, request, context):
print("SUBTRACT received:")
print(request)
res = request.a - request.b
response = calc_pb2.CalcReply()
response.c = res
return response
def Multiply(self, request, context):
print("MULTIPLY received:")
print(request)
res = request.a * request.b
response = calc_pb2.CalcReply()
response.c = res
return response
def Divide(self, request, context):
print("DIVIDE received:")
print(request)
res = request.a / request.b
response = calc_pb2.CalcReplyF()
response.c = res
return response
def MultiPlus(self, request_iterator, context):
sum = 0
count = 1
print(f"Receiving MULTIPLUS stream from {context.peer()}...")
for request in request_iterator:
print(f"This is request {request.counter} from {context.peer()}, my internal counter is {count}, the request was {request.a}")
sum += request.a
count += 1
response = calc_pb2.CalcReply()
response.c = sum
return response
def InteractiveMultiPlus(self, request_iterator, context):
sum = 0
count = 1
for request in request_iterator:
print(f"This is request {request.counter} from {context.peer()}, my internal counter is {count}, the request was {request.a}")
sum += request.a
count += 1
response = calc_pb2.CalcReply()
response.c = sum
yield response
def HashFile(self, request, context):
file = request.file
m = hashlib.sha256()
m.update(file)
h = m.hexdigest()
response = calc_pb2.HashResponse()
response.hash = h
return response
def serve():
print("Starting GRPC Example Server")
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calc_pb2_grpc.add_CalcerServicer_to_server(CalcerServicer(), server)
server.add_insecure_port("localhost:50051")
server.start()
server.wait_for_termination()
if __name__ == "__main__":
serve()