start node app from python script

守給你的承諾、 提交于 2019-12-05 00:47:56

问题


Is it possible to start a node.js app from within a python script on a raspberry pi?

On the command line I run sudo node myscript.js

could I use a library like os?


回答1:


The first line of file shall be:

#!/usr/bin/python

You can call command with 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:

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.




回答2:


As Selcuk mentioned in his comment, use the subprocess module:

#! /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:

#! /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().



来源:https://stackoverflow.com/questions/35788729/start-node-app-from-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!