Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to set up a simple Node.Js Server that should call a Python Script on running the URL. Kindly refer to the below python and Node js server files.

When I ran the server URL. The page loads! but then the server crashes and now it is giving me the following error (in the cmd prompt):

    Server listening on http://localhost:8080/

this is here so we are in

events.js:141

      throw er; // Unhandled 'error' event

      ^

Error: spawn C:UsersShubhamAnaconda3libos.py ENOENT

    at exports._errnoException (util.js:837:11)

    at Process.ChildProcess._handle. "theme"

        return theme

    else :

        print("lyrics: "+lyrics)

try:    

    runForFun(sys.argv[0], sys.argv[1], sys.argv[2])

except IndexError:

    print('Please supply arguments')

Node js file

//Lets require/import the HTTP module

var http = require('http');

var PythonShell = require('python-shell');

//Let's define a port we want to listen to

const PORT=8080; 

//We need a function that handles requests and sends the response

function handleRequest(request, response){

    var options = {

      mode: 'text',

      pythonPath: 'C:\Users\Shubham\Anaconda3\lib\os.py',

      pythonOptions: ['-u'],

      scriptPath: 'C:\Users\Shubham\Google Drive\Capstone\Theme Extraction',

      args: ['value1', 'value2', 'value3']

    };

    console.log("this is here so we are in");

    PythonShell.run('runPython.py', options, function (err, results) {

    if (err) throw err;

    console.log('results: %j', results, 'finished');

});

    response.end('It Works!! Path Hit: ' + request.url);

}

//Create a server

var server = http.createServer(handleRequest);

//Lets start our server

server.listen(PORT, function(){

    //Callback triggered when server is successfully listening. Hurray!

    console.log("Server listening on: http://localhost:%s/", PORT);

});

1 Answer

0 votes
by (107k points)

Kindly be informed that you need to escape the slashes in the file paths and the pythonPath has to point at a Python executable file, not a script.

Hence, the correct setup would look like this:

var options = {

      mode: 'text',

      pythonPath: 'C:\\Python\\pythonw.exe',

      pythonOptions: ['-u'],

      scriptPath: 'C:\\Users\\Shubham\\Google Drive\\Capstone\\Theme Extraction',

      args: ['value1', 'value2', 'value3']

    }; 

You can also use a Python web framework that will simply import your script to run it.  

Related questions

0 votes
1 answer
asked Sep 26, 2019 in Python by Karan Singh (4.1k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Karan Singh (4.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...