{"id":177075,"date":"2024-05-08T19:23:17","date_gmt":"2024-05-08T11:23:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177075.html"},"modified":"2024-05-08T19:23:22","modified_gmt":"2024-05-08T11:23:22","slug":"%e6%80%8e%e4%b9%88%e7%94%a8python%e5%af%b9%e4%b8%80%e4%b8%aa%e4%ba%a4%e4%ba%92%e5%bc%8f%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c%e7%a8%8b%e5%ba%8f%e8%bf%9b%e8%a1%8c%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177075.html","title":{"rendered":"\u600e\u4e48\u7528Python\u5bf9\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060850\/718ac4d1-cf8f-4e9e-8827-440a8dfec1eb.webp\" alt=\"\u600e\u4e48\u7528Python\u5bf9\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\" \/><\/p>\n<p><p>\u7528Python\u5bf9\u4e00\u4e2a\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\uff0c<strong>\u4e3b\u8981\u65b9\u5f0f\u6d89\u53ca\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>pexpect<\/code>\u5e93<\/strong>\u3001\u4ee5\u53ca<strong>\u7b2c\u4e09\u65b9\u5de5\u5177<\/strong>\u5982<code>expect<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5728\u4e0d\u540c\u7a0b\u5ea6\u4e0a\u5b9e\u73b0\u4e0e\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u4ea4\u4e91\u4ea4\u4e92\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\uff0c<strong><code>pexpect<\/code>\u5e93\u7684\u4f7f\u7528\u56e0\u5176\u5bf9\u4ea4\u4e91\u4ea4\u4e92\u7a0b\u5e8f\u7684\u5f3a\u5927\u652f\u6301\u800c\u5c24\u4e3a\u7a81\u51fa<\/strong>\u3002<\/p>\n<\/p>\n<p><p><code>pexpect<\/code>\u80fd\u591f\u542f\u52a8\u5b50\u5e94\u7528\u7a0b\u5e8f\u5e76\u4e0e\u4e4b\u8fdb\u884c\u4ea4\u4e91\u4ea4\u4e92\uff0c\u5b83\u9002\u7528\u4e8e\u81ea\u52a8\u5316\u63a7\u5236\u90a3\u4e9b\u9700\u8981\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u6bd4\u5982\uff0c\u81ea\u52a8\u5316SSH\u767b\u9646\u3001\u81ea\u52a8\u5316ftp\u64cd\u4f5c\u7b49\u3002<code>pexpect<\/code>\u901a\u8fc7\u7b49\u5f85\u9884\u5b9a\u7684\u8f93\u51fa\u7136\u540e\u53d1\u9001\u8f93\u5165\u6765\u5de5\u4f5c\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u81ea\u52a8\u5316\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SUBPROCESS\u6a21\u5757\u57fa\u7840\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u63a5\u53e3\uff0c\u7528\u4e8e\u5728Python\u7a0b\u5e8f\u4e2d\u542f\u52a8\u548c\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u3002\u8fd9\u4e2a\u6a21\u5757\u80fd\u591f\u8fd0\u884c\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u547d\u4ee4\uff0c\u83b7\u53d6\u5b83\u7684\u8f93\u5165\u3001\u8f93\u51fa\u548c\u9519\u8bef\u7ba1\u9053\uff0c\u4ee5\u53ca\u5b83\u7684\u8fd4\u56de\u7801\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b<\/strong>\uff1a<\/p>\n<p><code>subprocess<\/code>\u901a\u8fc7<code>Popen<\/code>\u7c7b\u5b9e\u73b0\u4e0e\u5b50\u8fdb\u7a0b\u7684\u4ea4\u4e91\u4ea4\u4e92\u3002<code>Popen<\/code>\u7c7b\u63a5\u53d7\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u8981\u6267\u884c\u7684\u547d\u4ee4\u53ca\u5176\u53c2\u6570\u3002\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u542f\u52a8\u4efb\u4f55\u4f60\u9700\u8981\u7684\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7\u6807\u51c6\u8f93\u5165\uff08stdin\uff09\u3001\u6807\u51c6\u8f93\u51fa(stdout)\u3001\u6807\u51c6\u9519\u8bef(stderr)\u4e0e\u4e4b\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ea4\u4e91\u4ea4\u4e92\u7684\u5b9e\u73b0<\/strong>\uff1a<\/p>\n<p>\u4e00\u65e6\u521b\u5efa\u4e86<code>Popen<\/code>\u5bf9\u8c61\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<code>communicate()<\/code>\u65b9\u6cd5\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e91\u4ea4\u4e92\u3002\u8be5\u65b9\u6cd5\u5141\u8bb8\u4f60\u53d1\u9001\u6570\u636e\u5230\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u5165\uff0c\u5e76\u8bfb\u53d6\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u53ef\u9009\u7684\u8f93\u5165\u53c2\u6570\uff0c\u7528\u4e8e\u53d1\u9001\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7b49\u5f85\u8fdb\u7a0b\u7ed3\u675f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001PEXPECT\u5e93\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>pexpect<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9Python\u5e93\uff0c\u7528\u4e8e\u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u4e0e\u4e4b\u4ea4\u4e92\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u7528\u6237\u8f93\u5165\u7684\u7a0b\u5e8f\u3002<code>pexpect<\/code>\u53ef\u4ee5\u8bc6\u522b\u5b50\u7a0b\u5e8f\u671f\u671b\u7684\u8f93\u51fa\uff0c\u5e76\u6309\u9884\u671f\u53d1\u9001\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<p>\u8981\u4f7f\u7528<code>pexpect<\/code>\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pexpect<\/code>\u5b8c\u6210\u5b89\u88c5\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>spawn<\/code>\u7c7b\u6765\u542f\u52a8\u548c\u63a7\u5236\u5b50\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ea4\u4e92\u5f0f\u64cd\u4f5c\u5b9e\u4f8b<\/strong>\uff1a<\/p>\n<p><code>pexpect<\/code>\u7684<code>expect()<\/code>\u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u5b50\u7a0b\u5e8f\u8f93\u51fa\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u3002\u4e00\u65e6\u8be5\u5b57\u7b26\u4e32\u51fa\u73b0\uff0c<code>pexpect<\/code>\u5c31\u53ef\u4ee5\u53d1\u9001\u54cd\u5e94\u3002\u6b64\u5916\uff0c<code>sendline()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5411\u5b50\u7a0b\u5e8f\u53d1\u9001\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u6309\u4e0b\u56de\u8f66\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u6a21\u62df\u547d\u4ee4\u884c\u4ea4\u4e92\u5c24\u5176\u6709\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001EXPECT\u5de5\u5177\u53ca\u5176\u5728Python\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>expect<\/code>\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u7a0b\u5e8f\uff0c\u800c\u975ePython\u5e93\uff0c\u4f46\u5b83\u53ef\u4ee5\u4e0ePython\u914d\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u4f9b\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u4ea4\u4e92\u81ea\u52a8\u5316\u80fd\u529b\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong><code>expect<\/code>\u5de5\u5177\u6982\u8ff0<\/strong>\uff1a<\/p>\n<p><code>expect<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u63a7\u5236\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684UNIX\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u8bc6\u522b\u7a0b\u5e8f\u7684\u8f93\u51fa\u7136\u540e\u63d0\u4f9b\u76f8\u5e94\u7684\u8f93\u5165\u6765\u5de5\u4f5c\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u81ea\u52a8\u5316\u7684\u767b\u5f55\u3001FTP\u64cd\u4f5c\u3001\u7f51\u7edc\u8bbe\u5907\u914d\u7f6e\u7b49\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528<code>expect<\/code><\/strong>\uff1a<\/p>\n<p>\u867d\u7136<code>expect<\/code>\u672c\u8eab\u5e76\u975ePython\u5de5\u5177\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u6837\uff0cPython\u7a0b\u5e8f\u53ef\u4ee5\u5229\u7528<code>expect<\/code>\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u901a\u8fc7\u5916\u90e8\u811a\u672c\u4e0e\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5b9e\u9645\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u81ea\u52a8\u5316SSH\u767b\u5f55<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528<code>pexpect<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u6613\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u81ea\u52a8\u5316SSH\u767b\u5f55\u8fc7\u7a0b\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6d89\u53ca\u7b49\u5f85<code>ssh<\/code>\u547d\u4ee4\u7684\u5bc6\u7801\u63d0\u793a\uff0c\u7136\u540e\u81ea\u52a8\u63d0\u4f9b\u5bc6\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u547d\u4ee4\u884c\u5de5\u5177\u81ea\u52a8\u5316\u6d4b\u8bd5<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528<code>subprocess<\/code>\u6216<code>pexpect<\/code>\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316\u6267\u884c\u547d\u4ee4\u884c\u5de5\u5177\u7684\u6d4b\u8bd5\uff0c\u5305\u62ec\u53d1\u9001\u547d\u4ee4\u5e76\u9a8c\u8bc1\u671f\u671b\u7684\u8f93\u51fa\u3002\u8fd9\u5728\u8fdb\u884c\u8f6f\u4ef6\u5f00\u53d1\u548c\u6d4b\u8bd5\u65f6\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u90a3\u4e9b\u63d0\u4f9b\u547d\u4ee4\u884c\u754c\u9762\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u901a\u8fc7\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>pexpect<\/code>\u5e93\u548c<code>expect<\/code>\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4e0e\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u52a3\u52bf\uff0c\u4f46<code>pexpect<\/code>\u56e0\u5176\u6613\u7528\u6027\u548c\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u652f\u6301\u800c\u5728\u4f17\u591a\u573a\u666f\u4e2d\u8131\u9896\u800c\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4e0e\u547d\u4ee4\u884c\u4ea4\u4e92\u5f0f\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528Python\u7684subprocess\u6a21\u5757\uff0c\u901a\u8fc7\u521b\u5efa\u5b50\u8fdb\u7a0b\u6765\u4e0e\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u4f7f\u7528Popen\u51fd\u6570\u6765\u542f\u52a8\u5b50\u8fdb\u7a0b\u5e76\u4e0e\u5176\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u4f7f\u7528communicate\u65b9\u6cd5\u6765\u53d1\u9001\u8f93\u5165\u5e76\u83b7\u53d6\u8f93\u51fa\u3002<\/li>\n<li>\u9996\u5148\uff0c\u9700\u8981\u627e\u5230\u8981\u4e0e\u4e4b\u4ea4\u4e92\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528which\u547d\u4ee4\u6765\u67e5\u627e\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8def\u5f84\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3aPopen\u51fd\u6570\u7684\u53c2\u6570\u3002<\/li>\n<li>\u7136\u540e\uff0c\u9700\u8981\u4f7f\u7528subprocess.Popen\u51fd\u6570\u6765\u542f\u52a8\u5b50\u8fdb\u7a0b\u3002\u53ef\u4ee5\u5c06shell\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\uff0c\u4ee5\u4fbf\u80fd\u591f\u5728Python\u4e2d\u6267\u884c\u547d\u4ee4\u884c\u547d\u4ee4\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528communicate\u65b9\u6cd5\u6765\u53d1\u9001\u8f93\u5165\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u53ef\u4ee5\u5c06\u8f93\u5165\u4f5c\u4e3acommunicate\u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u4f7f\u7528communicate\u65b9\u6cd5\u8fd4\u56de\u7684\u5143\u7ec4\u6765\u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<p><strong>Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528Python\u7684pexpect\u5e93\u53ef\u4ee5\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\u3002pexpect\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u5f3a\u5927\u7684\u63a5\u53e3\uff0c\u7528\u4e8e\u4e0e\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002<\/li>\n<li>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5pexpect\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip install pexpect\u547d\u4ee4\u6765\u5b89\u88c5\u3002<\/li>\n<li>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528spawn\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u4e0e\u4e4b\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u5c06\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8def\u5f84\u4f5c\u4e3aspawn\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528expect\u65b9\u6cd5\u6765\u5339\u914d\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u9700\u8981\u7684\u8f93\u51fa\uff0c\u5e76\u5728\u5339\u914d\u6210\u529f\u65f6\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528sendline\u65b9\u6cd5\u6765\u53d1\u9001\u8f93\u5165\u7ed9\u5b50\u8fdb\u7a0b\u3002\u53ef\u4ee5\u5c06\u9700\u8981\u53d1\u9001\u7684\u8f93\u5165\u4f5c\u4e3asendline\u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6a21\u62df\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528Python\u7684io\u6a21\u5757\u53ef\u4ee5\u6a21\u62df\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002io\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u3002<\/li>\n<li>\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528io.StringIO\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u53ef\u8bfb\u5199\u7684\u5b57\u7b26\u4e32IO\u5bf9\u8c61\u3002\u53ef\u4ee5\u5c06\u5e0c\u671b\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528io.BytesIO\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u53ef\u8bfb\u5199\u7684\u5b57\u8282IO\u5bf9\u8c61\u3002\u53ef\u4ee5\u5c06\u5e0c\u671b\u8f93\u51fa\u7684\u5b57\u8282\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u7684stdin\u5c5e\u6027\u6765\u66ff\u6362\u6807\u51c6\u8f93\u5165\u6d41\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5411\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u53d1\u9001\u8f93\u5165\u3002\u53ef\u4ee5\u5c06\u521b\u5efa\u7684\u53ef\u8bfb\u5199\u7684\u5b57\u7b26\u4e32IO\u5bf9\u8c61\u8d4b\u503c\u7ed9sys.stdin\u3002<\/li>\n<li>\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u7684stdout\u5c5e\u6027\u6765\u66ff\u6362\u6807\u51c6\u8f93\u51fa\u6d41\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u6355\u83b7\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002\u53ef\u4ee5\u5c06\u521b\u5efa\u7684\u53ef\u8bfb\u5199\u7684\u5b57\u8282IO\u5bf9\u8c61\u8d4b\u503c\u7ed9sys.stdout\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5bf9\u4e00\u4e2a\u4ea4\u4e91\u4ea4\u4e92\u5f0f\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\uff0c\u4e3b\u8981\u65b9\u5f0f\u6d89\u53ca\u4f7f\u7528subprocess\u6a21\u5757\u3001pexpect\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":177082,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177075"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=177075"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177075\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177082"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}