Python 中用于批量复制的持久 WinSCP 连接

发布于 2024-12-17 12:34:38 字数 458 浏览 4 评论 0原文

我正在尝试将数千个文件复制到远程服务器。这些文件是在脚本内实时生成的。我正在 Windows 系统上工作,需要将文件复制到 Linux 服务器(因此需要转义)。

我目前有:

import os
os.system("winscp.exe /console /command  \"option batch on\" \"option confirm off\" \"open user:pass@host\" \"put f1.txt /remote/dest/\"")

我正在使用Python来生成文件,但需要一种方法来持久保存远程连接,以便我可以在生成每个文件时将其复制到服务器(而不是每次创建一个新连接)。这样,我只需要更改 put 选项中的字段:

"put f2 /remote/dest"
"put f3 /remote/dest"

等等。

I'm trying to copy thousands files to a remote server. These files are generated in real-time within the script. I'm working on a Windows system and need to copy the files to a Linux server (hence the escaping).

I currently have:

import os
os.system("winscp.exe /console /command  \"option batch on\" \"option confirm off\" \"open user:pass@host\" \"put f1.txt /remote/dest/\"")

I'm using Python to generate the files but need a way to persist the remote connection so that I can copy each file, to the server, as it is generated (as opposed to creating a new connection each time). That way, I'll only need to change the field in the put option thus:

"put f2 /remote/dest"
"put f3 /remote/dest"

etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-12-24 12:34:38

我需要这样做,并发现与此类似的代码效果很好:

from subprocess import Popen, PIPE

WINSCP = r'c:\<path to>\winscp.com'

class UploadFailed(Exception):
    pass

def upload_files(host, user, passwd, files):
    cmds = ['option batch abort', 'option confirm off']
    cmds.append('open sftp://{user}:{passwd}@{host}/'.format(host=host, user=user, passwd=passwd))
    cmds.append('put {} ./'.format(' '.join(files)))
    cmds.append('exit\n')
    with Popen(WINSCP, stdin=PIPE, stdout=PIPE, stderr=PIPE,
               universal_newlines=True) as winscp: #might need shell = True here
        stdout, stderr = winscp.communicate('\n'.join(cmds))
    if winscp.returncode:
        # WinSCP returns 0 for success, so upload failed
        raise UploadFailed

这是简化的(并使用 Python 3),但你明白了。

I needed to do this and found that code similar to this worked well:

from subprocess import Popen, PIPE

WINSCP = r'c:\<path to>\winscp.com'

class UploadFailed(Exception):
    pass

def upload_files(host, user, passwd, files):
    cmds = ['option batch abort', 'option confirm off']
    cmds.append('open sftp://{user}:{passwd}@{host}/'.format(host=host, user=user, passwd=passwd))
    cmds.append('put {} ./'.format(' '.join(files)))
    cmds.append('exit\n')
    with Popen(WINSCP, stdin=PIPE, stdout=PIPE, stderr=PIPE,
               universal_newlines=True) as winscp: #might need shell = True here
        stdout, stderr = winscp.communicate('\n'.join(cmds))
    if winscp.returncode:
        # WinSCP returns 0 for success, so upload failed
        raise UploadFailed

This is simplified (and using Python 3), but you get the idea.

回心转意 2024-12-24 12:34:38

除了使用外部程序 (winscp),您还可以使用 python ssh 库,例如 pyssh

Instead of using an external program (winscp) you could also use an python ssh-library like pyssh.

愿与i 2024-12-24 12:34:38

您必须在 Python 中启动持久 WinSCP 子进程,并将 put 命令连续提供给其标准输入。

我没有这方面的 Python 示例,但有一个等效的 JScript 示例:
https://winscp.net/eng/docs/guide_automation_advanced#inout
或 C# 示例:
https://winscp.net/eng/docs/guide_dotnet#input

虽然使用 WinSCP通过 Python 的 COM 接口进行 .NET 汇编会更容易:
https://winscp.net/eng/docs/library

You would have to start persistent WinSCP sub-process in Python and feed the put commands to its standard input continuously.

I do not have Python example for this, but there's an equivalent JScript example:
https://winscp.net/eng/docs/guide_automation_advanced#inout
or C# example:
https://winscp.net/eng/docs/guide_dotnet#input

Though using WinSCP .NET assembly via its COM interface for Python would be a way easier:
https://winscp.net/eng/docs/library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文