我正在开发一个简单的工具,可以将文件传输到一个硬编码的位置,密码也是硬编码的。我是一个python新手,但多亏了ftplib,它很容易:

import ftplib

info= ('someuser', 'password')    #hard-coded

def putfile(file, site, dir, user=(), verbose=True):
    """
    upload a file by ftp to a site/directory
    login hard-coded, binary transfer
    """
    if verbose: print 'Uploading', file
    local = open(file, 'rb')    
    remote = ftplib.FTP(site)   
    remote.login(*user)         
    remote.cwd(dir)
    remote.storbinary('STOR ' + file, local, 1024)
    remote.quit()
    local.close()
    if verbose: print 'Upload done.'

if __name__ == '__main__':
    site = 'somewhere.com'            #hard-coded
    dir = './uploads/'                #hard-coded
    import sys, getpass
    putfile(sys.argv[1], site, dir, user=info)

问题是我找不到任何支持sFTP的库。要安全地做这样的事情,正常的方法是什么?

编辑:感谢这里的答案,我已经得到了它与Paramiko工作,这是语法。

import paramiko

host = "THEHOST.com"                    #hard-coded
port = 22
transport = paramiko.Transport((host, port))

password = "THEPASSWORD"                #hard-coded
username = "THEUSERNAME"                #hard-coded
transport.connect(username = username, password = password)

sftp = paramiko.SFTPClient.from_transport(transport)

import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]    #hard-coded
localpath = sys.argv[1]
sftp.put(localpath, path)

sftp.close()
transport.close()
print 'Upload done.'

再次感谢!


当前回答

带有sshfs的PyFilesystem是一个选项。它在引子下使用Paramiko,并在上面提供了一个更好的平台独立界面。

import fs

sf = fs.open_fs("sftp://[user[:password]@]host[:port]/[directory]")
sf.makedir('my_dir')

or

from fs.sshfs import SSHFS
sf = SSHFS(...

其他回答

Twisted可以帮助你做你正在做的事情,看看他们的文档,有很多例子。此外,它是一个成熟的产品,背后有一个庞大的开发者/用户社区。

您应该查看pysftp https://pypi.python.org/pypi/pysftp,它取决于paramiko,但将大多数常见用例包装为几行代码。

import pysftp
import sys

path = './THETARGETDIRECTORY/' + sys.argv[1]    #hard-coded
localpath = sys.argv[1]

host = "THEHOST.com"                    #hard-coded
password = "THEPASSWORD"                #hard-coded
username = "THEUSERNAME"                #hard-coded

with pysftp.Connection(host, username=username, password=password) as sftp:
    sftp.put(localpath, path)

print 'Upload done.'

fspec是一个很好的选择,它提供了一个类似sftp实现的文件系统。

from fsspec.implementations.sftp import SFTPFileSystem
fs = SFTPFileSystem(host=host, username=username, password=password)

# list a directory
fs.ls("/")

# open a file
with fs.open(file_name) as file:
    content = file.read()

同样值得注意的是,fspec在实现中使用了paramiko。

如果你想要简单,你可能还想看看面料。它是一个像Ruby的Capistrano一样的自动部署工具,但是更简单,当然对于Python来说。它建在Paramiko之上。

您可能不想进行“自动部署”,但Fabric仍然非常适合您的用例。为了向您展示Fabric有多简单:脚本的fab文件和命令看起来像这样(未经测试,但99%确定它可以工作):

fab_putfile.py:

from fabric.api import *

env.hosts = ['THEHOST.com']
env.user = 'THEUSER'
env.password = 'THEPASSWORD'

def put_file(file):
    put(file, './THETARGETDIRECTORY/') # it's copied into the target directory

然后使用fab命令运行该文件:

fab -f fab_putfile.py put_file:file=./path/to/my/file

这样就完成了!:)

下面是一个使用pysftp和私钥的示例。

import pysftp

def upload_file(file_path):

    private_key = "~/.ssh/your-key.pem"  # can use password keyword in Connection instead
    srv = pysftp.Connection(host="your-host", username="user-name", private_key=private_key)
    srv.chdir('/var/web/public_files/media/uploads')  # change directory on remote server
    srv.put(file_path)  # To download a file, replace put with get
    srv.close()  # Close connection

Pysftp是一个使用paramiko和pycrypto的易于使用的SFTP模块。它为sftp提供了一个简单的接口。你可以用pysftp做的其他事情非常有用:

data = srv.listdir()  # Get the directory and file listing in a list
srv.get(file_path)  # Download a file from remote server
srv.execute('pwd') # Execute a command on the server

这里有更多关于PySFTP的命令。