R
R
Robot Chappie2016-09-30 10:53:39
JavaScript
Robot Chappie, 2016-09-30 10:53:39

How to pass data to the input stream?

Suppose there is a python script that asks for input, like this:

def who():
      name = input('You name\n')
      print(name);

who()

There is also a JavaScript script that calls a python script:
const spawn = require('child_process').spawn;
const test = spawn('python3', ['test.py'])

test.stdout.on('data', (data) => {
  console.log(data.toString('utf8'))
});

test.on('close', (code) => {
  console.log(code);
});

As a result, when you run the script in JS, "You name" is displayed in the stdout stream and the user must enter data.
Please tell me how to make it so that values ​​are substituted into the input stream, for example, from some variable and the final result is displayed on stdout. I would be grateful for any hints and examples. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romy4, 2016-09-30
@BenderLib

test.stdin.write("Username");
here you can’t like a link to a stackoverflow
, so here’s an example

var spawn = require('child_process').spawn,
    child = spawn('phantomjs');

child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);

child.stdin.write("console.log('Hello from PhantomJS')\n");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question