{"id":181783,"date":"2024-05-08T21:05:12","date_gmt":"2024-05-08T13:05:12","guid":{"rendered":""},"modified":"2024-05-08T21:05:23","modified_gmt":"2024-05-08T13:05:23","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8php%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%87%8c%e8%bf%90%e8%a1%8cpython%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/181783.html","title":{"rendered":"\u5982\u4f55\u5728PHP\u670d\u52a1\u5668\u91cc\u8fd0\u884cpython\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27071619\/fad942bc-2e6b-4d29-bff8-aa8d946abf37.webp\" alt=\"\u5982\u4f55\u5728PHP\u670d\u52a1\u5668\u91cc\u8fd0\u884cpython\u811a\u672c\" \/><\/p>\n<p><p>\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u662f\u4e00\u79cd\u6574\u5408\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u4f18\u52bf\u7684\u6709\u6548\u65b9\u6cd5\uff0c<strong>\u4e3b\u8981\u6d89\u53ca\u4f7f\u7528PHP exec() \u51fd\u6570\u3001cgi-bin\u76ee\u5f55\u914d\u7f6e\u3001\u4ee5\u53ca\u5229\u7528shell\u811a\u672c<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528PHP exec() \u51fd\u6570\u662f\u6700\u76f4\u63a5\u548c\u666e\u904d\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u5141\u8bb8PHP\u811a\u672c\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\uff0c\u5e76\u4e14\u53ef\u4ee5\u6536\u96c6\u6267\u884c\u7684\u8f93\u51fa\u3002\u8fd9\u4e3a\u5728PHP-dominated\u73af\u5883\u4e2d\u52a0\u5165Python\u811a\u672c\u7684\u5f3a\u5927\u529f\u80fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f8\u5bf9\u7b80\u5355\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528PHP EXEC() \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>exec() \u51fd\u6570\u5728PHP\u4e2d\u88ab\u5e7f\u6cdb\u7528\u4e8e\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u3002\u8981\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u786e\u4fdd\u670d\u52a1\u5668\u7684\u914d\u7f6e\u5141\u8bb8exec()\u51fd\u6570\u7684\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u914d\u7f6e\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528exec()\u4e4b\u524d\uff0c\u786e\u4fddPHP\u548cPython\u73af\u5883\u5747\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6e\u3002Python\u811a\u672c\u7684\u8def\u5f84\u548c\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4\u5e94\u8be5\u6839\u636e\u670d\u52a1\u5668\u73af\u5883\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884cPython\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u6267\u884cPython\u811a\u672c\u7684\u57fa\u672c\u8bed\u6cd5\u7c7b\u4f3c\u4e8eexec(&quot;python your_script.py&quot;)\u3002\u8fd9\u884c\u4ee3\u7801\u4f1a\u542f\u52a8Python\u89e3\u91ca\u5668\uff0c\u5e76\u6267\u884c\u6307\u5b9a\u7684\u811a\u672c\u3002\u5982\u679c\u811a\u672c\u9700\u8981\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u4e2d\u6dfb\u52a0\u53c2\u6570\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982exec(&quot;python your_script.py argument1 argument2&quot;)\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p><strong>\u4e8c\u3001\u5728CGI-BIN\u76ee\u5f55\u4e0b\u8fd0\u884cPYTHON\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u670d\u52a1\u5668\u4ee5\u901a\u8fc7CGI\u534f\u8bae\u8fd0\u884cPython\u811a\u672c\u662f\u53e6\u4e00\u79cd\u65b9\u6cd5\u3002CGI-bin\u76ee\u5f55\u662f\u8bb8\u591aWeb\u670d\u52a1\u67b6\u6784\u9884\u7559\u7ed9\u811a\u672c\u6267\u884c\u7684\u7279\u6b8a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8bbe\u7f6eCGI\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u901a\u8fc7CGI\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u786e\u4fdd\u670d\u52a1\u5668\u652f\u6301CGI\u5904\u7406\uff0c\u5e76\u4e14Python\u811a\u672c\u5b58\u653e\u5728cgi-bin\u76ee\u5f55\u4e0b\u3002\u8fd8\u9700\u8981\u5728Python\u811a\u672c\u9996\u884c\u6dfb\u52a0#!\u8def\u5f84\/to\/python\uff0c\u4ee5\u6307\u793a\u7cfb\u7edf\u4f7f\u7528\u6b63\u786e\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u548c\u6743\u9650\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u6587\u4ef6\uff08\u5982Apache\u7684httpd.conf\uff09\u53ef\u80fd\u9700\u8981\u66f4\u65b0\uff0c\u4ee5\u5141\u8bb8CGI\u811a\u672c\u7684\u6267\u884c\u3002\u786e\u4fdd\u6587\u4ef6\u6743\u9650\u548c\u6240\u6709\u6743\u8bbe\u7f6e\u6b63\u786e\uff0c\u4ee5\u4fbfWeb\u670d\u52a1\u5668\u7528\u6237\u53ef\u4ee5\u6267\u884c\u8fd9\u4e9b\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p><strong>\u4e09\u3001\u901a\u8fc7SHELL\u811a\u672c\u95f4\u63a5\u8c03\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u4ecePHP\u8c03\u7528Python\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b\u6216\u5b58\u5728\u5b89\u5168\u987e\u8651\uff0c\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Shell\u811a\u672c\u4f5c\u4e3a\u4e2d\u95f4\u4ef6\u6765\u8c03\u7528Python\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efaShell\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aShell\u811a\u672c\uff0c\u8be5\u811a\u672c\u8d1f\u8d23\u542f\u52a8Python\u811a\u672c\u3002\u8fd9\u79cd\u65b9\u6cd5\u589e\u52a0\u4e86\u4e00\u5c42\u62bd\u8c61\uff0c\u4f46\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u7075\u6d3b\u6027\uff0c\u4f8b\u5982\u5728\u6267\u884cPython\u811a\u672c\u4e4b\u524d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ecePHP\u8c03\u7528Shell\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Shell\u811a\u672c\u548c\u76f4\u63a5\u6267\u884cPython\u811a\u672c\u975e\u5e38\u76f8\u4f3c\uff0c\u552f\u4e00\u7684\u533a\u522b\u662f\u6307\u5b9a\u7684\u547d\u4ee4\u53d1\u751f\u4e86\u53d8\u5316\u3002\u4f8b\u5982exec(&quot;sh your_shell_script.sh&quot;)\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p><strong>\u56db\u3001\u5b89\u5168\u6027\u548c\u6027\u80fd\u8003\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u8fd0\u884c\u5916\u90e8\u811a\u672c\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u5168\u6027\u63aa\u65bd<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u53ea\u5141\u8bb8\u53ef\u4fe1\u7684\u811a\u672c\u6267\u884c\u5e76\u4e14\u5bf9\u4f20\u5165\u7684\u53c2\u6570\u8fdb\u884c\u8fc7\u6ee4\uff0c\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u7531\u4e8e\u8c03\u7528\u5916\u90e8\u811a\u672c\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\uff0c\u5e94\u5f53\u5408\u7406\u5b89\u6392\u811a\u672c\u6267\u884c\u7684\u9891\u7387\u548c\u65f6\u95f4\uff0c\u5c3d\u53ef\u80fd\u901a\u8fc7\u7f13\u5b58\u7ed3\u679c\u51cf\u5c11\u5bf9\u5916\u90e8\u811a\u672c\u7684\u8c03\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u5f00\u53d1\u7684\u7075\u6d3b\u6027\uff0c\u4e5f\u4e3a\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u5de5\u5177\u9009\u9879\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\u6761\u4ef6\u9009\u62e9\u6700\u9002\u5b9c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728PHP\u670d\u52a1\u5668\u4e0a\u5982\u4f55\u914d\u7f6e\u548c\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u6765\u914d\u7f6e\u548c\u8fd0\u884cPython\u811a\u672c\u5728PHP\u670d\u52a1\u5668\u4e0a\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u4e86Python\u89e3\u91ca\u5668\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c<code>python --version<\/code>\u547d\u4ee4\u6765\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u6b21\uff0c\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u4f60\u7684Python\u4ee3\u7801\u7684\u811a\u672c\u6587\u4ef6\uff0c\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684PHP\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2aPHP\u6587\u4ef6\uff0c\u7528\u4e8e\u6267\u884cPython\u811a\u672c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6216<code>shell_exec()<\/code>\u51fd\u6570\u6765\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\uff0c\u4f7f\u7528<code>chmod<\/code>\u547d\u4ee4\u6765\u786e\u4fddPython\u811a\u672c\u6587\u4ef6\u6709\u6267\u884c\u6743\u9650\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u8fd0\u884c<code>chmod +x your_script.py<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u901a\u8fc7\u8bbf\u95eePHP\u6587\u4ef6\u7684URL\u6765\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd0\u884cPython\u811a\u672c\u65f6\u8981\u5c0f\u5fc3\u5b89\u5168\u95ee\u9898\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u9650\u5236\u4e86\u7528\u6237\u7684\u8f93\u5165\u548c\u6267\u884c\u6743\u9650\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528Web\u670d\u52a1\u5668\u4e2d\u7684PHP\u8c03\u7528\u5e76\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u5728Web\u670d\u52a1\u5668\u4e2d\u4f7f\u7528PHP\u8c03\u7528\u548c\u8fd0\u884cPython\u811a\u672c\u7684\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u786e\u4fddPHP\u5b89\u88c5\u4e86<code>exec()<\/code>\u51fd\u6570\u548c<code>shell_exec()<\/code>\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u6b21\uff0c\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u4f60\u7684Python\u4ee3\u7801\u7684\u811a\u672c\u6587\u4ef6\uff0c\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6216<code>shell_exec()<\/code>\u51fd\u6570\u6765\u6267\u884cPython\u811a\u672c\u3002\u4f8b\u5982\uff1a<code>exec(&quot;python3 \/path\/to\/your_script.py&quot;);<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\uff0c\u901a\u8fc7\u8bbf\u95ee\u5305\u542b\u8c03\u7528Python\u811a\u672c\u7684PHP\u6587\u4ef6\u7684URL\u6765\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8bf7\u786e\u4fdd\u4f60\u5df2\u7ecf\u9650\u5236\u4e86\u7528\u6237\u7684\u8f93\u5165\uff0c\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6216<code>shell_exec()<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u5176\u4ed6\u65b9\u6cd5\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528Python\u7684CGI\uff08\u516c\u5171\u7f51\u5173\u63a5\u53e3\uff09\u6a21\u5757\u6765\u5728Web\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728PHP\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u5e76\u914d\u7f6eWSGI\uff08Web\u670d\u52a1\u5668\u7f51\u5173\u63a5\u53e3\uff09\u6a21\u5757\uff0c\u5c06Python\u811a\u672c\u96c6\u6210\u5230PHP\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982PHP-FFI\uff08Foreign Function Interface\uff09\uff0c\u5c06Python\u811a\u672c\u4f5c\u4e3aPHP\u6269\u5c55\u6765\u8c03\u7528\u548c\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5229\u7528PHP\u7684sockets\u6269\u5c55\u4e0ePython\u811a\u672c\u8fdb\u884c\u901a\u4fe1\uff0c\u5b9e\u73b0\u4ea4\u4e92\u548c\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8bf7\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u786e\u4fdd\u5728\u6267\u884cPython\u811a\u672c\u65f6\u8003\u8651\u5230\u5b89\u5168\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728PHP\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u662f\u4e00\u79cd\u6574\u5408\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u4f18\u52bf\u7684\u6709\u6548\u65b9\u6cd5\uff0c\u4e3b\u8981\u6d89\u53ca\u4f7f\u7528PHP exec() \u51fd [&hellip;]","protected":false},"author":3,"featured_media":181795,"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\/181783"}],"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=181783"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/181783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/181795"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=181783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=181783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=181783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}