Python 中用于批量复制的持久 WinSCP 连接
我正在尝试将数千个文件复制到远程服务器。这些文件是在脚本内实时生成的。我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我需要这样做,并发现与此类似的代码效果很好:
这是简化的(并使用 Python 3),但你明白了。
I needed to do this and found that code similar to this worked well:
This is simplified (and using Python 3), but you get the idea.
除了使用外部程序 (winscp),您还可以使用 python ssh 库,例如 pyssh。
Instead of using an external program (winscp) you could also use an python ssh-library like pyssh.
您必须在 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