擅长:python、mysql、java
<p>还有一些人建议使用<code>shell=True</code>,如果要将可信输入传递给shell,那么<a href="https://stackoverflow.com/a/10405562/577088">this answer</a>就可以了。然而,<code>shell=True</code>引入了一些不安全性。为了安全起见,<a href="http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline" rel="nofollow noreferrer">docs</a>建议如下:</p>
<pre><code>output=`dmesg | grep hda`
# becomes
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
</code></pre>