{"id":2805,"date":"2016-08-07T00:44:19","date_gmt":"2016-08-06T16:44:19","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2805"},"modified":"2016-08-07T00:44:19","modified_gmt":"2016-08-06T16:44:19","slug":"%e5%9c%a8python%e4%b8%ad%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2805.html","title":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u5916\u90e8\u547d\u4ee4"},"content":{"rendered":"<p>=Start=<\/p>\n<h5>\u7f18\u7531\uff1a<\/h5>\n<p>Python\u4e2d\u7684<a href=\"https:\/\/docs.python.org\/2\/library\/subprocess.html#module-subprocess\">subprocess<\/a>\u6a21\u5757\u5141\u8bb8\u4f60\u6d3e\u751f\u51fa\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u901a\u8fc7pipe\u8fde\u63a5\u5b83\u4eec\u7684\u6807\u51c6\u8f93\u5165\/\u6807\u51c6\u8f93\u51fa\/\u6807\u51c6\u51fa\u9519\uff0c\u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u8fd4\u56de\u72b6\u6001\u7801\u3002subprocess\u6a21\u5757\u662f\u88ab\u8bbe\u8ba1\u7528\u6765\u66ff\u4ee3\u4e0b\u9762\u51e0\u4e2a\u6bd4\u8f83\u8001\u7684\u6a21\u5757\u548c\u51fd\u6570\u7684\uff1a<\/p>\n<ul>\n<li>os.system<\/li>\n<li>os.spawn*<\/li>\n<li>os.popen*<\/li>\n<li>popen2.*<\/li>\n<li>commands.*<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0324\">PEP 324<\/a> \u2013 \u4e14 PEP324 \u660e\u786e\u63a8\u8350\u4f7f\u7528subprocess\u6a21\u5757<\/p>\n<h5>\u6b63\u6587\uff1a<\/h5>\n<p>\u5728\u4f7f\u7528subprocess\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u65f6\u5019\uff0c\u8981\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li>\u5728\u521b\u5efa\u5b50\u8fdb\u7a0b\u4e4b\u540e\uff0c\u7236\u8fdb\u7a0b\u662f\u5426\u6682\u505c\uff0c\u5e76\u7b49\u5f85\u5b50\u8fdb\u7a0b\u8fd0\u884c\uff1b<\/li>\n<li>\u51fd\u6570\u8fd4\u56de\u4ec0\u4e48\uff1b<\/li>\n<li>\u5f53returncode\u4e0d\u4e3a0\u65f6\uff0c\u7236\u8fdb\u7a0b\u5982\u4f55\u5904\u7406\u3002<\/li>\n<\/ol>\n<h6>\u5c0f\u7ed3\uff1a<\/h6>\n<pre class=\"lang:default decode:true \" title=\"\u5982\u4f55\u9009\u62e9subprocess\u4e2d\u7684\u51fd\u6570\/\u5bf9\u8c61\">1.\u963b\u585e\u5f0f\uff0c\u4e14\u4e0d\u9700\u8981\u8f93\u51fa\u7ed3\u679c\np = subprocess.Popen(); p.communicate()\/p.wait()\nsubprocess.call()\n\n2.\u963b\u585e\u5f0f\uff0c\u8981\u68c0\u67e5\u5b50\u8fdb\u7a0b\u6267\u884c\u72b6\u6001\nsubprocess.call()\nsubprocess.check_call()\nsubprocess.check_output()\n\n3.\u963b\u585e\u5f0f\uff0c\u8981\u68c0\u67e5\u5b50\u8fdb\u7a0b\u6267\u884c\u72b6\u6001\uff0c\u4e14\u8981\u67e5\u770b\u9519\u8bef\u539f\u56e0\nsubprocess.check_output()\n\n4.\u963b\u585e\u5f0f\uff0c\u624b\u52a8\u68c0\u67e5\u5b50\u8fdb\u7a0b\u6267\u884c\u72b6\u6001\np = subprocess.Popen(cmd, shell=True)\nwhile p.poll() is None:\n    time.sleep(1)\nif p.returncode != 0:\n    print('\"{0}\" exec failed.'.format(cmd))\n\n5.\u975e\u963b\u585e\u5f0f\np = subprocess.Popen()\n\n\n#\u5e38\u89c1\u7528\u6cd5\ntry:\n    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\nexcept (OSError, ValueError) as e:\n    print(\"%r failed, reason %s\" % (cmd, str(e)))\n    return -1\nstdout_data, stderr_data = p.communicate()\nif p.returncode != 0:\n    print(\"%r failed, status code %s stdout %r stderr %r\" % (cmd, p.returncode, stdout_data, stderr_data))<\/pre>\n<h6><span style=\"color: #0000ff;\"><strong>subprocess\u6a21\u5757\u63d0\u4f9b\u7684\u5e38\u7528\u51fd\u6570<\/strong><\/span><\/h6>\n<p>subprocess.call()<\/p>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/li>\n<li>\u8fd4\u56de\u9000\u51fa\u4fe1\u606f(returncode\uff0c\u76f8\u5f53\u4e8eexit code)<\/li>\n<\/ul>\n<p>subprocess.check_call()<\/p>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/li>\n<li>\u8fd4\u56de0<\/li>\n<li>\u68c0\u67e5\u9000\u51fa\u4fe1\u606f\uff0c\u5982\u679creturncode\u4e0d\u4e3a0\uff0c\u5219\u629b\u51fa\u9519\u8befsubprocess.CalledProcessError\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u6709returncode\u5c5e\u6027\uff0c\u53ef\u7528try&#8230;except&#8230;\u6765\u68c0\u67e5(\u89c1Python\u9519\u8bef\u5904\u7406)\u3002<\/li>\n<\/ul>\n<p>subprocess.check_output()<\/p>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/li>\n<li>\u8fd4\u56de\u5b50\u8fdb\u7a0b\u5411\u6807\u51c6\u8f93\u51fa\u7684\u8f93\u51fa\u7ed3\u679c<\/li>\n<li>\u68c0\u67e5\u9000\u51fa\u4fe1\u606f\uff0c\u5982\u679creturncode\u4e0d\u4e3a0\uff0c\u5219\u629b\u51fa\u9519\u8befsubprocess.CalledProcessError\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u6709returncode\u5c5e\u6027\u548coutput\u5c5e\u6027\uff0coutput\u5c5e\u6027\u4e3a\u6807\u51c6\u8f93\u51fa\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u53ef\u7528try&#8230;except&#8230;\u6765\u68c0\u67e5\u3002<\/li>\n<\/ul>\n<h6><span style=\"color: #0000ff;\"><strong>subprocess.Popen\u5bf9\u8c61<\/strong><\/span><\/h6>\n<p>\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u7684\u4e09\u4e2a\u51fd\u6570\u90fd\u662f\u57fa\u4e8ePopen()\u7684\u5c01\u88c5(wrapper)\u3002\u8fd9\u4e9b\u5c01\u88c5\u7684\u76ee\u7684\u5728\u4e8e\u8ba9\u6211\u4eec\u5bb9\u6613\u4f7f\u7528\u5b50\u8fdb\u7a0b\u3002\u5f53\u6211\u4eec\u60f3\u8981\u66f4\u4e2a\u6027\u5316\u6211\u4eec\u7684\u9700\u6c42\u7684\u65f6\u5019\uff0c\u5c31\u9700\u8981\u4f7f\u7528Popen\u7c7b\uff0c\u8be5\u7c7b\u751f\u6210\u7684\u5bf9\u8c61\u7528\u6765\u4ee3\u8868\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e0e\u4e0a\u9762\u7684\u5c01\u88c5\u4e0d\u540c\uff0cPopen\u5bf9\u8c61\u521b\u5efa\u540e\uff0c\u4e3b\u7a0b\u5e8f\u4e0d\u4f1a\u81ea\u52a8\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u3002\u6211\u4eec\u5fc5\u987b\u8c03\u7528\u5bf9\u8c61\u7684wait()\u65b9\u6cd5\uff0c\u7236\u8fdb\u7a0b\u624d\u4f1a\u7b49\u5f85(\u4e5f\u5c31\u662f\u963b\u585eblock)\uff1a<\/p>\n<pre class=\"lang:default decode:true \">import subprocess\nchild = subprocess.Popen([\"ping\", \"-c\", \"5\", \"www.baidu.com\"])\nprint(\"parent process\")<\/pre>\n<p>\u4ece\u8fd0\u884c\u7ed3\u679c\u4e2d\u770b\u5230\uff0c\u7236\u8fdb\u7a0b\u5728\u5f00\u542f\u5b50\u8fdb\u7a0b\u4e4b\u540e\u5e76\u6ca1\u6709\u7b49\u5f85child\u7684\u5b8c\u6210\uff0c\u800c\u662f\u76f4\u63a5\u8fd0\u884cprint\u3002<\/p>\n<p>\u5bf9\u6bd4\u7b49\u5f85\u7684\u60c5\u51b5:<\/p>\n<pre class=\"lang:default decode:true \">import subprocess\nchild = subprocess.Popen([\"ping\", \"-c\", \"5\", \"www.baidu.com\"])\nchild.wait()\nprint(\"parent process\")<\/pre>\n<div>Popen.poll() \u00a0 \u00a0#<span style=\"color: #3366ff;\"><strong>\u53ef\u7528\u4e8e\u68c0\u67e5\u5b50\u8fdb\u7a0b\u72b6\u6001<\/strong><\/span><\/div>\n<div>\u00a0 \u00a0 \u68c0\u67e5\u5b50\u8fdb\u7a0b\u662f\u5426\u5df2\u7ec8\u6b62\u3002\u8bbe\u7f6e\u5e76\u8fd4\u56de returncode attribute.<\/div>\n<div><\/div>\n<div>Popen.wait() \u00a0 \u00a0#<strong><span style=\"color: #3366ff;\">\u53ef\u7528\u6765\u963b\u585e\u7236\u8fdb\u7a0b\uff0c\u7b49\u5f85\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5<\/span><\/strong><\/div>\n<div>\u00a0 \u00a0 \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u3002\u8bbe\u7f6e\u5e76\u8fd4\u56de returncode attribute.<\/div>\n<div><\/div>\n<div>Popen.pid<\/div>\n<div>\u00a0 \u00a0 <span style=\"color: #3366ff;\"><strong>\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7<\/strong><\/span>\u3002\u5982\u679c\u4f60\u8bbe\u7f6e\u4e86`shell=True`\u9009\u9879\uff0c\u5219\u8fd4\u56de\u7684\u662f\u6d3e\u751fshell\u7684\u8fdb\u7a0b\u53f7<\/div>\n<div><\/div>\n<div>Popen.returncode<\/div>\n<div>\u00a0 \u00a0 <strong><span style=\"color: #3366ff;\">\u5b50\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\uff0c\u7531 poll() \u548c wait() \u8fdb\u884c\u8bbe\u7f6e\uff08\u95f4\u63a5\u7684\u4e5f\u88ab communicate() \u8bbe\u7f6e\uff09<\/span><\/strong>\uff0c\u5f53\u503c\u4e3a None \u65f6\u8868\u660e\u8fdb\u7a0b\u8fd8\u672a\u7ec8\u6b62\u3002(\u5728Unix\u7cfb\u7edf\u4e2d\u82e5\u8fd4\u56de`-N`\u5219\u8868\u660e\u8fdb\u7a0b\u662f\u88ab\u901a\u8fc7N\u53f7\u4fe1\u53f7\u7ed9\u7ec8\u6b62\u6389\u7684)<\/div>\n<h6><span style=\"color: #0000ff;\"><strong>subprocess\u6a21\u5757\u4e2d\u7684\u5f02\u5e38<\/strong><\/span><\/h6>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/subprocess.html#exceptions\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/subprocess.html#exceptions<\/a><\/p>\n<ul>\n<li>OSError #\u5f53\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u5e76\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u65f6\u4f1a\u629b\u51fa\u8be5\u9519\u8bef\uff0c\u5728\u5e94\u7528\u4e2d\u5e94\u8be5\u6ce8\u610f\u8be5\u5f02\u5e38<\/li>\n<li>ValueError #\u5f53\u5728\u8c03\u7528Popen\u65f6\u4f7f\u7528\u4e86\u4e0d\u5408\u6cd5\u7684\u53c2\u6570\u4f1a\u629b\u51fa\u8be5\u9519\u8bef<\/li>\n<li>CalledProcessError #\u5728\u8c03\u7528 check_call() \u548c check_output() \u65f6\uff0c\u5982\u679c\u7a0b\u5e8f\u6267\u884c\u5931\u8d25\u5219\u4f1a\u629b\u51fa\u8be5\u9519\u8bef<\/li>\n<\/ul>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/vamei\/archive\/2012\/09\/23\/2698014.html\" target=\"_blank\">Python\u6807\u51c6\u5e9306 \u5b50\u8fdb\u7a0b (subprocess\u5305)<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/subprocess.html\">https:\/\/docs.python.org\/2\/library\/subprocess.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/89228\/calling-an-external-command-in-python\">http:\/\/stackoverflow.com\/questions\/89228\/calling-an-external-command-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/12713801\/python-2-7-3-process-popen-failures\">http:\/\/stackoverflow.com\/questions\/12713801\/python-2-7-3-process-popen-failures<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/python-run-external-command-and-get-output\/\">http:\/\/www.cyberciti.biz\/faq\/python-run-external-command-and-get-output\/<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a Python\u4e2d\u7684subprocess\u6a21\u5757\u5141\u8bb8\u4f60\u6d3e\u751f\u51fa\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u901a\u8fc7pipe\u8fde\u63a5\u5b83\u4eec\u7684 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[641,8,642],"class_list":["post-2805","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-popen","tag-python","tag-subprocess"],"views":4076,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2805"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2805\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}