Skip to main content

scp module for paramiko

Project description

The scp.py module uses a paramiko transport to send and recieve files via the scp1 protocol. This is the protocol as referenced from the openssh scp program, and has only been tested with this implementation.

Example

from paramiko import SSHClient
from scp import SCPClient

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com')

# SCPCLient takes a paramiko transport as an argument
scp = SCPClient(ssh.get_transport())

scp.put('test.txt', 'test2.txt')
scp.get('test2.txt')

# Uploading the 'test' directory with its content in the
# '/home/user/dump' remote directory
scp.put('test', recursive=True, remote_path='/home/user/dump')

scp.close()
$ md5sum test.txt test2.txt
fc264c65fb17b7db5237cf7ce1780769 test.txt
fc264c65fb17b7db5237cf7ce1780769 test2.txt

Using ‘with’ keyword

from paramiko import SSHClient
from scp import SCPClient

with SSHClient() as ssh:
    ssh.load_system_host_keys()
    ssh.connect('example.com')

    with SCPClient(ssh.get_transport()) as scp:
        scp.put('test.txt', 'test2.txt')
        scp.get('test2.txt')
$ md5sum test.txt test2.txt
fc264c65fb17b7db5237cf7ce1780769 test.txt
fc264c65fb17b7db5237cf7ce1780769 test2.txt

Uploading file-like objects

The putfo method can be used to upload file-like objects:

import io
from paramiko import SSHClient
from scp import SCPClient

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com')

# SCPCLient takes a paramiko transport as an argument
scp = SCPClient(ssh.get_transport())

# generate in-memory file-like object
fl = io.BytesIO()
fl.write(b'test')
fl.seek(0)
# upload it directly from memory
scp.putfo(fl, '/tmp/test.txt')
# close connection
scp.close()
# close file handler
fl.close()

Tracking progress of your file uploads/downloads

A progress function can be given as a callback to the SCPClient to handle how the current SCP operation handles the progress of the transfers. In the example below we print the percentage complete of the file transfer.

from paramiko import SSHClient
from scp import SCPClient
import sys

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com')

# Define progress callback that prints the current percentage completed for the file
def progress(filename, size, sent):
    sys.stdout.write("%s's progress: %.2f%%   \r" % (filename, float(sent)/float(size)*100) )

# SCPCLient takes a paramiko transport and progress callback as its arguments.
scp = SCPClient(ssh.get_transport(), progress=progress)

# you can also use progress4, which adds a 4th parameter to track IP and port
# useful with multiple threads to track source
def progress4(filename, size, sent, peername):
    sys.stdout.write("(%s:%s) %s's progress: %.2f%%   \r" % (peername[0], peername[1], filename, float(sent)/float(size)*100) )
scp = SCPClient(ssh.get_transport(), progress4=progress4)

scp.put('test.txt', '~/test.txt')
# Should now be printing the current progress of your put function.

scp.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scp-0.13.5.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

scp-0.13.5-py2.py3-none-any.whl (8.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file scp-0.13.5.tar.gz.

File metadata

  • Download URL: scp-0.13.5.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for scp-0.13.5.tar.gz
Algorithm Hash digest
SHA256 0ab2ee9178b8be53dc5c9fe2f33761e01a2435087f5c9fb5b54519873c650d60
MD5 f6d327bcaea6568c97dbe1ced8e6799e
BLAKE2b-256 d235392dde4dc4a1cb48f90fc7137104ac3246654c73aafd81efde5bd0078b12

See more details on using hashes here.

File details

Details for the file scp-0.13.5-py2.py3-none-any.whl.

File metadata

  • Download URL: scp-0.13.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for scp-0.13.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 be62d3ca534790dbaa509634fe28af6729f0d798388eaa5fdd3a1b191556dbde
MD5 3b282ff374b6c853c4d6fe29ca986074
BLAKE2b-256 565ea872b3659a651607d3f399e569b0fc581adbac892a89503f492a3277a8ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page