Skip to content

Instantly share code, notes, and snippets.

@wfng92
Created September 8, 2022 08:36
Show Gist options
  • Save wfng92/975636f5bcde123cb6bdf9258a2df943 to your computer and use it in GitHub Desktop.
Save wfng92/975636f5bcde123cb6bdf9258a2df943 to your computer and use it in GitHub Desktop.
class Greeter(hello_pb2_grpc.GreeterServicer):
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment