-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc-server.py
42 lines (35 loc) · 1.3 KB
/
grpc-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
#!/usr/bin/env python3
from concurrent import futures
from PIL import Image
import io,base64
import grpc
import lab6_pb2
import lab6_pb2_grpc
class Server(lab6_pb2_grpc.Lab6grpcServicer):
def __init__(self):
pass
def doAdd(self, request, context):
print(f"Receive doAdd({request.a},{request.b})")
return lab6_pb2.addReply(c = request.a+request.b )
def doDotproduct(self, request, context):
print(f"Receive doDotproduct")
c = 0
for a,b in zip(request.a,request.b):
c += a*b
return lab6_pb2.dotproductReply(c = c)
def doRawimage(self,request,context):
print(f"Receive raw image")
img = Image.open(io.BytesIO(request.img))
return lab6_pb2.imageReply(width=img.size[0],height=img.size[1])
def doJsonimage(self,request,context):
print(f"Receive json image")
img = Image.open(io.BytesIO(base64.b64decode(request.img.encode())))
return lab6_pb2.imageReply(width=img.size[0],height=img.size[1])
def serve():
print("hi im server")
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
lab6_pb2_grpc.add_Lab6grpcServicer_to_server(Server(), server)
server.add_insecure_port('[::]:9999')
server.start()
server.wait_for_termination()
serve()