{"id":8836,"date":"2023-08-23T16:04:49","date_gmt":"2023-08-23T08:04:49","guid":{"rendered":"\/?p=8836"},"modified":"2023-08-23T17:25:26","modified_gmt":"2023-08-23T09:25:26","slug":"%e6%9c%80%e5%85%a8%e6%80%bb%e7%bb%93%ef%bc%81%e8%81%8a%e8%81%8a-python-%e8%b0%83%e7%94%a8-js-%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"\/?p=8836","title":{"rendered":"\u6700\u5168\u603b\u7ed3\uff01\u804a\u804a Python \u8c03\u7528 JS \u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"<h2>1. \u524d\u8a00<\/h2>\n<p>\u65e5\u5e38 Web \u7aef\u722c\u866b\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u53c2\u6570\u88ab\u52a0\u5bc6\u7684\u573a\u666f\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7f51\u9875\u6e90\u4ee3\u7801<\/p>\n<p>\u901a\u8fc7\u8c03\u5f0f\uff0c\u4e00\u5c42\u5c42\u5265\u79bb\u51fa\u5173\u952e\u7684 JS \u4ee3\u7801\uff0c\u4f7f\u7528 Python \u53bb\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5f97\u51fa\u53c2\u6570\u52a0\u5bc6\u524d\u540e\u7684 Python \u5b9e\u73b0<\/p>\n<p>\u672c\u6587\u5c06\u804a\u804a\u5229\u7528 Python \u8c03\u7528 JS \u76844\u79cd\u65b9\u5f0f<\/p>\n<h2>2. \u51c6\u5907<\/h2>\n<p>\u4ee5\u4e00\u6bb5\u7b80\u5355\u7684 JS \u811a\u672c\u4e3a\u4f8b\uff0c\u5c06\u4ee3\u7801\u5199\u5165\u5230\u6587\u4ef6\u4e2d<\/p>\n<pre><code class=\"language-javascript\">\/\/norm.js\n\/\/\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\nfunction add(num1, num2) {  \n    return num1 + num2;\n}<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c<\/p>\n<h2>3. \u65b9\u5f0f\u4e00\uff1aPyExecJS<\/h2>\n<p>PyExecJS \u662f\u4f7f\u7528\u6700\u591a\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5e95\u5c42\u5b9e\u73b0\u65b9\u5f0f\u662f\uff1a\u5728\u672c\u5730 JS \u73af\u5883\u4e0b\u8fd0\u884c JS \u4ee3\u7801<\/p>\n<p>\u652f\u6301\u7684 JS \u73af\u5883\u5305\u542b\uff1aNode.js\u3001PyV8\u3001PhantomJS\u3001Nashorn \u7b49<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4f9d\u8d56\u5305 PyExecJS<\/p>\n<pre><code>\/\/py_exec_js_demo.py<\/code><\/pre>\n<p>\/\/\u5b89\u88c5\u4f9d\u8d56<br \/>\n<code>pip3 install PyExecJS<\/code><\/p>\n<p>\u7136\u540e\uff0c\u4ece JS \u6587\u4ef6\u4e2d\u8bfb\u53d6\u6e90\u7801<\/p>\n<pre><code class=\"language-python\">def js_from_file(file_name):  \n    &quot;&quot;&quot;  \u8bfb\u53d6js\u6587\u4ef6  :return:  \n    &quot;&quot;&quot;  \n    with open(file_name, &#039;r&#039;, encoding=&#039;UTF-8&#039;) as file:    \n        result = file.read()\n    return result<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u4f7f\u7528 execjs \u7c7b\u7684compile()\u65b9\u6cd5\u7f16\u8bd1\u52a0\u8f7d\u4e0a\u9762\u7684 JS \u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u4e00\u4e2a\u4e0a\u4e0b\u6587\u5bf9\u8c61<\/p>\n<pre><code class=\"language-python\">import execjs\nfrom js_code import *\n# \u7f16\u8bd1\u52a0\u8f7djs\u5b57\u7b26\u4e32\ncontext1 = execjs.compile(js_from_file(&#039;.\/norm.js&#039;))<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u8c03\u7528\u4e0a\u4e0b\u6587\u5bf9\u8c61\u7684call() \u65b9\u6cd5\u6267\u884c JS \u65b9\u6cd5<\/p>\n<p>\u5176\u4e2d\uff0c\u53c2\u6570\u5305\u542b\uff1aJS \u4ee3\u7801\u88ab\u8c03\u7684\u65b9\u6cd5\u540d\u3001\u5bf9\u5e94\u65b9\u6cd5\u7684\u4f20\u5165\u53c2\u6570<\/p>\n<pre><code class=\"language-python\"># \u8c03\u7528js\u4ee3\u7801\u4e2d\u7684add()\u65b9\u6cd5\uff0c\u53c2\u6570\u4e3a2\u548c3\n# \u65b9\u6cd5\u540d\uff1aadd\n# \u53c2\u6570\uff1a2\u548c3\nresult1 = context1.call(&quot;add&quot;, 2, 3)\nprint(result1)<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\uff0c<font color='red'>\u7531\u4e8e PyExecJS \u8fd0\u884c\u5728\u672c\u5730 JS \u73af\u5883\u4e0b\uff0c\u4f7f\u7528\u4e4b\u524d\u4f1a\u542f\u52a8 JS \u73af\u5883\uff0c\u6700\u7ec8\u5bfc\u81f4\u8fd0\u884c\u901f\u5ea6\u4f1a\u504f\u6162<\/font><\/p>\n<p>\u66f4\u591a\u529f\u80fd\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/doloopwhile\/PyExecJS\">https:\/\/github.com\/doloopwhile\/PyExecJS<\/a><\/p>\n<h2>4. \u65b9\u5f0f\u4e8c\uff1ajs2py<\/h2>\n<p><font color='red'>js2py<\/font>\u4f5c\u4e3a\u4e00\u4e2a\u7eaf Python \u5b9e\u73b0\u7684 JS \u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u5b8c\u5168\u8131\u79bb JS \u73af\u5883\uff0c\u76f4\u63a5\u5c06 JS \u4ee3\u7801\u8f6c\u6362\u4e3a Python \u4ee3\u7801<\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5\u4f9d\u8d56\u5e93<\/p>\n<pre><code># \u5b89\u88c5\u4f9d\u8d56\u5e93\npip3 install js2py<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528 <code>js2py<\/code> \u4e2d\u7684<code>EvalJs()<\/code>\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u4e0a\u4e0b\u6587\u5bf9\u8c61<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u83b7\u53d6\u4e0a\u4e0bjs2py\u751f\u6210\u4e00\u4e2a\u4e0a\u4e0b\u6587\u73af\u5883\ncontext = js2py.EvalJs()<\/code><\/pre>\n<p>\u63a5\u7740\u5229\u7528\u4e0a\u4e0b\u6587\u5bf9\u8c61\u6267\u884c JS \u811a\u672c\uff0c\u8f6c\u6362\u4e3a Python \u4ee3\u7801<\/p>\n<pre><code class=\"language-python\"># \u6267\u884c\u6574\u6bb5JS\u4ee3\u7801\ncontext.execute(js_content)<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u5229\u7528\u4e0a\u4e0b\u6587\u8c03\u7528 JS \u4e2d\u7684\u65b9\u6cd5\uff0c\u5e76\u5236\u5b9a\u8f93\u5165\u53c2\u6570\u5373\u53ef<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u4e0a\u4e0b\u6587context\u8c03\u7528\u5177\u4f53\u7684\u51fd\u6570\n# \u51fd\u6570\u540d\uff1aadd\n# \u53c2\u6570\uff1a1\uff0c2\nresult = context.add(1, 2)\nprint(result)<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u662f\uff0c\u5982\u679c JS \u662f\u5f88\u957f\u7684\u6df7\u6dc6\u4ee3\u7801\uff0c\u8f6c\u6362\u4e3a Python \u7684\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u62a5\u9519<\/p>\n<p>\u66f4\u591a\u529f\u80fd\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/PiotrDabkowski\/Js2Py\">https:\/\/github.com\/PiotrDabkowski\/Js2Py<\/a><\/p>\n<h2>5. \u65b9\u5f0f\u4e09\uff1aNode.js<\/h2>\n<p>\u5b9e\u9645\u4e0a\u662f\u4f7f\u7528 Python \u7684os.popen\u6267\u884c node \u547d\u4ee4\uff0c\u6267\u884c JS \u811a\u672c<\/p>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u672c\u5730\u5df2\u7ecf\u5b89\u88c5\u4e86 Node.js \u73af\u5883<\/p>\n<p>\u4fee\u6539 JS \u811a\u672c\uff0c\u65b0\u589e\u4e00\u4e2a\u5bfc\u51fa\u51fd\u6570 init \uff0c\u65b9\u4fbf\u5185\u90e8\u51fd\u6570\u88ab\u8c03\u7528<\/p>\n<pre><code class=\"language-javascript\">\/\/\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\nfunction add(num1, num2) {\n    return num1 + num2;\n}\n\/\/\u65b0\u589e\u4e00\u4e2a\u5bfc\u51fa\u51fd\u6570\uff08node\u65b9\u5f0f\uff09\nmodule.exports.init = function (arg1, arg2) {\n    \/\/\u8c03\u7528\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\n    console.log(add(arg1, arg2));\n};<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5c06\u8c03\u7528 JS \u65b9\u6cd5\u7684\u547d\u4ee4\u7ec4\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-python\"># \u7ec4\u6210\u8c03\u7528js\u7684\u547d\u4ee4\n# node\u547d\u4ee4\uff1anode -e\ncmd = &#039;node -e &quot;require(\\\\&quot;%s\\\\&quot;).init(%s,%s)&quot;&#039; % (&#039;.\/norm&#039;, 3, 5)<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u901a\u8fc7 os.popen \u6267\u884c\u547d\u4ee4\u5373\u53ef<\/p>\n<pre><code class=\"language-python\">pipeline = os.popen(cmd)# \u8bfb\u53d6\u7ed3\u679c\nresult = pipeline.read()print(&#039;\u7ed3\u679c\u662f:&#039;, result)<\/code><\/pre>\n<h2>6. \u65b9\u5f0f\u56db\uff1aPyV8<\/h2>\n<p><code>PyV8<\/code> \u662f Google \u5c06 Chrome V8 \u5f15\u64ce\u7528 Python \u5c01\u88c5\u7684\u4f9d\u8d56\u5e93<\/p>\n<p>\u5b83\u4e0d\u4f9d\u8d56\u672c\u5730 JS \u73af\u5883\uff0c\u8fd0\u884c\u901f\u5ea6\u5f88\u5feb<\/p>\n<pre><code class=\"language-python\">import PyV8\nfrom js_code import js_from_file\nwith PyV8.JSContext() as ctx:\n    ctx.eval(js_from_file(&#039;.\/norm.js&#039;))\n# \u8c03\u7528js\u51fd\u6570\uff0c\u6307\u5b9a\u53c2\u6570\nctx.locals.add(1, 2)<\/code><\/pre>\n<p>\u4f46\u662f\u7ecf\u8fc7\u53cd\u590d\u6d4b\u8bd5\u53d1\u73b0\uff0cMAC \u548c PC \u5728 Python3 \u73af\u5883\u4e0b\uff0c\u4f7f\u7528 PyV8 \u4f1a\u62a5\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u4e0d\u63a8\u8350\u4f7f\u7528\uff01<\/p>\n<p>\u66f4\u591a\u529f\u80fd\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/emmetio\/pyv8-binaries\">https:\/\/github.com\/emmetio\/pyv8-binaries<\/a><\/p>\n<h2>7. \u6700\u540e<\/h2>\n<p>\u4e0a\u9762\u603b\u7ed3\u4e86 Python \u8c03\u7528 JS \u7684 4 \u79cd\u65b9\u5f0f<\/p>\n<p>\u5b9e\u9645\u722c\u866b\u9879\u76ee\u4e2d\uff0c\u4e00\u822c\u4f1a\u5148\u4f7f\u7528 node \u547d\u4ee4\u8fdb\u884c\u4e00\u6b21\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6ca1\u95ee\u9898\u540e\uff0c\u518d\u4f7f\u7528\u524d 3 \u79cd\u65b9\u5f0f\u7684\u4efb\u610f\u4e00\u79cd\u8fdb\u884c Python \u6539\u5199<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u524d\u8a00 \u65e5\u5e38 Web \u7aef\u722c\u866b\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u53c2\u6570\u88ab\u52a0\u5bc6\u7684\u573a\u666f\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5206\u6790\u7f51\u9875\u6e90\u4ee3\u7801 \u901a\u8fc7\u8c03\u5f0f\uff0c\u4e00\u5c42\u5c42\u5265\u79bb\u51fa\u5173\u952e\u7684 JS \u4ee3   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[89,189],"tags":[78,16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8836"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8836"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8836\/revisions"}],"predecessor-version":[{"id":8838,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8836\/revisions\/8838"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8836"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}