Created
September 8, 2022 08:39
-
-
Save wfng92/7d3b51880d0f79b637b61501f6e48085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent import futures | |
import logging | |
import os | |
import grpc | |
from protos import hello_pb2, hello_pb2_grpc | |
def get_filepath(filename, extension): | |
return f'{filename}{extension}' | |
class Greeter(hello_pb2_grpc.GreeterServicer): | |
def SayHello(self, request, context): | |
return hello_pb2.StringResponse(message=f'Hello, {request.name}! Your age is {request.age}') | |
def UploadFile(self, request_iterator, context): | |
data = bytearray() | |
filepath = 'dummy' | |
for request in request_iterator: | |
if request.metadata.filename and request.metadata.extension: | |
filepath = get_filepath(request.metadata.filename, request.metadata.extension) | |
continue | |
data.extend(request.chunk_data) | |
with open(filepath, 'wb') as f: | |
f.write(data) | |
return hello_pb2.StringResponse(message='Success!') | |
def DownloadFile(self, request, context): | |
chunk_size = 1024 | |
filepath = f'{request.filename}{request.extension}' | |
if os.path.exists(filepath): | |
with open(filepath, mode="rb") as f: | |
while True: | |
chunk = f.read(chunk_size) | |
if chunk: | |
entry_response = hello_pb2.FileResponse(chunk_data=chunk) | |
yield entry_response | |
else: # The chunk was empty, which means we're at the end of the file | |
return | |
def serve(): | |
server = grpc.server(futures.ThreadPoolExecutor(max_workers=4)) | |
hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) | |
server.add_insecure_port('[::]:50051') | |
server.start() | |
server.wait_for_termination() | |
if __name__ == '__main__': | |
logging.basicConfig() | |
serve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment