[英]start node app from python script
Is it possible to start a node.js app from within a python script on a raspberry pi? 是否可以从覆盆子pi上的python脚本中启动node.js应用程序?
On the command line I run sudo node myscript.js
在命令行上我运行sudo node myscript.js
could I use a library like os? 我可以使用像os这样的库吗?
The first line of file shall be: 第一行文件应为:
#!/usr/bin/python
You can call command with subprocess.call : 您可以使用subprocess.call调用命令:
from subprocess import call
# Note that you have to specify path to script
call(["node", "path_to_script.js"])
Then you have to set +x
permissions for file to be executable: 然后,您必须为文件设置+x
权限才能执行:
chmod +x filename.py
Know you are ready to go: 知道你准备好了:
./filename.py
Note: checkout Raspberry Pi Stack Exchange , you can find a lot of use full info there. 注意:结帐Raspberry Pi Stack Exchange ,你可以在那里找到很多使用完整信息。
As Selcuk mentioned in his comment, use the subprocess
module: 正如Selcuk在评论中提到的那样,使用subprocess
模块:
#! /usr/bin/env python
import subprocess
subprocess.call('sudo node myscript.js')
It's very likely that you'll encounter a FileNotFoundError
when trying to run your command with sudo
. 尝试使用sudo
运行命令时,很可能会遇到FileNotFoundError
。 If you do, you can try: 如果你这样做,你可以尝试:
#! /usr/bin/env python
import subprocess
subprocess.call('sudo node myscript.js', shell=True)
Per the Python documentation, be VERY careful about using the shell=True
parameter as this could be a problem if you allow any arbitrary user input to be passed to subprocess.call()
. 根据Python文档,使用shell=True
参数要非常小心,因为如果允许将任意用户输入传递给subprocess.call()
,这可能是个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.