0

In JavaScript code declared a variable, that variable should be passed as input to python child process which is called inside JavaScript code, where python code executes its output by taking input from JavaScript variable

JavaScript code

    var input="onepiece";
    var spawn = require("child_process").spawn;
    var python_process = spawn('python3',["/home/centos/python.py",input] );
    python_process.stderr.on('data',function(data) {
    console.log(data.toString());
    });

import sys
input1=sys.argv[1]
print(input1)
count=0;
for i in range(0, len(input1)):
    if(input1[i] != ' '):
        count = count + 1;
print(  str(count));

when I execute javscript code i should get onepiece 8

3
  • stackoverflow.com/questions/47704780/…. Commented May 20, 2022 at 8:26
  • What happens with your code now? Commented May 20, 2022 at 8:29
  • this method didn't work Commented May 20, 2022 at 8:33

1 Answer 1

1

Made it work by connecting the stream listener to stdout rather than stderr:

python_process.stdout.on('data',function(data) {
   console.log(data.toString());
});
Sign up to request clarification or add additional context in comments.

Comments

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.