从nodejs从html运行python脚本

从nodejs从html运行python脚本,python,php,html,node.js,Python,Php,Html,Node.js,尽管如此,我这里的设置相当复杂(我很乐意让它更简单) 我有一个nodejs服务器正在运行(用于本地主机访问),nodejs正在读取文件并在浏览器中输出 这使我有可能拥有自己的服务器来测试多个东西,例如,我可以访问localhost/test.html或localhost/web.php。而且效果很好 现在我想控制灯光。我有一个python脚本可以做到这一点(它在终端中运行得非常好) 现在,我希望能够使用nodejs运行的html页面上的一个简单按钮来运行脚本 我尝试了多种方法,包括pythons

尽管如此,我这里的设置相当复杂(我很乐意让它更简单) 我有一个nodejs服务器正在运行(用于本地主机访问),nodejs正在读取文件并在浏览器中输出

这使我有可能拥有自己的服务器来测试多个东西,例如,我可以访问localhost/test.html或localhost/web.php。而且效果很好

现在我想控制灯光。我有一个python脚本可以做到这一点(它在终端中运行得非常好)

现在,我希望能够使用nodejs运行的html页面上的一个简单按钮来运行脚本

我尝试了多种方法,包括pythonshell、php,但找不到有效的方法

所以我可以从我的页面运行一个python脚本,由nodejs在浏览器中输出

编辑:

下面是evreything如何运行的更详细信息: 我运行一个简单的读取文件并获取url

app.js

http = require("http"),
path = require("path"),
url = require("url"),
fs = require("fs");

function sendError(errCode, errString, response)
{
  response.writeHead(errCode, {"Content-Type": "text/plain"});
  response.write(errString + "\n");
  response.end();
  return;
}

function sendFile(err, file, response)
{
  if(err) return sendError(500, err, response);
  response.writeHead(200);
  response.write(file, "binary");
  response.end();
}

function getFile(exists, response, localpath)
{
  if(!exists) return sendError(404, '404 Not Found', response);
  fs.readFile(localpath, "binary",
   function(err, file){ sendFile(err, file, response);});
}

function getFilename(request, response)
{
  var urlpath = url.parse(request.url).pathname; // following domain or IP and port
  var localpath = path.join(process.cwd(), urlpath); // if we are at root
  fs.exists(localpath, function(result) { getFile(result, response, localpath)});
}

var server = http.createServer(getFilename);
server.listen(1000);
console.log("Server available...");  
从那里我可以调用同一目录中的任何文件,因此我调用index.html

index.html

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <title>Index</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

  </head>
  <body class="text-white bg-dark">
    <center>
    <form method="post">
    <input type="submit" value="OFF" name="OFF" onclick="turnOff()">
    <input type="submit" value="ON" name="ON" onclick="turnOn()">
    </form>
    </center>
    
    <script>
    function turnOff(){
        //something i need to call shell command python3 turnOff.py
    }

    function turnOn(){
        //something i need to call shell command python3 turnOn.py
    }
    
    </script>
    
  </body>
</html>

指数
功能关闭(){
//我需要调用shell命令python3turnoff.py
}
函数开启(){
//我需要调用shell命令python3 turnOn.py
}
我有两个文件打开.py关闭.py


谢谢

如果是同一台服务器,您可以按以下方式运行代码

   exec('python code.py');

请参阅:

您是否尝试将脚本的输出写入文件,并在浏览器中显示文件内容?我有一个.py文件在同一根目录中,我就是找不到如何运行它这是一个可行的想法:)但由于我想从html页面中的按钮运行脚本,我不能要求child_进程运行exec..想法是-当你点击一个HTML按钮时,你会向你的nodeJS服务器发送一个请求(例如ajax)来运行这个代码谢谢,我对ajax请求一无所知,你能告诉我一个如何使用它的例子吗?请将这个问题标记为已解决,并以你需要帮助的方式用一个特定的问题打开新的问题。见:,