Skip to content

Instantly share code, notes, and snippets.

@xiaowl
Created July 9, 2012 10:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaowl/3075638 to your computer and use it in GitHub Desktop.
Save xiaowl/3075638 to your computer and use it in GitHub Desktop.
Pure python implementation of `scp`, using SFTP protocol. Depends on paramiko.
def scp(sshclient, src, dest=''):
path = os.path.abspath(os.path.expanduser(src)).rstrip('/')
basename = os.path.basename(path)
ftp = sshclient.open_sftp()
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
root = root.rstrip('/')
dirname = os.path.basename(root)
if root != path:
dirname = os.path.join(basename, root.split(path)[1].strip('/'))
try:
ftp.mkdir(dirname)
except: #folder exist
pass
for f in files:
if pattern is None:
inf, outf = os.path.join(root, f), os.path.join(dirname, f)
ftp.put(inf, outf)
else:
outf = os.path.join(dest, os.path.basename(path))
ftp.put(path, os.path.join(dest, os.path.basename(path)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment