I';我无法从express应用程序执行python脚本

I';我无法从express应用程序执行python脚本,python,node.js,express,raspberry-pi,gpio,Python,Node.js,Express,Raspberry Pi,Gpio,当我在express应用程序中单击按钮时,我正在尝试执行python脚本。脚本正在打开我的树莓圆周率中的一个LED。 我已经测试了脚本,它们可以工作,但是当我试图从服务器上执行它们时,它根本不工作。 我正在使用“spawn”创建一个子进程,然后通过stdin写入以执行脚本文件 这是我的路由器文件: var express = require('express') var router = express.Router() var python = require('child_process'

当我在express应用程序中单击按钮时,我正在尝试执行python脚本。脚本正在打开我的树莓圆周率中的一个LED。 我已经测试了脚本,它们可以工作,但是当我试图从服务器上执行它们时,它根本不工作。 我正在使用“spawn”创建一个子进程,然后通过stdin写入以执行脚本文件

这是我的路由器文件:

var express = require('express')
var router = express.Router()

var python = require('child_process').spawn('python', [ '-i' ])
//python.setEncoding('utf-8')
python.stdout.pipe(process.stdout)

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index')
})

router.get('/green', green)
router.get('/yellow', yellow)
router.get('/red', red)

module.exports = router

function green(req, res) {
    console.log('Turning on green led...')
    python.stdin.write("execfile('./public/python/green_led.py')")
    res.redirect('/')
}

function yellow(req, res) {
    console.log('Turning on yellow led...')
    python.stdin.write("execfile('./public/python/yellow_led.py')")
    res.redirect('/')
}

function red(req, res) {
    console.log('Turning on red led...')
    python.stdin.write("execfile('./public/python/red_led.py')")
    res.redirect('/')
}
您可以查看Github回购协议


谢谢

我通过使用
exec
而不是
spawn
来修复它。这是我的路由器文件:

var express = require('express')
var router = express.Router()

var exec = require('child_process').exec

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index')
})

router.get('/green', green)
router.get('/yellow', yellow)
router.get('/red', red)
router.get('/auto', auto)

module.exports = router

function green(req, res) {
    console.log('Turning on green led...')
    var child = exec('python ./public/python/green_led.py')
    child.stdout.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.stderr.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.on('close', function(code) {
        console.log('closing code: ' + code)
    })
    res.redirect('/')
}

function yellow(req, res) {
    console.log('Turning on yellow led...')
    var child = exec('python ./public/python/yellow_led.py')
    child.stdout.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.stderr.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.on('close', function(code) {
        console.log('closing code: ' + code)
    })
    res.redirect('/')
}

function red(req, res) {
    console.log('Turning on red led...')
    var child = exec('python ./public/python/red_led.py')
    child.stdout.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.stderr.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.on('close', function(code) {
        console.log('closing code: ' + code)
    })
    res.redirect('/')
}

function auto(req, res) {
    console.log('Turning on auto led...')
    var child = exec('python ./public/python/loop_led.py')
    child.stdout.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.stderr.on('data', function(data) {
        console.log('stdout: ' + data)
    })
    child.on('close', function(code) {
        console.log('closing code: ' + code)
    })
    res.redirect('/')
}

当您尝试运行时,能否提供更多详细信息?您是否有任何错误?在我的public/python目录中有3个python脚本,每个连接到RPi的LED都有一个。如果我手动运行这些脚本,它们就可以工作,但是如果我尝试用上面的代码运行它们,它不会显示任何内容,只会返回304响应,LED不会亮起。首先从expressjs应用程序运行一个非常简单的python脚本(hello world),看看是否可以工作。