This commit is contained in:
Maximilian Wagner
2024-01-30 20:04:56 +01:00
parent 64318902b4
commit 2c6c21b761
22 changed files with 1620 additions and 93 deletions

View File

@@ -0,0 +1,25 @@
# Memory efficient file transfer
#
# Copyright 2021 (c) Erik de Lange
# Released under MIT license
_buffer = bytearray(512) # adjust size to your systems available memory
_bmview = memoryview(_buffer) # reuse pre-allocated _buffer
def sendfile(conn, filename):
""" Send a file to a connection in chunks - lowering memory usage.
:param socket conn: connection to send the file content to
:param str filename: name of file the send
"""
try:
with open(filename, "rb") as fp:
while True:
n = fp.readinto(_buffer)
if n == 0:
break
conn.write(_bmview[:n])
except:
print(f"WEB:File {filename} not found")