Skip to content
12 changes: 9 additions & 3 deletions paramiko/sftp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

from paramiko.sftp_attr import SFTPAttributes
from paramiko.ssh_exception import SSHException
from paramiko.sftp_file import SFTPFile
from paramiko.sftp_file import SFTPFile, MAX_REQUEST_SIZE
from paramiko.util import ClosingContextManager


Expand Down Expand Up @@ -323,7 +323,9 @@ def listdir_iter(self, path=".", read_aheads=50):
self._request(CMD_CLOSE, handle)
return

def open(self, filename, mode="r", bufsize=-1):
def open(
self, filename, mode="r", bufsize=-1, max_request_size=MAX_REQUEST_SIZE
):
"""
Open a file on the remote server. The arguments are the same as for
Python's built-in `python:file` (aka `python:open`). A file-like
Expand Down Expand Up @@ -351,6 +353,10 @@ def open(self, filename, mode="r", bufsize=-1):
:param str filename: name of the file to open
:param str mode: mode (Python-style) to open in
:param int bufsize: desired buffering (-1 = default buffer size)
:param int max_request_size:
size of read/write requests on the file some ftp servers will choke
if you send read/write requests larger than 32768

:return: an `.SFTPFile` object representing the open file

:raises: ``IOError`` -- if the file could not be opened.
Expand Down Expand Up @@ -379,7 +385,7 @@ def open(self, filename, mode="r", bufsize=-1):
filename, mode, u(hexlify(handle))
),
)
return SFTPFile(self, handle, mode, bufsize)
return SFTPFile(self, handle, mode, bufsize, max_request_size)

# Python continues to vacillate about "open" vs "file"...
file = open
Expand Down
26 changes: 17 additions & 9 deletions paramiko/sftp_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
)
from paramiko.sftp_attr import SFTPAttributes

# Some sftp servers will choke if you send read/write requests larger than
# this size.
MAX_REQUEST_SIZE = 32768


class SFTPFile(BufferedFile):
"""
Expand All @@ -54,11 +58,15 @@ class SFTPFile(BufferedFile):
that built-in Python file objects are.
"""

# Some sftp servers will choke if you send read/write requests larger than
# this size.
MAX_REQUEST_SIZE = 32768

def __init__(self, sftp, handle, mode="r", bufsize=-1):
def __init__(
self,
sftp,
handle,
mode="r",
bufsize=-1,
max_request_size=MAX_REQUEST_SIZE,
):
self.max_request_size = max_request_size
BufferedFile.__init__(self)
self.sftp = sftp
self.handle = handle
Expand Down Expand Up @@ -177,7 +185,7 @@ def _read_prefetch(self, size):
return prefetch

def _read(self, size):
size = min(size, self.MAX_REQUEST_SIZE)
size = min(size, self.max_request_size)
if self._prefetching:
data = self._read_prefetch(size)
if data is not None:
Expand All @@ -191,7 +199,7 @@ def _read(self, size):

def _write(self, data):
# may write less than requested if it would exceed max packet size
chunk = min(len(data), self.MAX_REQUEST_SIZE)
chunk = min(len(data), self.max_request_size)
sftp_async_request = self.sftp._async_request(
type(None),
CMD_WRITE,
Expand Down Expand Up @@ -469,7 +477,7 @@ def prefetch(self, file_size=None):
chunks = []
n = self._realpos
while n < file_size:
chunk = min(self.MAX_REQUEST_SIZE, file_size - n)
chunk = min(self.max_request_size, file_size - n)
chunks.append((n, chunk))
n += chunk
if len(chunks) > 0:
Expand Down Expand Up @@ -503,7 +511,7 @@ def readv(self, chunks):

# break up anything larger than the max read size
while size > 0:
chunk_size = min(size, self.MAX_REQUEST_SIZE)
chunk_size = min(size, self.max_request_size)
read_chunks.append((offset, chunk_size))
offset += chunk_size
size -= chunk_size
Expand Down
35 changes: 35 additions & 0 deletions tests/test_sftp_big.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,38 @@ def test_A_big_file_renegotiate(self, sftp):
finally:
sftp.remove("%s/hongry.txt" % sftp.FOLDER)
t.packetizer.REKEY_BYTES = pow(2, 30)

def test_2_big_file_max_request_size(self, sftp):
"""
write a 1MB file with no buffering.
"""
kblob = 1024 * b"x"
start = time.time()
try:
with sftp.open(
"%s/hongry.txt" % sftp.FOLDER, "w", max_request_size=16384
) as f:
for n in range(1024):
f.write(kblob)
if n % 128 == 0:
sys.stderr.write(".")
sys.stderr.write(" ")

assert (
sftp.stat("%s/hongry.txt" % sftp.FOLDER).st_size == 1024 * 1024
)
end = time.time()
sys.stderr.write("%ds " % round(end - start))

start = time.time()
with sftp.open(
"%s/hongry.txt" % sftp.FOLDER, "r", max_request_size=16384
) as f:
for n in range(1024):
data = f.read(1024)
assert data == kblob

end = time.time()
sys.stderr.write("%ds " % round(end - start))
finally:
sftp.remove("%s/hongry.txt" % sftp.FOLDER)