这种方式最直接
import os
os.system('/Users/apple/.nvm/versions/node/v16.9.0/bin/node test.js')
/Users/apple/Documents/meiminjun/Notebook/10-python/jsencrypt.js:1141
j_lm && "Microsoft Internet Explorer" == navigator.appName
^
ReferenceError: navigator is not defined
at /Users/apple/Documents/meiminjun/Notebook/10-python/jsencrypt.js:1141:44
at Object.<anonymous> (/Users/apple/Documents/meiminjun/Notebook/10-python/jsencrypt.js:2824:3)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.<anonymous> (/Users/apple/Documents/meiminjun/Notebook/10-python/test.js:1:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
256
这种方式可以拿到返回值,但是无法使用模块化特性,只能单文件方式
import execjs
import os
os.environ["EXECJS_RUNTIME"] = "Node"
# 查看环境
execjs.get().name
'JavaScriptCore'
# eval 执行简单脚本
e = execjs.eval('new Array(1,2,3)')
print(e)
[1, 2, 3]
# execjs.compile用于执行更复杂的js代码
x = execjs.compile('''
function add(x,y){
return x+y;
};
''')
print(x.call('add', '1', '2'))
12
with open('test.js') as f:
jsdata = f.read()
ctx = execjs.compile(jsdata)
# print(x.call('add', '1', '2'))
publickey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv6PccYjPuMMwkLpEiugcxctFWV2Kw5XCWgSoVYVx7XO9N2Q7tSIW5mPqoAKDg'
print(x.call('jsencrypt', publickey, 'Fengliya1991'))
---------------------------------------------------------------------------
ProgramError Traceback (most recent call last)
/var/folders/vh/4zv6yqys5w7dry2wxbzxkkvxf0lzcv/T/ipykernel_24039/120078937.py in <module>
5 # print(x.call('add', '1', '2'))
6 publickey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv6PccYjPuMMwkLpEiugcxctFWV2Kw5XCWgSoVYVx7XO9N2Q7tSIW5mPqoAKDg'
----> 7 print(x.call('jsencrypt', publickey, 'Fengliya1991'))
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_abstract_runtime_context.py in call(self, name, *args)
35 if not self.is_available():
36 raise execjs.RuntimeUnavailableError
---> 37 return self._call(name, *args)
38
39 @abstractmethod
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_external_runtime.py in _call(self, identifier, *args)
90 def _call(self, identifier, *args):
91 args = json.dumps(args)
---> 92 return self._eval("{identifier}.apply(this, {args})".format(identifier=identifier, args=args))
93
94 def _exec_with_pipe(self, source):
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_external_runtime.py in _eval(self, source)
76
77 code = 'return eval({data})'.format(data=data)
---> 78 return self.exec_(code)
79
80 def _exec_(self, source):
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_abstract_runtime_context.py in exec_(self, source)
16 if not self.is_available():
17 raise execjs.RuntimeUnavailableError
---> 18 return self._exec_(source)
19
20 def eval(self, source):
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_external_runtime.py in _exec_(self, source)
86 else:
87 output = self._exec_with_pipe(source)
---> 88 return self._extract_result(output)
89
90 def _call(self, identifier, *args):
/usr/local/anaconda3/envs/py39/lib/python3.9/site-packages/execjs/_external_runtime.py in _extract_result(self, output)
165 return value
166 else:
--> 167 raise ProgramError(value)
168
169
ProgramError: ReferenceError: Can't find variable: jsencrypt
import os
# os.environ.keys()
os.environ["EXECJS_RUNTIME"] = "Node"
execjs.get().name
'JavaScriptCore'