如何使用PyExecJS运行js模块

时间:2019-04-24 03:03:15

标签: python

我正在使用Node.js作为受支持的运行时。

This是sha1链接。

import execjs

node = execjs.get()

def js_file(path):
    o = open(path).read()
    return node.compile(o)


def js_sha(param):
    js_path = '../js/sha1.min.js'
    ctx = js_file(js_path)
    # ctx.eval("sha1 = require('js-sha1')")
    return ctx.call('sha1', param)

当我用来运行它时。

print(js_sha('Xr0Z-javascript-obfuscation-1{}'.format(1556028122)))

我收到以下错误:

Traceback (most recent call last):
  File "/Users/wyx/bitcoin_workspace/spider_seed/tests/t_js.py", line 109, in <module>
    print(js_sha('Xr0Z-javascript-obfuscation-1{}'.format(1556028122)))
  File "/Users/wyx/bitcoin_workspace/spider_seed/tests/t_js.py", line 55, in js_sha
    return ctx.call('sha1', param)
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_abstract_runtime_context.py", line 37, in call
    return self._call(name, *args)
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_external_runtime.py", line 92, in _call
    return self._eval("{identifier}.apply(this, {args})".format(identifier=identifier, args=args))
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_external_runtime.py", line 78, in _eval
    return self.exec_(code)
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_abstract_runtime_context.py", line 18, in exec_
    return self._exec_(source)
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_external_runtime.py", line 88, in _exec_
    return self._extract_result(output)
  File "/Users/wyx/bitcoin_workspace/spider_seed/.env/lib/python3.6/site-packages/execjs/_external_runtime.py", line 167, in _extract_result
    raise ProgramError(value)
execjs._exceptions.ProgramError: ReferenceError: sha1 is not defined

0 个答案:

没有答案