Skip to main content

scp module for paramiko

Project description

Pure python scp module
======================

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
-------

.. code-block:: python

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()


.. code-block::

$ md5sum test.txt test2.txt
fc264c65fb17b7db5237cf7ce1780769 test.txt
fc264c65fb17b7db5237cf7ce1780769 test2.txt

Using 'with' keyword
------------------

.. code-block:: python

from paramiko import SSHClient
from scp import SCPClient

ssh = SSHClient()
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')


.. code-block::

$ 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:

.. code-block:: python

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.

.. code-block:: python

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)

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.11.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

scp-0.11.0-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: scp-0.11.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for scp-0.11.0.tar.gz
Algorithm Hash digest
SHA256 ea095dd1d0e131874bc9930c3965bce3d1d70be5adb2a30d811fcaea4708a9ee
MD5 9874e188a4d6aa5c849f7a09feecbdb9
BLAKE2b-256 36bf2dd8050c17166f731fc081e091aacd3044723cbc6f0fccabd6742d8dbb42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scp-0.11.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5a1b44b3b8d1da27a3592bffebfc717e3c68c9a8bbb87db786b56a3924d2911f
MD5 65a865c7ab5575793d23fdcdcb1313ed
BLAKE2b-256 ef8c399c5ddffc8c1bd2593ee6c8937ed31f005dcfa0c591da149f7b4b993f14

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