【问题标题】:I'm not able to execute python script from an express app我无法从快速应用程序执行 python 脚本
【发布时间】:2019-03-13 22:32:25
【问题描述】:

当我单击快速应用程序中的按钮时,我正在尝试执行 python 脚本。该脚本只是在我的 Raspberry Pi 中打开一个 LED。 我已经测试了脚本并且它们可以工作,但是当我尝试从服务器执行它们时它根本不起作用。 我正在使用“spawn”创建一个子进程,然后通过标准输入写入执行脚本。

这是我的路由器文件:

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 repo Here

谢谢!

【问题讨论】:

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

标签: python node.js express raspberry-pi gpio


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多