如何从Python中执行带参数的JavaScript代码

2024-06-29 00:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

使用从python执行javascript裸体.toolshed.shell: How can I execute JavaScript code from Python? 我的问题回答得很好,但有什么办法可以做到这一点,并通过辩论来执行呢?像

from Naked.toolshed.shell import execute_js

response = execute_js('file.js', arg1) # arguments to pass
if response.exitcode == 0:
  print(response.stdout)
else:
  sys.stderr.write(response.stderr)

同时文件.js可能看起来像

^{pr2}$

有没有办法做到这一点?在


Tags: fromexecuteresponsestderrjscodejavascriptshell
1条回答
网友
1楼 · 发布于 2024-06-29 00:58:43

如文件所示,可以:

The execute_js() function runs the execute() function on a Node.js script file. Instead of passing the command to be executed as the first parameter, pass a Node.js script filepath as the first parameter and any additional command arguments as the second parameter (optional). The executed command is concatenated from these strings with the following code:

if len(arguments) > 0:
    js_command = 'node ' + file_path + " " + arguments
else:
    js_command = 'node ' + file_path

Parameters:
file_path (string) - the filepath to the Node.js script that is to be executed by the shell

arguments (string) - optional, any additional arguments to be used with your command

文件: http://naked.readthedocs.io/toolshed_shell.html#Naked.toolshed.shell.execute_js

相关问题 更多 >