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这样的库吗?
2 个解决方案
#1
7
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,你可以在那里找到很多使用完整信息。
#2
2
As Selcuk mentioned in his comment, use the subprocess
module:
正如Selcuk在评论中提到的那样,使用子进程模块:
#! /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
. If you do, you can try:
尝试使用sudo运行命令时,很可能会遇到FileNotFoundError。如果你这样做,你可以尝试:
#! /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(),这可能会成为一个问题。
#1
7
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,你可以在那里找到很多使用完整信息。
#2
2
As Selcuk mentioned in his comment, use the subprocess
module:
正如Selcuk在评论中提到的那样,使用子进程模块:
#! /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
. If you do, you can try:
尝试使用sudo运行命令时,很可能会遇到FileNotFoundError。如果你这样做,你可以尝试:
#! /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(),这可能会成为一个问题。