1

Bare with me, i have a pretty complex setting here (i'd be happy to get it simpler) I have a nodejs server running (for localhost access) the nodejs is reading a file and output in browser

This gives me the possibility to have my own server for testing multiple things so i can access localhost/test.html or localhost/web.php for example. and it works fine.

Now i want to control lights. i have a python script that can do that (and it works very well in terminal)

Now i want to be able to run the script using a simple button on the html page ran by nodejs.

I've tried multiple things, including pythonshell, php, but can't find the working one..

So I can i run a python script from my page outputed in browser by nodejs ?

EDIT :

here is in more detail how evreything is running : I node a app.js running a simple read file and get 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...");  

from there i can call any files in the same dir, i therefore call a 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>

And i have two files turnOn.py and turnOff.py

Thanks

2
  • Have you tried writing the output of the script into a file, and displaying the file contents in the browser? Commented Jan 5, 2021 at 12:25
  • I have a .py file in the same root, i just can’t find how to run it Commented Jan 5, 2021 at 12:38

1 Answer 1

1

If it is the same server, you can run code in the following way

   exec('python code.py');

see: https://medium.com/stackfame/how-to-run-shell-script-file-or-command-using-nodejs-b9f2455cb6b7

Sign up to request clarification or add additional context in comments.

4 Comments

that is a working idea :) But since i'd like to run the script from a button in html page, i can't require child_process to run exec..
The idea is - when you click on an HTML button, you will send a request (for example ajax) to your nodeJS server to run this code
thanks, i just don't know anything about ajax request, could you tell me an exemple on how to use it ?
Please mark this question as resolved and open new one with a specific question in the way you need help. see: AJAX, jQueryAjax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.