Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a limit_bandwidth parameter to get and put function. #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __exit__(self, type, value, traceback):
self.close()

def put(self, files, remote_path=b'.',
recursive=False, preserve_times=False):
recursive=False, preserve_times=False, limit_bandwidth=0):
"""
Transfer files to remote host.

Expand All @@ -135,12 +135,16 @@ def put(self, files, remote_path=b'.',
@param preserve_times: preserve mtime and atime of transfered files
and directories.
@type preserve_times: bool
@param limit_bandwidth: Limits the used bandwidth, specified in Kbit/s.
@type limit_bandwidth: int
"""
self.preserve_times = preserve_times
self.channel = self._open()
self._pushed = 0
self.channel.settimeout(self.socket_timeout)
scp_command = (b'scp -t ', b'scp -r -t ')[recursive]
scp_command = b'scp -t '
scp_command += (b'', b' -r ')[recursive]
scp_command += (b'', b' -l '+ str(limit_bandwidth).encode() + b' ')[limit_bandwidth > 0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use a proper if here?

if limit_bandwidth > 0:
    scp_command += b' -l ' + str(limit_bandwidth).encode() + b' '

self.channel.exec_command(scp_command +
self.sanitize(asbytes(remote_path)))
self._recv_confirm()
Expand All @@ -156,7 +160,7 @@ def put(self, files, remote_path=b'.',
self.close()

def get(self, remote_path, local_path='',
recursive=False, preserve_times=False):
recursive=False, preserve_times=False, limit_bandwidth=0):
"""
Transfer files from remote host to localhost

Expand All @@ -171,6 +175,8 @@ def get(self, remote_path, local_path='',
@param preserve_times: preserve mtime and atime of transfered files
and directories.
@type preserve_times: bool
@param limit_bandwidth: Limits the used bandwidth, specified in Kbit/s.
@type limit_bandwidth: int
"""
if not isinstance(remote_path, (list, tuple)):
remote_path = [remote_path]
Expand All @@ -187,12 +193,14 @@ def get(self, remote_path, local_path='',
asunicode(self._recv_dir))
rcsv = (b'', b' -r')[recursive]
prsv = (b'', b' -p')[preserve_times]
lmbw = (b'', b' -l '+ str(limit_bandwidth).encode())[limit_bandwidth > 0]
self.channel = self._open()
self._pushed = 0
self.channel.settimeout(self.socket_timeout)
self.channel.exec_command(b"scp" +
rcsv +
prsv +
lmbw +
b" -f " +
b' '.join(remote_path))
self._recv_all()
Expand Down
18 changes: 18 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ def test_up_and_down(self):
finally:
os.chdir(previous)

def test_up_and_down_with_limit(self):
'''send and receive files with the same client'''
previous = os.getcwd()
testfile = os.path.join(self._temp, 'testfile')
testfile_sent = os.path.join(self._temp, 'testfile_sent')
testfile_rcvd = os.path.join(self._temp, 'testfile_rcvd')
try:
os.chdir(self._temp)
with open(testfile, 'w') as f:
f.write("TESTING\n")
with SCPClient(self.ssh.get_transport(), socket_timeout=60.0) as scp:
scp.put(testfile, testfile_sent, limit_bandwidth=10)
scp.get(testfile_sent, testfile_rcvd, limit_bandwidth=10)

assert open(testfile_rcvd).read() == 'TESTING\n'
finally:
os.chdir(previous)


if __name__ == '__main__':
unittest.main()