2015-09-25 152 views
1

我试图使用一个子进程通过sftp与一些文件进行交互。我可以让它连接并执行一个命令,但是这个过程会死掉。我怎样才能保持对这个过程的控制,并继续在sftp中移动?如何向Python子进程提交多个命令?


import os 
import subprocess 

serverFiles = 'sftp.servername.com' 
userFiles = 'myusername' 
keyfileFiles = 'C:\\key\\file\\path' 


with subprocess.Popen(['sftp','-Q','-i',keyfileFiles,userFiles+'@'+serverFiles], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) as proc: 
    (out, err) = proc.communicate(input='ls -l'.encode()) 
    print(out.decode()) 
    #process dies. cannot proc.stdin.write, cannot proc.communicate 

我的输出显示我欢迎横幅,然后从顶层文件夹中的LS,则进程结束,任何下列命令的错误出与proc被关闭。

+0

你能使用其他python模块的SFTP像paramiko? http://www.paramiko.org/ 你也可以在shell上编写交互命令的pexpect脚本? 我不认为subprocess是用于交互式shell命令的正确模块 – cmidi

+0

我的第一个想法是使用paramiko,但有一些限制阻止方式的限制。即版本管理和管理对外部软件包的厌恶。你有没有在子流程之外的建议? – meteorainer

+0

我会建议pexpect https://pexpect.readthedocs.org/en/stable/为交互式shell命令创建一个期望脚本,如果pexpect可用 – cmidi

回答

相关问题