python - 我无法从快速应用程序执行 python 脚本
问题描述
当我单击快速应用程序中的按钮时,我正在尝试执行 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 repo
谢谢!
解决方案
我设法通过使用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('/')
}
推荐阅读
- javascript - 将 2 个字段链接在一起,并在组合 prisma 时使它们独一无二
- flutter - 文件未正确转换为 base64 字符串
- parameter-passing - 如何通过重力表单表单字段检索在 url 中传递的值
- java - 在 while 循环中进行多个输入时出现错误
- python - Pyqtgraph 中绘图的悬停工具
- python - 大型多维数据的无监督聚类
- reactjs - Moment.js 减法函数不适用于自定义日期
- android - 使用 customAdapter 时,带有 MultiChoiceModeListener 的 listView 不会突出显示选择项
- javascript - 如何使用 nuxt“to”指令进入动态路由
- list - 如何将图像文件存储到 Dart 中的列表