首页 > 解决方案 > Windows OS下Nodejs运行python脚本

问题描述

我有一个简单的 python 脚本(在 windows os 下完美运行),我想用 nodejs 执行。所以我建立了nodejs服务器,我建立了html页面,一切都在windows下运行。但是我的python脚本没有运行......相同的nodejs服务器+ html页面+脚本在raspbian OS下完美运行。所以我不明白错误在哪里。有人可以帮我找出错误吗?谢谢。

nodejs服务器:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');            
const formidable = require('formidable');
const path = require('path');
const PORT = 8000;
var fs = require('fs');
const {execFile, exec, spawn} = require ('child_process');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));   


app.get('/', function (req, res){
    res.sendFile(__dirname + '/public/index.html');     
});


function runScript(){                                   
    return spawn('python', [
        path.join(__dirname + '/public/script.py'), 
    ]);
}


app.post('/start', function(req, res){                  
    const subprocess = runScript()
    res.sendFile(__dirname + '/public/index.html');
    console.log('script launched')
});

app.listen(8000, function () {
    console.log('Server Started on Port : 8000 !')
})

html页面(简历):

            <div id="right">
                <form action="/start" method="post">    
                    <input title="Clic to Start " type="submit" id="state" value="START">           
                </form>
                <form action="/replay" method="post">
                    <input title="Clic to stop"  type="submit" id="state" value="STOP">     
                </form>
            </div>

剧本 :

# coding: utf-8
import os, sys
import subprocess
import time
import json

global listedouble
listedouble=[]

def wol():
    counter = 0
    while counter < 100:
        counter = counter + 1
        print(counter)
    if counter == 100:
        print("le compteur est arrive a 1000")


def save():
    File_name = "TEST_python"
    with open("C:/Users/430154/Desktop/nodejs/public/"+File_name+".json", "w") as file:           # ici le repertoire correspond au repertoire raspberry
        json.dump(listedouble, file)

wol()
save()

非常感谢。

标签: pythonnode.js

解决方案


推荐阅读