0

I want to use python scripts with my Node.JS server, but whenever I run the node server, the python script doesn't run. It starts the server but no python runs. I think it's in this part of the code but I'm not exactly sure.

My index.js:

app.post("/readPython", (request, response) => {
 var dataToSend;
 const python = spawn('python', ['python/cookie.py'], "Kevin");

 python.stdout.on('data', function (data) {
  dataToSend = data.toString();
 });

 python.stderr.on('data', data => {
 console.error(`stderr: ${data}`);
 });

 python.on('exit', (code) => {
 console.log(`child process exited with code ${code}, ${dataToSend}`);
 response.sendFile(`${__dirname}/html/result.html`);
 });
});

My python script:

import sys

print("Hello World!")

sys.stdout.flush()

I don't know exactly what I should expect but whatever it is, the script isn't running. Any help?

1 Answer 1

1

the 3rd argument (options) in spawn() should be an object. I am assuming you are trying to send Kevin as argument. It should be like

const python = spawn('python', ['helloworld.py',"Kevin"]);

python.stdout.on('data', (data)=> {

        console.log(data.toString());
       
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I did that but it didnt show anything. Still just says listening on port

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.