Reputation: 49
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
Upvotes: 1
Views: 679
Reputation: 1147
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
Upvotes: 1