{"id":2891,"date":"2020-11-04T16:46:53","date_gmt":"2020-11-04T08:46:53","guid":{"rendered":"https:\/\/han.pm\/?p=2891"},"modified":"2020-11-08T17:46:06","modified_gmt":"2020-11-08T09:46:06","slug":"python-%e4%b8%8b%e6%89%a7%e8%a1%8c-js-%e7%a0%b4%e8%a7%a3%e7%bd%91%e7%ab%99%e5%8a%a0%e5%af%86%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/jianghan.tech\/?p=2891","title":{"rendered":"Python \u4e0b\u6267\u884c JS \u7834\u89e3\u7f51\u7ad9\u52a0\u5bc6\u4fe1\u606f"},"content":{"rendered":"\nPython \u4e0b\u5229\u7528 ExecJS \u6267\u884c JS \u6765\u7834\u89e3\u90e8\u5206\u7f51\u7ad9\u7684\u52a0\u5bc6\uff0c\u7ed3\u8bba\uff0c\u6211\u5efa\u8bae\u522b\u8d70\u8fd9\u6761\u8def\u5f84\uff0c\u6211\u7684\u6700\u7ec8\u65b9\u6cd5\u662f \u7528 nodejs \u5199\u4e2a\u5c0f\u7a0b\u5e8f\u4e13\u95e8\u6267\u884c js, \u5c06\u7ed3\u679c\u901a\u8fc7 HTTP \u8fd4\u56de\u7ed9 Python \u4f7f\u7528\u3002\n\n\n\n \u5148\u6267\u884c pip install PyExecJS \u6709\u4e9b\u7f51\u7ad9\u4f1a\u5bf9\u4e00\u4e9b\u767b\u5f55\u548c\u8ba4\u8bc1\u76f8\u5173\u7684\u4fe1\u606f\u8fdb\u884c\u52a0\u5bc6\uff0c\u4f46\u662f\u5bc6\u6587\u90fd\u5728\u524d\u7aef\u91cc\uff0c\u6211\u9047\u5230\u7684\u60c5\u5f62\u662f\u4f7f\u7528\u7684\u662f Crypto-JS \u5e93\uff0c\u4f7f\u7528\u9ed8\u8ba4\u5bc6\u94a5\"secretkey123\" AES \u52a0\u5bc6\u3002\u4f46\u662f\u8fd9\u4e2a\u5e93\u7684 AES \u52a0\u5bc6\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684 iv\uff0c\u662f\u968f\u673a\u7684\uff0c\u5728 python \u4e0b\u7684 AES \u6ca1\u6cd5\u6a21\u62df\uff0c\u4e8e\u662f\u60f3\u5230\u7528 Python \u53bb\u8c03\u7528 JS\u3002\n\n\n\n\u6b63\u6587<br>\u80fd\u591f\u6b63\u5e38\u6267\u884c\u4e4b\u524d\u9700\u8981\u5b89\u88c5 node \u6216\u8005 PyV8 \u7b49 JS\u5f15\u64ce\uff0c\u6ca1\u6709JS\u73af\u5883\u4f1a\u62a5\u9519: \"Could not find an available JavaScript runtime.\" \u7531\u4e8e\u955c\u50cf\u662f Docker \u4e0b\u7ee7\u7eed Alpine \u7684\u7cfb\u7edf\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e0b\u547d\u4ee4\u5728 Alpine \u4e0b\u5b89\u88c5 NodeJS\n\n\n\n<code>apk add nodejs<br>apk add npm<\/code>\n\n\n\n\n<pre class=\"wp-block-code\"><code>import execjs\n\ndef get_decrypt_data(ctx, data, key):\n    jscode = 'CryptoJS.AES.decrypt(\"{0}\",\"{1}\").toString(CryptoJS.enc.Utf8)'.format(data, key)\n    return ctx.eval(jscode)\n\ndef get_decrypt_data_by_call(ctx, data, key):\n    #\u8c03\u7528js\u65b9\u6cd5  \u7b2c\u4e00\u4e2a\u53c2\u6570\u662fJS\u7684\u65b9\u6cd5\u540d\uff0c\u540e\u9762\u7684data\u548ckey\u662fjs\u65b9\u6cd5\u7684\u53c2\u6570\n    return (ctx.call('CryptoJS.AES.decrypt', data, key))\n\ndef get_encrypt_data(ctx, data, key):\n    jscode = 'CryptoJS.AES.encrypt(\"{0}\",\"{1}\")'.format(data, key)\n    print (jscode)\n    return ctx.eval(jscode)\n\ndef get_js():\n    f = open(\".\/aes.js\", 'r', encoding='utf-8') # \u6253\u5f00JS\u6587\u4ef6\n    line = f.readline()\n    jsCtx = ''\n    while line:\n        jsCtx = jsCtx+line\n        line = f.readline()\n    return jsCtx\n\n\nif __name__ == '__main__':\n    \n    name = execjs.get().name\n    print(\"JS \u5f15\u64ce\uff1a \" + name)\n    jsstr = get_js()\n    ctx = execjs.compile(jsstr) #\u52a0\u8f7dJS\u6587\u4ef6\n   \n    # print(get_decrypt_data(ctx,\"\u5bc6\u6587\", \"secretkey123\"))\n\n    print(get_encrypt_data(ctx, \"abcssssss\", \"secretkey123\"))<\/code><\/pre>\n\n\n\n\n\u5728 Node \u7684\u73af\u5883\u4e0b\uff0c\u6211\u5728 Mac \u4e0b \u548c Alpine \u73af\u5883\u4e0b\u90fd\u5c1d\u8bd5\u901a\u8fc7 node\u5f15\u64ce\u6267\u884c\u52a0\u5bc6\u4ee3\u7801\uff0c\u90fd\u629b\u51fa\"execjs._exceptions.ProgramError: None\"\u7684\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u89e3\u5bc6\u4ee3\u7801\u662f\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u7684\uff0c\u4f46\u662f\u8fd9\u4e9b\u4ee3\u7801\u5728\u6d4f\u89c8\u5668\u90fd\u597d\u597d\u7684\uff0c\u7f51\u4e0a\u6ca1\u6709\u627e\u5230\u89e3\u51b3\u65b9\u5f0f\uff0c\u4e8e\u662f\u5c1d\u8bd5\u66f4\u6362\u5df2\u7ecf\u4e3a PyV8 \u5f15\u64ce\uff0c\u56e0\u4e3a Chrome\u6d4f\u89c8\u5668\u4e5f\u662f V8 \u5f15\u64ce\u3002\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"343\" src=\"https:\/\/i0.wp.com\/han.pm\/wp-content\/uploads\/2020\/11\/image-4-1024x343.png?resize=1024%2C343&#038;ssl=1\" alt=\"\" class=\"wp-image-2892\" srcset=\"https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-4.png?resize=1024%2C343&amp;ssl=1 1024w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-4.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-4.png?resize=768%2C257&amp;ssl=1 768w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-4.png?resize=1536%2C514&amp;ssl=1 1536w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-4.png?resize=2048%2C685&amp;ssl=1 2048w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n\n\n\n\n\u4f46\u662f Alpine \u4e0b\u6ca1\u6709\u5bf9 PyV8 \u7a0b\u5e8f\u7684\u652f\u6301\uff0c\u5b89\u88c5\u7a0b\u5e8f\u90fd\u662f\u9488\u5bf9 ubuntu, debian, centos \u4e4b\u7c7b\u7684\u3002 \u76f4\u63a5 pip install \u662f\u4e0d\u884c\u7684\uff0c\u9700\u8981\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e00\u4e9b\u652f\u6301\u5305\u3002\u5c1d\u8bd5\u7f16\u8bd1\u4e86\u51e0\u4e2a\u7f51\u4e0a\u7684 Dockerfile \u5931\u8d25\u4e86\uff0c\u4e3b\u8981\u539f\u56e0\u662f\u4fe1\u606f\u8fc7\u65f6\u8fd8\u6709\u5916\u7f51\u73af\u5883\u4e86\u3002\n\n\n\ngiang\/pyv8-python3 Dockerfile - Docker Hub<br>https:\/\/hub.docker.com\/r\/giang\/pyv8-python3\/dockerfile\/\n\n\n\n\u8fd9\u4e2a Dockerfile \u9700\u8981\u4ece googleapi \u4e0a\u62c9\u53d6\u4fe1\u606f\uff0c\u7f16\u8bd1\u65f6\u95f4\u5f88\u4e45\u800c\u4e14\u8fd8\u5931\u8d25\u4e86\n\n\n\npyv8-docker\/Dockerfile at master \u00b7 new-mind\/pyv8-docker<br>https:\/\/github.com\/new-mind\/pyv8-docker\/blob\/master\/Dockerfile\n\n\n\n\u8fd9\u4e00\u4e2a\u4e5f\u5931\u8d25\u4e86\uff0c\u9996\u5148\u5b83\u662f py2.7 \u7684\u6211\u9700\u8981\u7684\u662f py3 \u7cfb\u5217\u7684\uff0cubuntu \u6700\u65b0\u7248\u9ed8\u8ba4\u6709 py3 \u7684\u652f\u6301\uff0c\u7136\u540e PyV8 \u7684 google\u4ed3\u5e93\u4e5f\u8fc7\u671f\u4e86\uff0c\u6362\u6210\u4e86 github\uff0c\u4f46\u662f wget \u83b7\u53d6\u6587\u4ef6\u4e0d\u5b8c\u6574\uff0c\u6ca1\u6cd5\u89e3\u538b\u3002\n\n\n\n\u6700\u7ec8\u6211\u76f4\u63a5\u5728\u6211\u7684 CentOS \u670d\u52a1\u5668\u4e0a\u5b89\u88c5\uff0c\u5148\u5b89\u88c5\u652f\u6301\u73af\u5883\n\n\n\n\n<pre class=\"wp-block-preformatted\">yum install boost\nyum install boost-devel\nyum install boost-doc<\/pre>\n\n\n\n\n\u7136\u540e\u4e0b\u8f7d\u5e93<br>https:\/\/raw.githubusercontent.com\/emmetio\/pyv8-binaries\/master\/pyv8-linux64-p3.zip<br>\u7528 wget \u4e0b\u8f7d\u4f1a\u6587\u4ef6\u4e0d\u5b8c\u6574\u5bfc\u81f4\u89e3\u538b\u4e0d\u4e86\uff0c\u6211\u662f\u76f4\u63a5\u6d4f\u89c8\u5668\u4e0b\u8f7d\u540e\u81ea\u5df1\u89e3\u538b\u7684\uff0c\u89e3\u538b\u540e\u5c06 _PyV8.cpython-33m.so \u91cd\u65b0\u547d\u540d\u4e3a _PyV8.so, \u5e76\u5c06_PyV8.so \u548c PyV8.py \u590d\u5236\u5230 Python3 \u7684 site-packages \u76ee\u5f55\u4e0b\uff0c\u6211\u8fd9\u91cc\u662f \/usr\/local\/lib\/python3.6\/site-packages\u3002\n\n\n\n\u4e4b\u540e\u5728 python3 \u4e0b\u6267\u884c <code>import PyV8<\/code> \u6ca1\u6709\u62a5\u9519\u8868\u793a\u5b89\u88c5\u6210\u529f<br>\u901a\u8fc7 os.environ[\"EXECJS_RUNTIME\"] = \"Node\" \u53ef\u4ee5\u9009\u5b9a ExecJS \u7684 JS\u5f15\u64ce\uff0c\u7136\u800c PyV8 \u540c\u6837\u51fa\u73b0\u81f4\u547d\u9519\u8bef Segmentation fault\u3002\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"133\" src=\"https:\/\/i0.wp.com\/han.pm\/wp-content\/uploads\/2020\/11\/image-5-1024x133.png?resize=1024%2C133&#038;ssl=1\" alt=\"\" class=\"wp-image-2893\" srcset=\"https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-5.png?resize=1024%2C133&amp;ssl=1 1024w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-5.png?resize=300%2C39&amp;ssl=1 300w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-5.png?resize=768%2C99&amp;ssl=1 768w, https:\/\/i0.wp.com\/jianghan.tech\/wp-content\/uploads\/2020\/11\/image-5.png?w=1422&amp;ssl=1 1422w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n\n\n\n\n\u4e00\u4e9b\u5176\u4ed6\u7684\u5f15\u64ce\u6bd4\u5982 PhantomJS \u4e4b\u7c7b\u7684\uff0c\u6211\u4e5f\u6298\u817e\u4e86\u4e00\u4e0b\uff0c\u90fd\u4e0d\u5927\u65b9\u4fbf\u3002\u6700\u7ec8\u6211\u51b3\u5b9a\u81ea\u5df1\u5199 Node\u7a0b\u5e8f\u6765\u4e13\u95e8\u4e3a Python\u63d0\u4f9b\u52a0\u5bc6\u670d\u52a1<br>\n\n\n\n\n<pre class=\"wp-block-code\"><code>var CryptoJS = require(\"crypto-js\");\n\nvar express = require('express');\nvar app = express();\n\nvar bodyParser = require('body-parser');\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({extended: true}));\n\napp.get('\/', function (req, res) {\n     res.send('hello');\n});\n\napp.post('\/encrypt', function(req, res){\n  \/\/ console.log(req);\n    console.log(req.body);\n    var data = req.body.data;\n    var encrypt = CryptoJS.AES.encrypt(data, \"secretkey123\");\n    console.log(encrypt.toString());\n    res.send(encrypt.toString());\n});\n\nvar server = app.listen(8300, function() {\n    console.log('Listening on port %d', server.address().port);\n});<\/code><\/pre>\n\n\n\n\n\u5b8c\u7f8e\u89e3\u51b3\u95ee\u9898\u3002\n\n\n\n<br>\u53c2\u8003\uff1a\n\n\n\nPython\u722c\u866b\u6742\u8bb0 - python\u8fd0\u884cjs - \u7b80\u4e66<br>https:\/\/www.jianshu.com\/p\/e01a3d504700\n\n\n\n\u3010Python3\u3011 PyV8\u7684\u5b89\u88c5\u4e0e\u4f7f\u7528 - \u4f9d\u7136\u8303\u513f\u7279\u897f - \u535a\u5ba2\u56ed<br>https:\/\/www.cnblogs.com\/richerdyoung\/p\/8535149.html\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e0b\u5229\u7528 ExecJS \u6267\u884c JS \u6765\u7834\u89e3\u90e8\u5206\u7f51\u7ad9\u7684\u52a0\u5bc6\uff0c\u7ed3\u8bba\uff0c\u6211\u5efa\u8bae\u522b\u8d70\u8fd9\u6761\u8def\u5f84\uff0c\u6211\u7684\u6700\u7ec8\u65b9\u6cd5\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jianghan.tech\/index.php?rest_route=\/wp\/v2\/posts\/2891"}],"collection":[{"href":"https:\/\/jianghan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianghan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianghan.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jianghan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2891"}],"version-history":[{"count":0,"href":"https:\/\/jianghan.tech\/index.php?rest_route=\/wp\/v2\/posts\/2891\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianghan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianghan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianghan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}