-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
55 lines (44 loc) · 2.26 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
# flake8: noqa
from concurrent import futures
import os
import io
from collections import defaultdict
import logging
import requests
import grpc
import ImageService_pb2 as service
import ImageService_pb2_grpc as rpc
CLIENT_ID = os.environ['IMGUR_CLIENT_ID']
class ImageServiceServer(rpc.ImageServiceServicer): # inheriting here from the protobuf rpc file which is generated
def __init__(self):
self.images = defaultdict(io.BytesIO)
logging.info("successfuly created the images store")
def Upload(self, request_iterator, context):
for request in request_iterator:
if request.StatusCode == service.ImageUploadStatusCode.InProgress:
logging.info(f'> {request.Id} - receiving image')
self.images[request.Id].write(request.Content)
result = service.ImageUploadResponse(Id=request.Id, StatusCode=service.ImageUploadStatusCode.Ok, Message='waiting for more')
if request.StatusCode == service.ImageUploadStatusCode.Ok and not request.Content:
logging.info('transfer completed!')
logging.info(f'> {request.Id} - sending image')
image = self.images[request.Id].getvalue()
payload = {'image': image}
headers = {'Authorization': f'Client-ID {CLIENT_ID}'}
url = "https://api.imgur.com/3/image"
response = requests.request("POST", url, headers=headers, data=payload, files=[])
response.raise_for_status()
logging.info(f'> {request.Id} - deleting image from mem')
del self.images[request.Id]
logging.info(f'> {request.Id} - returning status')
result = service.ImageUploadResponse(Id=request.Id, StatusCode=service.ImageUploadStatusCode.Ok, Message=response.text.encode('utf8'))
return result
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
port = 22222
grpc_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
rpc.add_ImageServiceServicer_to_server(ImageServiceServer(), grpc_server)
logging.info(f'Starting server. Listening at {port}...')
grpc_server.add_insecure_port(f'[::]:{port}')
grpc_server.start()
grpc_server.wait_for_termination()