简体   繁体   English

获取Node.js服务器以触发python脚本

[英]Get nodejs server to trigger a python script

I have a node.js server running on a Raspberry Pi 3 B+. 我有一个在Raspberry Pi 3 B +上运行的node.js服务器。 (I'm using node because I need the capabilities of a bluetooth library that works well). (我正在使用节点,因为我需要运行良好的蓝牙库的功能)。

Once the node server picks up a message from a bluetooth device, I want it to fire off an event/command/call to a different python script running on the same device. 一旦节点服务器从蓝牙设备接收到一条消息,我希望它触发事件/命令/调用,该事件/命令/调用将在同一设备上运行。

What is the best way to do this? 做这个的最好方式是什么? I've looked into spawning child processes and running the script in them, but that seems messy... Additionally, should I set up a socket between them and stream data through it? 我研究了产生子进程并在其中运行脚本的过程,但这似乎很麻烦……此外,我是否应该在它们之间建立套接字并通过它进行数据流传输? I imagine this is done often, what is the consensus solution? 我想这经常做,共识解决方案是什么?

Running a child process is how you would run a python script. 运行子进程就是您运行python脚本的方式。 That's how you do it from nodejs or any other program (besides a python program). 这就是从nodejs或任何其他程序(除了python程序)中执行此操作的方式。

There are dozens of options for communicating between the python script and the nodejs program. 在python脚本和nodejs程序之间进行通讯的选项有很多。 The simplest would be stdin/stdout which are automatically set up for you when you create the child process, but you could also give the nodejs app a local http server that the python script could communicate with or vice versa. 最简单的是stdin / stdout,它在创建子进程时自动为您设置,但是您也可以为nodejs应用提供一个本地http服务器,python脚本可以与之通信,反之亦然。

Or, set up a regular socket between the two. 或者,在两者之间设置常规插座。

If, as you now indicate in a comment, your python script is already running, then you may want to use a local http server in the nodejs app and the python script can just send an http request to that local http server whenever it has some data it wants to pass to the nodejs app. 如您现在在注释中指示的那样,如果您的python脚本已经在运行,那么您可能想在nodejs应用中使用本地http服务器,并且只要有一些脚本,python脚本就可以向该本地http服务器发送一个http请求。它想要传递给nodejs应用程序的数据。 Or, if you primarily want data to flow the opposite direction, you can put the http server in the python app and have the nodejs server send data to the python app. 或者,如果您主要希望数据沿相反的方向流动,则可以将http服务器放在python应用程序中,然后让nodejs服务器将数据发送到python应用程序。

If you want good bidirectional capabilities, then you could also set up a socket.io connection between the two and then you can easily send messages either way at any time. 如果您想要良好的双向功能,则还可以在两者之间建立一个socket.io连接,然后可以随时轻松地以任何一种方式发送消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM