{"id":145154,"date":"2019-05-25T08:11:33","date_gmt":"2019-05-25T00:11:33","guid":{"rendered":"https:\/\/gulass.cn\/?p=145154"},"modified":"2019-05-20T10:14:18","modified_gmt":"2019-05-20T02:14:18","slug":"ppython-php-python","status":"publish","type":"post","link":"https:\/\/gulass.cn\/ppython-php-python.html","title":{"rendered":"PPython\uff0c\u4e00\u79cd\u4ece\u6839\u672c\u4e0a\u5c06 PHP \u4e0e Python \u6709\u6548\u7ed3\u5408\u7684\u6280\u672f"},"content":{"rendered":"
Python \u4e0e PHP \u90fd\u662f\u5e7f\u6cdb\u4f7f\u7528\u7684\u8bed\u8a00\uff0c\u5404\u6709\u6240\u957f\uff0c\u8ba9\u4eba\u671f\u5f85\u4e24\u8005\u7ed3\u5408\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u6548\u679c\u3002<\/p>\n
\u5728 PHP \u4e2d\u8c03\u7528 Python \u5b9e\u73b0\u67d0\u4e9b\u5904\u7406\uff0c\u8fd9\u79cd\u9700\u6c42\u867d\u7136\u6bd4\u8f83\u5c0f\u4f17\uff0c\u8fd8\u662f\u5b9e\u7528\u7684\u3002\u76ee\u524d\u7f51\u4e0a\u53ef\u4ee5\u67e5\u5230\u5f88\u591a\u8d44\u6599\u4ecd\u5728\u63a2\u8ba8 exec()\uff08\u4e5f\u5305\u62ec system()\u3001shell_exec()\u3001passthru() \u7b49\uff09\u6267\u884c\u5916\u90e8\u7684 Python \u6587\u4ef6\uff0c\u4f46\u8fd9\u53ea\u662f\u4e00\u79cd\u901a\u7528\u7684\u65b9\u5f0f\uff0c\u8c03\u7528\u6210\u672c\u6bd4\u8f83\u9ad8\uff0c\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\uff0c\u9700\u8981\u88c5\u8f7d\u6574\u4e2a Python \u89e3\u91ca\u73af\u5883\u3002<\/p>\n
\u6709\u6b64\u7c7b\u9700\u6c42\u7684\u5f00\u53d1\u8005\u975e\u5e38\u9002\u5408\u770b\u4e00\u4e0b PPython\uff0c\u8fd9\u662f\u4e00\u79cd\u4ece\u6839\u672c\u4e0a\u5c06 PHP \u4e0e Python \u6709\u6548\u7ed3\u5408\u7684\u6280\u672f\u3002<\/p>\n
PPython \u6700\u521d\u89c1\u4e8e https:\/\/code.google.com\/p\/ppython\uff0c\u8be5\u4f5c\u8005\u5c06 lajp\uff08\u4e00\u79cd PHP \u7ed3\u5408 Java \u7684\u6280\u672f\uff09\u79fb\u690d\u5230\u4e86 Python \u4e0a\u3002<\/p>\n
\u8be5\u9879\u76ee\u6700\u521d\u5efa\u7acb\u4e8e 2012 \u5e74\uff0c\u800c\u4e14\u4f3c\u4e4e\u5df2\u7ecf\u505c\u6b62\u7ef4\u62a4\u591a\u5e74\uff0c\u4e0d\u8fc7\u76ee\u524d\u6765\u770b\u5176\u601d\u8def\u53ca\u6548\u679c\u8fd8\u662f\u503c\u5f97\u80af\u5b9a\u7684\uff0c\u56e0\u6b64\u5c06\u6b64\u9879\u76ee\u4ece\u505c\u6b62\u8fd0\u8425\u7684 Google Code \u4e0a\u8fc1\u79fb\u5230\u4e86 GitHub\uff0c\u5e76\u9075\u5faa\u539f Apache \u8bb8\u53ef\u8bc1\u91cd\u65b0\u53d1\u5e03\u548c\u7ef4\u62a4\u3002<\/p>\n
\u65e5\u524d\u7b14\u8005\u5bf9\u6b64\u4f5c\u4e86\u4e00\u756a\u5c1d\u8bd5\uff0c\u5bf9 PPython \u7684\u65b9\u4fbf\u6613\u7528\u6709\u6240\u4f53\u4f1a\u3002<\/p>\n
PHP \u4e0e Python \u901a\u8baf\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5957\u63a5\u5b57\u673a\u5236\uff1aTCP \u5957\u63a5\u5b57\u548c UNIX \u5957\u63a5\u5b57\u3002UNIX \u5957\u63a5\u5b57\u662f Unix\/Linux \u672c\u5730\u5957\u63a5\u5b57\uff0c\u76f8\u5bf9\u4e8e TCP \u5957\u63a5\u5b57\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n
PHP \u548c Python \u5404\u6709\u5176\u8bed\u8a00\u5185\u90e8\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff0c\u901a\u5e38 PHP \u8fdb\u7a0b\u4e0e Python \u8fdb\u7a0b\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u65f6\uff0c\u9700\u8981\u8fdb\u884c\u8f6c\u7801\u5904\u7406\u3002\u6b64\u7c7b\u8f6c\u6362\u5982\u7531\u5e94\u7528\u81ea\u884c\u5b9e\u73b0\uff0c\u4ece\u5f00\u53d1\u6548\u7387\u5230\u8fd0\u884c\u6027\u80fd\u90fd\u4f1a\u589e\u52a0\u4e0d\u5c11\u989d\u5916\u8d1f\u62c5\u3002<\/p>\n
PPython \u5bf9 PHP \u548c Python \u95f4\u7684\u901a\u8baf\u65b9\u5f0f\u7684\u5904\u7406\u652f\u6301 TCP \u5957\u63a5\u5b57\u548c UNIX \u5957\u63a5\u5b57\u4e24\u79cd\u673a\u5236\uff0c\u517c\u987e\u901a\u8baf\u6548\u7387\u548c\u5206\u5e03\u5f0f\uff0c\u8f6c\u7801\u7531\u670d\u52a1\u7edf\u4e00\u5904\u7406\uff0cPython \u4e3a PHP \u7684\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u683c\u5f0f\u517c\u5bb9\uff0c\u4f7f PHP \u7aef\u5f00\u53d1\u65e0\u987b\u4e3a\u5e95\u5c42\u901a\u8baf\u62c5\u5fc3\u3002<\/p>\n
Python \u56e0\u5176\u8bed\u8a00 GIL \u7279\u6027\uff0c\u540c\u4e00\u8fdb\u7a0b\u5185\u591a\u7ebf\u7a0b\u6548\u7387\u4e0d\u9ad8\u3002PPython \u53ef\u6839\u636e\u9879\u76ee\u9700\u8981\u90e8\u7f72\u670d\u52a1\uff0c\u591a\u8fdb\u7a0b\u8fd0\u884c Python\uff0c\u63d0\u9ad8\u5e94\u7528\u7efc\u5408\u6027\u80fd\u3002<\/p>\n
<\/p>\n
PPython \u7684\u4ee3\u7801\u53ef\u4ece\u4e0a\u8ff0\u9879\u76ee\u4ed3\u5e93\u4e2d\u4e0b\u8f7d\u3002<\/p>\n
\u4e0b\u8f7d\u5f97\u5230\u7684\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4e0b\u4e09\u4e2a\u662f PPython \u7684\u6838\u5fc3\u4ee3\u7801\uff0c\u4f5c\u7528\u5982\u4e0b\uff1a<\/p>\n
\u5c06\u4ee5\u4e0a\u6587\u4ef6\u653e\u7f6e\u5230\u4efb\u610f\u76ee\u5f55\u3002\u5148\u4fee\u6539\u51c6\u5907\u8fd0\u884c PPython \u7684\u7aef\u53e3\uff0c\u76d1\u542c\u7aef\u53e3\u4e0d\u9650\uff0c\u53ea\u8981 php_python.py \u548c php_python.php \u4e24\u7aef\u4fee\u6539\u4e00\u81f4\u3002\u7b14\u8005\u7edf\u4e00\u6539\u4e3a 10240\u3002<\/p>\n
\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u8fd0\u884c php_python.py\uff0c\u53ea\u8981 Python \u73af\u5883\u6b63\u5e38\uff0c\u4fbf\u5c06\u8fd0\u884c\u8d77\u4e00\u4e2a PPython \u7684\u670d\u52a1\u3002<\/p>\n
-------------------------------------------\r\n- PPython Service\r\n- Time: 2019-05-13 22:24:09\r\n-------------------------------------------\r\nListen port: 10240\r\ncharset: utf-8\r\nServer startup...<\/pre>\nPHP \u7aef\u5f15\u5165 php_python.php\uff0c\u5c31\u53ef\u4ee5\u7528 ppython \u51fd\u6570\u4e0e\u4e4b\u524d\u542f\u52a8\u7684 PPython \u670d\u52a1\u901a\u8baf\uff0c\u4f20\u5165\u8bf7\u6c42\u7531 PPython \u670d\u52a1\u8c03\u7528 Python \u5904\u7406\u540e\u8fd4\u56de\u7ed3\u679c\uff0c\u5982 $res = ppython('test::go') \u662f\u8c03\u7528test.py \u4e2d\u7684 go \u51fd\u6570\uff0c\u4e5f\u53ef\u52a0\u4e0a\u66f4\u591a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8d77\u5c06\u4e3a\u88ab\u8c03\u7684\u51fd\u6570\u4f20\u9012\u66f4\u591a\u53c2\u6570\u3002<\/p>\n
php_python.py \u662f PPython \u542f\u52a8\u540e\u76f4\u63a5\u8fd0\u884c\u7684\u5168\u5c40\u4ee3\u7801\uff0c\u6709\u5168\u5c40\u914d\u7f6e\u6216\u8fdb\u7a0b\u542f\u52a8\u540e\u7684\u901a\u7528\u5904\u7406\u90fd\u5199\u5728\u8fd9\u91cc\uff0c\u5982\u539f\u751f\u4ee3\u7801\u4e2d\u5efa\u7acb\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u7b49\uff0c\u9879\u76ee\u4e2d\u5e94\u89c6\u60c5\u51b5\u4f5c\u4f18\u5316\u3002<\/p>\n
\u4f46 Python \u4ee4\u4eba\u611f\u5174\u8da3\u7684\u4e3b\u8981\u65b9\u9762\u4e0d\u53ea\u662f\u50cf PHP \u90a3\u6837\u63cf\u8ff0\u4e1a\u52a1\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5728\u4eba\u5de5\u667a\u80fd\u7b49\u9886\u57df\u6240\u9700\u8981\u7684\u8ba1\u7b97\u578b\u4efb\u52a1\u63d0\u4f9b\u5bf9\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u7684\u5904\u7406\uff0c\u56e0\u6b64\u4e8c\u8005\u7684\u7ed3\u5408\u53ef\u4ee5\u7ed9 PHP \u5e26\u6765\u66f4\u591a\u5e94\u7528\u573a\u666f\u3002<\/p>\n
\u6539\u8fdb<\/strong><\/div>\n\u6b64\u5916\uff0c\u539f\u751f\u7684 php_python.py \u8fd8\u6709\u4e9b\u4e0d\u8db3\u3002\u7b14\u8005\u7528 ppython \u8c03\u7528\u81ea\u5b9a\u4e49\u4ee3\u7801\u4e2d\u9047\u5230\u4e86\u4e09\u4e2a\u95ee\u9898\uff0c\u5e76\u76f8\u5e94\u505a\u4e86\u89e3\u51b3\uff1a<\/p>\n
\n
- \u4e0d\u652f\u6301 complex\uff08\u590d\u6570\u7c7b\uff09\uff0c\u590d\u6570\u662f\u6570\u5b66\u4e0a\u7684\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4e3b\u8981\u5305\u62ec real\uff08\u5b9e\u90e8\uff09\u548c imag\uff08\u865a\u90e8\uff09\u6570\u636e\uff0c\u867d\u7136\u65e5\u5e38\u751f\u6d3b\u4e2d\u9047\u5230\u8f83\u5c11\uff0c\u4f46 AI \u548c\u5404\u79cd\u4e13\u4e1a\u7814\u7a76\u9886\u57df\u6216\u5e76\u4e0d\u7f55\u89c1\u3002Python \u91cc\u6709 complex \u7c7b\uff0c\u5bf9\u590d\u6570\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u5404\u79cd\u8ba1\u7b97\uff0c\u4f46 PPython \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5bf9 complex \u6ca1\u6709\u5904\u7406\u3002\u4e3a\u4e86\u80fd\u8ba9 complex \u5305\u62ec\u7684\u6570\u636e\u80fd\u6b63\u5e38\u8fd4\u56de\uff0c\u53ea\u8981\u5728 process.py \u7684 z_encode() \u65b9\u6cd5\u4e2d\u52a0\u4e0a\u7b26\u5408 PHP \u8981\u6c42\u7684\u5e8f\u5217\u5316\u5904\u7406\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a\n
elif isinstance(p, numpy.complex128):\r\nt1 = str(p.real)\r\nt2 = str(p.imag)\r\nreturn 'O:7:\"complex\":2:{s:4:\"real\";d:%s;s:4:\"imag\";d:%s;}' % (t1,t2)<\/pre>\n<\/li>\n- \u4e0d\u652f\u6301 ndarray\uff08\u591a\u7ef4\u6570\u7ec4\uff09\u3002\u76f8\u6bd4 complex\uff0cndarray \u8981\u666e\u901a\u5f97\u591a\uff0c\u76f8\u4fe1\u51e1\u4f7f\u7528\u5230 Python \u7684\u5404\u79cd\u8ba1\u7b97\u529f\u80fd\uff0cndarray \u662f\u65e0\u6cd5\u56de\u907f\u7684\uff0c\u751a\u81f3 ndarray\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u6210\u5c31\u4e86 Python\u3002\u4f46\u539f php_python.py \u4e0d\u80fd\u8bc6\u522b ndarray\u3002\u4e0d\u8fc7\u89e3\u51b3\u8d77\u6765\u5e76\u4e0d\u96be\uff0c\u5728 process.py \u91cc\u627e\u5230z_encode() \u65b9\u6cd5\uff0c\u52a0\u4e0a\u4e0b\u9762\u8fd9\u6bb5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06 ndarray \u8f6c\u5316\u4e3a\u7b26\u5408 PHP \u8981\u6c42\u7684\u6570\u7ec4\uff08\u6570\u5b57\u7d22\u5f15\uff09\u3002\n
elif isinstance(p, numpy.ndarray):\r\ns = ''\r\ni = 0\r\nfor d in p:\r\ns += 'i:%d;%s' % (i,z_encode(d))\r\ni += 1\r\nreturn \"a:%d:{%s}\" % (len(p),s)<\/pre>\n<\/li>\n- \u539f\u4ee3\u7801\u4e0d\u592a\u7a33\u5065\uff0c\u5982\u6570\u636e\u4e3a ndarray \u7c7b\u578b\uff0cif p == None:\u62a5\u9519ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()\uff0c\u56e0\u4e3ap == None\u7684\u7ed3\u679c\u4e5f\u662fndarray\uff0c\u4e0d\u8fd4\u56defalse \uff0c\u5c06\u5224\u65ad\u65b9\u6cd5\u6539\u4e3aif p is None:\u53ef\u907f\u514d\u51fa\u9519\u3002\u76f8\u5e94\u5730 PHP \u7aef\u4e5f\u8981\u6ce8\u610f\u4e00\u4e0b\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u5904\u7406\u3002<\/li>\n<\/ol>\n
\u5904\u7406\u56de\u590d\u4e2d\u7c7b\u4f3c complex \u8fd9\u6837\u7684\u5bf9\u8c61\u6570\u636e\u65f6\uff0c\u5982\u7cfb\u7edf\u4e2d\u6ca1\u6709\u5b9a\u4e49\u76f8\u5e94\u7684\u7c7b\uff0cPHP \u662f\u53ef\u4ee5\u53cd\u5e8f\u5217\u5316\u7684\uff0c\u4f46\u5c06\u663e\u793a\u4e3a \u201cincomplete object\u201d\uff0cvardump \u770b\u5f97\u5230 real \u548c imag \u6570\u636e\uff0c\u4f46\u4e0d\u80fd\u76f4\u63a5\u64cd\u4f5c\uff0c\u81ea\u884c\u5b9a\u4e49 complex \u7c7b\u540e\uff0c\u5219\u6309\u6307\u5b9a\u7684\u7c7b\u8fdb\u884c\u89e3\u6790\uff0c\u4e0e PHP \u5185\u7684\u4e00\u822c\u5bf9\u8c61\u65e0\u5f02\uff0c\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u6240\u6709\u64cd\u4f5c\u3002<\/p>\n
\u81f3\u6b64\uff0cPHP \u4e0e Python \u7684\u529f\u80fd\u8c03\u8baf\u5df2\u65e0\u95ee\u9898\u3002<\/p>\n
\u8865\u5145\uff1a\u6ce8\u518c\u4e3a\u670d\u52a1<\/strong><\/div>\n\u547d\u4ee4\u884c\u4e0b\u542f\u52a8 php_python.py \u4e3b\u8981\u662f\u65b9\u4fbf\u8c03\u8bd5\uff0c\u53ef\u4ee5\u770b\u5230\u89c2\u5bdf\u53cd\u9988\u4fe1\u606f\u7b49\uff0c\u751f\u4ea7\u73af\u5883\u4e2d\u624b\u5de5\u542f\u52a8 PPython \u6bd5\u7adf\u4e0d\u592a\u65b9\u4fbf\u3002\u53ef\u4ee5\u5c06 PPython \u914d\u7f6e\u6210\u670d\u52a1\uff0c\u4fee\u6539\u7aef\u53e3\u4e5f\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u5e94\u7528\u914d\u7f6e\u4e0d\u540c\u7684 PPython \u7aef\u3002<\/p>\n
Linux \u4e0b\u5c06\u4e00\u4e2a\u8fdb\u7a0b\u6ce8\u518c\u4e3a\u670d\u52a1\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u5efa\u7acb \/usr\/lib\/systemd\/system\/ppython.service\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n
[Unit]\r\nDescription=PHP-Python Service\r\nAfter=network.target remote-fs.target nss-lookup.target\r\n\r\n[Service]\r\nExecStart={PPYTHON_PATH}\/php_python.py\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n\u5176\u4e2d {PPYTHON_PATH} \u8981\u6539\u6210\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\n\u6709\u4e86 PPython\uff0c\u53ef\u4ee5\u6452\u5f03 exec() \u8fd9\u79cd shell \u8c03\u7528\uff0c\u4f7f\u5f00\u53d1\u56de\u5f52\u5230\u903b\u8f91\u672c\u8eab\u3002<\/p>\n
\u4e2a\u4eba\u8ba4\u4e3a\u8be5\u65b9\u6848\u503c\u5f97\u6240\u6709\u5bf9 PHP \u548c Python \u90fd\u611f\u5174\u8da3\u7684\u5f00\u53d1\u4eba\u5458\u4e86\u89e3\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u53c2\u4e0e\u548c\u8d21\u732e\u8fd9\u4e2a\u9879\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Python \u4e0e PHP \u90fd\u662f\u5e7f\u6cdb\u4f7f\u7528\u7684\u8bed\u8a00\uff0c\u5404\u6709\u6240\u957f\uff0c\u8ba9\u4eba\u671f\u5f85\u4e24\u8005\u7ed3\u5408\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u6548\u679c\u3002 \u5728 PHP \u4e2d […]<\/p>\n","protected":false},"author":323,"featured_media":113018,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-145154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/145154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=145154"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/145154\/revisions"}],"predecessor-version":[{"id":145336,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/145154\/revisions\/145336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/113018"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=145154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=145154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=145154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}