Skip to content

Instantly share code, notes, and snippets.

@RamiAwar
Created May 24, 2018 11:24
Show Gist options
  • Save RamiAwar/5c0aa3703321b006bb1da2009f0c2940 to your computer and use it in GitHub Desktop.
Save RamiAwar/5c0aa3703321b006bb1da2009f0c2940 to your computer and use it in GitHub Desktop.
Example node server with python compilation support
var spawn = require("child_process").spawn;
var fs = require('fs');
var runPythonFile = function (filepath, fileName, stdin, callback, process_list) {
var proc1 = spawn('python3', [filepath + fileName + '.py']);
var stdout1 = "";
var stderr1 = "";
if (stdin) {
proc1.stdin.write(stdin + "\n");
proc1.stdin.end();
}
proc1.stdout.on('data', function (_stdout) {
stdout1 += _stdout;
});
proc1.stdout.on('end', function () {
fl = true;
});
proc1.stderr.on('data', function (_stderr) {
stderr1 += _stderr;
});
proc1.stderr.on('end', function () {
console.log(stderr1);
});
proc1.on('close', function (code) {
if (code == 0) callback(stdout1, "", "");
else {
console.log(stdout1)
callback("", stderr1, "");
}
});
console.log("PYTHON-------------------------------------------")
console.log(proc1);
process_list.push(proc1);
}
/*Async function to run py code and pass stdout,stderr to the callback fn*/
var runPython = function (code, stdin, callback, process_list) {
if (!fs.existsSync('./code')) {
fs.mkdirSync('./code', 0744);
}
if (!fs.existsSync('./code/python')) {
fs.mkdirSync('./code/python', 0744);
}
var fileName = 'Source-' + Math.floor(Math.random() * 100000) + '-' + new Date().getTime();
fs.writeFile('./code/python/' + fileName + '.py', code, function (err) {
if (!err) {
runPythonFile('./code/python/', fileName, stdin, function (stdout, stderr) {
//do nothing
}, process_list);
}
else {
console.log(err);
callback("", "", "Couldn't write the file!");
}
});
}
module.exports = {
runPython: runPython
, runPythonFile: runPythonFile
}
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
const port = 8080;
const python_support = require('./python_support.js');
var process_list = new Array(); // Creating an array to save bots and their processes in
var code = `import requests
import time
time.sleep(1);
r = requests.get('http://localhost:8080/feedback')
while True:
print('test');
time.sleep(1);
`;
app.listen(port, ()=>{
console.log('We are live on ' + port);
})
app.get('/on', (req, res) => {
// Try to instantiate bot process
python_support.runPython(code, "", function (stdout, stderr) {
callback(stdout, stderr, "");
}, process_list);
// notify on failed?
// Feedback
res.send('Bot process instantiated');
});
app.get('/off', (req, res) =>{
console.log("NODE-------------------------------------------");
console.log(process_list[0]);
process_list[0].stdin.pause();
process_list[0].kill();
console.log('Process 0 killed');
})
app.get('/feedback', (req, res) =>{
console.log("FEEDBACK-------------------------------------------")
console.log('Feedback received from python program');
res.send('ok');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment