0

I'm trying to spawn a child process to run a python script from Node. I have the following request that comes in:

/webcrawler?source=http://www.pygamers.com&method=BFS&nodeCount=3&depth=0&keyword=game

I have verified that my params are coming in correctly. Here is my code set up to handle the request in app.js:

app.get('/webcrawler', function(req, res){
  var python = require('child_process').spawn(
  'python',
  ["WebCrawler/Webcrawler.py"
  , req.query.source
  , req.query.method
  , req.query.nodeCount
  , req.query.depth
  , req.query.keyword]
  );
  var output = "";
  python.stdout.on('data', function(data){ output += data });
  python.on('close', function(code){
    if (code !== 0) {
        return res.send(500, code);
    }
    return res.send(200, output);
  });
});

I am calling my Python script, Webcrawler.py which is in the WebCrawler directory. The WebCrawler directory is in the same directory as app.js.

However, this request is giving me a 500, and I haven't been able to figure out why. It seems like I must be generating the child process incorrectly. I used this answer as a model for doing so.

Community
  • 1
  • 1
123
  • 8,402
  • 12
  • 51
  • 93
  • Either try removing Webcrawler as a part of the path you are instructing node, or give it an absolute path would be my guess - but I don't know node all that well. Might also be useful to know what the 500 is about. – James R Mar 15 '17 at 20:26
  • Check if your python script exits with your targeted exit code. Otherwise check the signal which is also passed as a result parameter to the close event. Do some further analysis with both parameters to check your code validity. See https://nodejs.org/api/child_process.html#child_process_event_close – DmiN Mar 15 '17 at 20:40

2 Answers2

0

It needs to be an absolute path, like /home/username/Webcrawler/webcrawler.py

Olivercodes
  • 1,088
  • 5
  • 17
0

Sounds like a path problem. Also checkout python-shell package. Makes your life so much easier.