擅长:python、mysql、java
<p>首先,复制<a href="http://porkmail.org/era/unix/award.html#cat" rel="nofollow noreferrer">useless use of cat</a>是不值得的,即使您想要其余的管道。在</p>
<p>其次,很明显,尽管您包含了完全没有意义的<code>cat</code>命令,但您忽略了真正执行某些操作的唯一命令—尝试都不会运行<code>ssh</code>。在</p>
<hr/>
<p>编辑以备将来参考-既然公认的答案是<em>仍然沉迷于cat的相同无用用途,那么代码应该是这样的:</p>
<pre><code>from subprocess import PIPE , Popen
p = Popen(["ssh", "remote_machine", "python"],
stdin=open("SomePythonScript.py",'r'), stdout=PIPE)
output = p.communicate()[0]
</code></pre>
<p>原来的shell代码首先应该是这样的:</p>
^{pr2}$
<p>你的原始代码包含</p>
<ul>
<li><pre><code>p1 = Popen([["cat", "SomePythonScript.py"], stdout=PIPE)
</code></pre>
<p>它与<code>open("SomePythonScript.py",'r')</code>具有相同的效果,但以运行子进程为代价却毫无益处。</p></li>
<li><pre><code>p2 = Popen(["ssh", "remote_machine", "cat - | python"], ...
</code></pre>
<p>它在远程计算机上运行一个额外的冗余进程。由于<code>cat -</code>只是将其stdin复制到stdout,因此可以完全忽略它</p></li>
</ul>