我无法从Express应用执行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存储库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('/')
}

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Matplotlib'粗体'字体 - python

跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…