python - 我无法从 Express 应用程序执行 python 脚本

标签 python node.js express raspberry-pi gpio

当我单击 Express 应用程序中的按钮时,我尝试执行 python 脚本。该脚本只是打开我的 Raspberry Pi 中的 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 存储库 Here

谢谢!

最佳答案

我设法使用 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('/')
}

关于python - 我无法从 Express 应用程序执行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152156/

相关文章:

python - 空列表、元组、字典如何分配内存?

node.js - Typescript - 如何在 Node.js 上声明全局变量

javascript - Firebase 部署函数发送推送通知时出错

reactjs - 为什么我不能将 Heroku 上的 React 应用程序从 http 重定向到 https?

node.js - socket.io 远程客户端不工作

python - 当 parentWidget 关闭时, float QDockWidget 不会关闭

python - 使用按钮更新 Matplotlib 中的注释

python - 编程新手,学习python。试图让这个程序工作

javascript - Electron JS - 无法解构 'BrowserWindow' 的属性 'require(...).remote',因为它未定义

javascript - 推送后数组仍然为空