我们的应用程序使用 python 和服务器端 Nodejs。 有 python 代码向用户发送通知。发送通知时,我们需要将该信息发送到第三方工具进行分析。我们使用的是python 2.7,分析工具有基于python 3的SDK。 所以,我们打算使用解析工具的node SDK。因此我们将编写从nodejs代码调用分析工具api的代码。 但是当发送通知时应该触发节点js函数。 因此,在Python代码中,一旦通知发送给用户,我们就可以调用nodejs函数来调用分析API。
我尝试检查谷歌,但没有找到任何可靠的例子。
要将基于 Node.js 的分析工具 SDK 与现有的 Python 2.7 应用程序集成以向用户发送通知,您可以按照以下步骤操作:
1。利用 Naked 的 muterun_js: 您可以合并 Naked 库中的 muterun_js 函数,以在 Python 2.7 应用程序中执行 JavaScript 代码。这允许您无缝触发 Node.js 函数。具体方法如下:
from Naked.toolshed.shell import muterun_js
def run_node_script(script_path):
response = muterun_js(script_path)
if response.exitcode == 0:
return response.stdout
else:
return response.stderr
# Call the Node.js function from your Python code
node_script_path = "path/to/your/node/script.js"
run_node_script(node_script_path)
2。发送通知后调用 Node.js 函数进行分析: 从 Python 代码发送通知后,您可以调用负责调用分析 API 的 Node.js 函数。您可以使用之前定义的 run_node_script 函数来触发此函数。
# After sending notifications in your Python code
run_node_script(node_script_path) # Invoke the Node.js function for analytics
通过执行以下步骤,您可以将 Node.js 分析 SDK 无缝集成到您的 Python 2.7 应用程序中。确保您调用的 Node.js 函数已设置为有效处理分析任务。您可能需要将相关数据从 Python 代码传递到 Node.js 函数以进行分析处理。
更详细的实现和使用说明,请参考Naked库的文档:[https://naked.readthedocs.io/toolshed_shell.html][1]。 此外,请确保您的 Node.js 环境已正确配置为按预期执行与分析相关的任务。