{"id":45383,"date":"2020-10-31T23:23:10","date_gmt":"2020-10-31T15:23:10","guid":{"rendered":"http:\/\/www.zhushiyao.com\/?p=45383"},"modified":"2020-10-31T23:23:55","modified_gmt":"2020-10-31T15:23:55","slug":"31521613fc","status":"publish","type":"post","link":"http:\/\/www.zhushiyao.com\/?p=45383","title":{"rendered":"\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4"},"content":{"rendered":"<div class=\"bpp-post-content\">\n<p style=\"white-space: normal;\"><span style=\"color: #ab1942; font-size: 17px;\"><strong><span style=\"font-size: 17px; color: #ab1942; font-family: Arial; background-color: #ffffff;\">\u95ee\u9898<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><span style=\"font-size: 15px;\">\u00a0<\/span><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><span style=\"font-size: 15px;\">\u4e5f\u8bb8\u5927\u5bb6\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u9047\u5230\u8fc7\u8981\u7528 Python \u811a\u672c\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u4f60\u8fd8\u4e0d\u77e5\u9053\u600e\u4e48\u8c03\u7528\u3002<img decoding=\"async\" style=\"display: inline-block; width: 20px; vertical-align: text-bottom;\" title=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" src=\"http:\/\/www.zhushiyao.com\/wp-content\/uploads\/2020\/10\/10-1604157790.png\" alt=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" \/><\/span><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><span style=\"font-size: 15px;\">\u00a0<\/span><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><span style=\"font-size: 15px;\">\u6ca1\u5173\u7cfb\uff0c\u65e9\u5728 12 \u5e74\u524d\u5c31\u6709\u7a0b\u5e8f\u5458\u5728 Stack Overflow \u4e0a\u6c42\u52a9\u63d0\u95ee\u5566\u3002<\/span><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><span style=\"font-size: 15px;\">\u00a0<\/span><\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages\" title=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" src=\"http:\/\/www.zhushiyao.com\/wp-content\/uploads\/2020\/10\/3-1604157790.png\" alt=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" \/><\/p>\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\">\n<p style=\"white-space: normal; font-variant-ligatures: normal; orphans: 2; widows: 2;\"><strong><span style=\"font-size: 15px;\">\u672c\u6587<\/span><span style=\"font-size: 15px;\">\u4e3a\u5927\u5bb6\u603b\u7ed3\u6574\u7406\u5176\u4e2d\u4e00\u4e9b\u9ad8\u8d5e\u56de\u7b54\uff0c\u4ee5\u5907\u4e0d\u65f6\u4e4b\u9700\uff01<\/span><\/strong><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"color: #ab1942; font-size: 17px;\"><strong><span style=\"font-size: 17px; color: #ab1942; font-family: Arial; background-color: #ffffff;\">\u89e3\u51b3\u65b9\u6cd5<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><strong><span style=\"font-size: 15px;\">\u65b9\u6cd51\uff1a<\/span><\/strong><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u00a0subprocess \uff0c\u6bd4\u5982\u8981\u6267\u884c <\/span><span style=\"font-family: Arial; color: #242729; font-size: 15px; background-color: #fffed5;\">ls -l<\/span><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u00a0\u547d\u4ee4<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> subprocess <\/span><\/code><code><span class=\"code-snippet_outer\">subprocess.run([<span class=\"code-snippet__string\">\"ls\"<\/span>, <span class=\"code-snippet__string\">\"-l\"<\/span>])<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-family: 'Courier New'; color: #242729; font-size: 15px;\">Python3.5 \u4e4b\u524d\u7684\u7248\u672c\uff0c\u9700\u8981\u4f7f\u7528 call<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> subprocess <\/span><\/code><code><span class=\"code-snippet_outer\">subprocess.call([<span class=\"code-snippet__string\">\"ls\"<\/span>, <span class=\"code-snippet__string\">\"-l\"<\/span>])<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><strong><span style=\"font-size: 15px;\">\u65b9\u6cd52\uff1a<\/span><\/strong><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">import os <\/span><\/code><code><span class=\"code-snippet_outer\">os.system(<span class=\"code-snippet__string\">\"ls -l\"<\/span>) <span class=\"code-snippet__comment\">#\u6267\u884c\u547d\u4ee4<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">os.popen(<span class=\"code-snippet__string\">\"ls -l\"<\/span>).<span class=\"code-snippet__built_in\">read<\/span>() <span class=\"code-snippet__comment\">#\u6267\u884c\u5e76\u8f93\u51fa\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c<\/span><\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"color: #ff6827;\"><strong><span style=\"font-family: 'Courier New'; font-size: 15px;\">\u65b9\u6cd5\u8ba8\u8bba\uff1a<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-family: 'Courier New'; color: #242729; font-size: 15px;\">\u95ee\u9898\u89e3\u51b3\u4e86\uff0c\u90a3\u4e48os.system \u548c subprocess \u90a3\u4e2a\u89e3\u51b3\u65b9\u6cd5\u66f4\u597d\u4e00\u70b9\u5462\uff1f\u8fd9\u91cc\u505a\u7b80\u5355\u8ba8\u8bba\uff0c\u867d\u7136os.system \u548c subprocess \u90fd\u80fd\u5b8c\u6210\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u7684\u529f\u80fd\uff0c\u4f46\u662fsubprocess \u6bd4 system \u66f4\u7075\u6d3b\uff08subprocess \u6211\u4eec\u53ef\u4ee5\u5f97\u5230 stdout,stderr \u4ee5\u53ca\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u7b49\u7b49\uff09\u3002\u5e76\u4e14\u5b98\u65b9\u6587\u6863\u4e5f\u662f\u66f4\u5efa\u8bae subprocess \u6a21\u5757\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"color: #ab1942; font-size: 17px;\"><strong><span style=\"font-size: 17px; color: #ab1942; font-family: Arial; background-color: #ffffff;\">\u4e0b\u9762\u662f\u5bf9\u5404\u79cd\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u7684\u65b9\u6cd5\u7684\u5c0f\u603b\u7ed3\uff1a<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">1\u3001<\/span><span style=\"font-size: 15px; background-color: #fffed5;\">os.system(&#8220;some_command with args&#8221;)<\/span><span style=\"font-size: 15px;\">\uff1a\u5c06\u547d\u4ee4\u548c\u53c2\u6570\u4f20\u7ed9\u7cfb\u7edfshell\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6267\u884c\u591a\u6761\u547d\u4ee4\uff0c\u5e76\u4e14\u53ef\u4ee5\u5efa\u7acb\u7ba1\u9053\u91cd\u5b9a\u5411\u4f8b\u5982\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">os.system(\"some_command &lt; input_file | another_command &gt; output_file\")<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u5f0f\u5f88\u7b80\u4fbf\uff0c\u4f46\u662f\u4f60\u4e0d\u5f97\u4e0d\u624b\u5de5\u5904\u7406\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u4f8b\u5982\u7a7a\u683c\u7b49\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">2\u3001<\/span><span style=\"font-size: 15px; background-color: #fffed5;\">stream=os.popen(&#8220;some_command with args&#8221;)<\/span><span style=\"font-size: 15px;\">\uff1a\u5b9e\u73b0\u7684\u529f\u80fd\u548cos.system\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662fos.popen\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7c7b\u6587\u4ef6\u5bf9\u8c61\uff0c\u8ba9\u4f60\u770b\u5230\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">stream=os.popen(<span class=\"code-snippet__string\">\"ls -l\"<\/span>)<\/span><\/code><code><span class=\"code-snippet_outer\">stream.read() <span class=\"code-snippet__comment\">#\u8f93\u51fals -l \u7684\u7ed3\u679c<\/span><\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">3\u3001subprocess\u7684Popen\u7c7b\uff0c\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528\u5b83\u6765\u4ee3\u66ffos.popen\uff0c\u4f46\u662f\u4f7f\u7528\u7684\u65f6\u5019\u53c2\u6570\u4f1a\u76f8\u5bf9\u590d\u6742\uff0c\u4f8b\u5982<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">print(subprocess.Popen(\"echo Hello World\", shell=True, stdout=subprocess.PIPE).stdout.read())<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-family: 'Courier New'; color: #242729; font-size: 15px;\">\u800c\u4e0d\u5efa\u8bae\u4f7f\u7528<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">print(os.popen(\"echo Hello World\").read())<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">4\u3001subprocess\u6a21\u5757\u4e2d\u7684call\u51fd\u6570\uff0c\u529f\u80fd\u548cPopen\u7c7b\u76f8\u4f3c\uff0c\u5e76\u4e14\u4f7f\u7528\u76f8\u540c\u7684\u53c2\u6570\uff0c\u4f46\u662f\u5b83\u4ec5\u4ec5\u5728\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u8fd4\u56de\u4e00\u4e2a\u4ee3\u7801\uff0c\u4f8b\u5982<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">return_code\u00a0=\u00a0subprocess.call(\"echo\u00a0Hello\u00a0World\",\u00a0shell=True)<\/span><\/code><code><span class=\"code-snippet_outer\">0 #\u6267\u884c\u6210\u529f<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">5\u3001\u5982\u679c\u4f7f\u7528\u7684\u662fPython3.5\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<\/span><span style=\"font-family: Arial; color: #242729; font-size: 15px; background-color: #fffed5;\">subprocess.run<\/span><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u51fd\u6570\uff0c\u5b83\u8ddf\u4e0a\u9762\u4ecb\u7ecd\u7684\u51fd\u6570\u57fa\u672c\u4e00\u6837\uff0c\u4f46\u662f\u66f4\u7075\u6d3b\uff0c\u4f1a\u5728\u547d\u4ee4\u884c\u6267\u884c\u7ed3\u675f\u540e\u8fd4\u56de\u4e00\u4e2aCompletedProcess\u5bf9\u8c61\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">return_code = subprocess.run(<span class=\"code-snippet__string\">\"echo Hello World\"<\/span>, shell=True)<\/span><\/code><code><span class=\"code-snippet_outer\">return_code<\/span><\/code><code><span class=\"code-snippet_outer\">CompletedProcess(args=<span class=\"code-snippet__string\">'echo Hello World'<\/span>, returncode=0) <span class=\"code-snippet__comment\">#CompletedProcess\u5bf9\u8c61<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__built_in\">type<\/span>(return_code)<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">6\u3001\u867d\u7136os\u6a21\u5757\u5305\u62ecfork\/exec\/spawn\u7b49\u51fd\u6570\uff0c\u4f46\u662f\u5e76\u4e0d\u5efa\u8bae\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">\u56e0\u6b64\u5982\u679c\u6709\u9700\u8981\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u9700\u8981\uff0c\u5efa\u8bae\u4f7f\u7528subprocess\uff0c\u800c\u975eos\u6a21\u5757\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u6700\u540e\uff0c\u7279\u522b\u8981\u6ce8\u610f\u5982\u679c\u4f60\u8981\u6267\u884c\u7684\u547d\u4ee4\u662f\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u4f5c\u4e3a\u53c2\u6570\uff0c\u90a3\u4e48\u5fc5\u987b\u5c0f\u5fc3\u8c28\u614e\uff0c\u5426\u5219\u4f1a\u9020\u6210\u4e25\u91cd\u7684\u540e\u679c\uff01\u6bd4\u5982\u4e0b\u9762\u7684\u4f8b\u5b50<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">print(subprocess.Popen(\"echo %s \" % user_input, stdout=PIPE).stdout.read())<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px; font-family: 'Courier New';\">\u770b\u5230\u8fd9\u6bb5\u4ee3\u7801\u80fd\u60f3\u5230\u4ec0\u4e48\u53ef\u6015\u7684\u4e8b\u60c5\u5417\uff1f\u5982\u679c\u6709\u4eba\u7ed9 user_input \u4f20\u9012\u7684\u662f<\/span><span style=\"font-size: 15px; font-family: Arial; color: #242729; background-color: #ffffff;\">\u00a0&#8220;my mama didnt love me &amp;&amp;<\/span><strong><span style=\"font-family: Arial; background-color: #ffffff; color: #ff6827;\">\u00a0rm -rf\u00a0<\/span><\/strong><span style=\"font-size: 15px; font-family: Arial; color: #242729; background-color: #ffffff;\">\/&#8221;\uff0c\u662f\u4e0d\u662f\u5934\u76ae\u53d1\u9ebb\uff01<img decoding=\"async\" style=\"display: inline-block; width: 20px; vertical-align: text-bottom;\" title=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" src=\"http:\/\/www.zhushiyao.com\/wp-content\/uploads\/2020\/10\/0-1604157791.png\" alt=\"Stack Overflow \u70ed\u5e16\uff1a\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\" \/>\u00a0\u6240\u4ee5\u8c28\u614e\u518d\u8c28\u614e\uff01\uff01\u5c3d\u91cf\u4e0d\u505a\u4ea4\u4e92\u5f0f\u8f93\u5165\uff0c\u800c\u662f\u5728\u7a0b\u5e8f\u4e2d\uff0c\u5c06\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u5199\u201c\u6b7b\u201d\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u505a\u4e3a\u5e38\u91cf\u4f20\u9012\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"color: #ab1942; font-size: 17px;\"><strong><span style=\"font-size: 17px; color: #ab1942; font-family: Arial; background-color: #ffffff;\">\u62d3\u5c55\u601d\u8def<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u00a0<\/span><\/p>\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">\u4e0b\u9762\u8fd8\u6574\u7406\u4e86\u4e00\u4e9b\u5176\u5b83\u7684\u65b9\u6cd5\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u518d\u6df1\u5165\u7814\u7a76\u3002<\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"color: #ff6827;\"><strong><span style=\"font-family: Arial; background-color: #ffffff; font-size: 15px;\">sh<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">sh\u7a0b\u5e8f\u662fsubprocess\u7684\u63a5\u53e3\uff0c\u8ba9\u4f60\u50cf\u8c03\u7528\u51fd\u6570\u4e00\u6837\u7684\u8c03\u7528\u60f3\u6267\u884c\u7684\u7a0b\u5e8f\u3002\u5982\u679c\u8981\u591a\u6b21\u8fd0\u884c\u4e00\u4e2a\u547d\u4ee4\uff0c\u8fd9\u5f88\u6709\u7528\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">sh.ls(\"-l\")\u00a0<\/span><\/code><code><span class=\"code-snippet_outer\">ls_cmd = sh.Command(\"ls\")<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><strong><span style=\"font-family: Arial; background-color: #ffffff; font-size: 15px; color: #ff6827;\">plumbum<\/span><\/strong><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\">plumbum\u529f\u80fd\u548csh\u7c7b\u4f3c\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">ls_cmd\u00a0=\u00a0plumbum.local(\"ls\u00a0-l\")\u00a0<\/span><\/code><code><span class=\"code-snippet_outer\">ls_cmd()<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><strong><span style=\"font-family: Arial; background-color: #ffffff; font-size: 15px; color: #ff6827;\">fabric<\/span><\/strong><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-size: 15px;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff;\">fabric\u00a0<\/span>\u662fpy2.5\u548c2.7\u7684\u5e93\u3002\u53ef\u4ee5\u8ba9\u4f60\u6267\u884c\u672c\u5730\u6216\u8fdc\u7a0bshell\u547d\u4ee4\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">fabric.operations.local('ls\u00a0-l')<\/span><\/code><code><span class=\"code-snippet_outer\">fabric.operations.local('ls -l', capture = True)<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\"><span style=\"color: #ff6827;\"><strong><span style=\"font-family: Arial; background-color: #ffffff; font-size: 15px;\">envoy<\/span><\/strong><\/span><\/p>\n<p style=\"white-space: normal;\">\n<p style=\"white-space: normal;\"><span style=\"font-family: Arial; color: #242729; background-color: #ffffff; font-size: 15px;\">envoy \u4ee5\u201csubprocess for humans\u201d\u95fb\u540d\uff0c\u5b83\u662f\u5bf9subprocess\u6a21\u5757\u7684\u5c01\u88c5\uff0c\u7b80\u5355\u6613\u7528<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\"><code><span class=\"code-snippet_outer\">r\u00a0=\u00a0envoy.run(<span class=\"code-snippet__string\">\"ls\u00a0-l\"<\/span>)\u00a0<\/span><\/code><code><span class=\"code-snippet_outer\">r.std_out<\/span><\/code><\/pre>\n<\/section>\n<p style=\"white-space: normal;\">\n<p><em><span style=\"font-size: 15px;\">\u53c2\u8003\u94fe\u63a5\uff1a<\/span><\/em><\/p>\n<p><em><span style=\"font-size: 12px;\">https:\/\/stackoverflow.com\/questions\/89228\/how-to-call-an-external-command<\/span><\/em><\/p>\n<p style=\"white-space: normal; text-align: center;\">\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898 \u00a0 \u4e5f\u8bb8\u5927\u5bb6\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u9047\u5230\u8fc7\u8981\u7528 Python \u811a\u672c\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u4f60\u8fd8\u4e0d\u77e5\u9053\u600e\u4e48\u8c03\u7528\u3002 \u00a0  &hellip; <a href=\"http:\/\/www.zhushiyao.com\/?p=45383\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5982\u4f55\u7528 Python \u8c03\u7528\u5916\u90e8\u547d\u4ee4\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[8],"_links":{"self":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/45383"}],"collection":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45383"}],"version-history":[{"count":2,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/45383\/revisions"}],"predecessor-version":[{"id":45390,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/45383\/revisions\/45390"}],"wp:attachment":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}