Connecting NodeJS Application With Python

Tram Ho

Nodejs is a popular web development platform today because of the benefits and convenience it offers. But there are a few things that it was not supported as machine learning, deep learning and libraries for Python AI tao.That sewing supports all of these things and sometimes many other things that nodejs no. People will wonder why not use python’s Django Framework to build web applications that can integrate machine learning and artificial intelligence. This seems to be fine if we build that application from scratch. But what if our application is Nodejs and it’s running, but now we want to add machine learning. It’s okay to connect Nodejs and Python as usual, using the child process module for Nodejs .

We will have 3 ways to interact between python and nodejs but first we need setup


  • Create a simple Express project:

    Create simple server.js file

    In the package.json file

    Try running the project before continuing

    The result is as follows

    Next we need a file that does not handle python :

Method 1: Call Python script from Node child process

  • We need to modify the Express file server to make it call the Python script child process

    The function used to interact between nodejs and python is child_process.spawn () as defined by the definition:

    The child_process.spawn() function will have the following parameters

    The parameters passed will be retrieved in the correct order

    Run the application and then enter the example link http://localhost:3000/name?firstname=van&lastname=nghia the result:

    in the console:

Method 2: Call Python script from the Node child process using the python-shell package

  • This can be interpreted as way 1.1 because we are still using the child process but for now we will use python-shell to include deployment details (as well as provide error handling and other utilities including). deploy a simple messaging to communicate directly with a single script).We will rewrite the file server as follows:

    Install package python-shell :

    run to test we get the result:

Method 3: Use a message broker

  • We will now run the Python and Node applications as separate processes and use a message broker to communicate between them. Here we use RabbitMQ , along with amqplib to integrate in node and pika for Python.Follow RabbitMQ installation if you haven’t already, on OSX you can simply run:

    Then start RabbitMQ :

    Now we’ll edit files server.js . Use amqplib in the node to connect to the currently running RabbitMQ instance:

    We write a simple message handler in Python to handle request messages, call and publish response messages – :

    Next We need to make some small changes to – now we will pass the parameters in, instead of using sys.argvs and expose to a method we can call from the message. py :

    The steps to run will first run file:

    Then run to the server node:

    Check the result


This is a simple combination of Python and Node.js, which is useful for situations where you want to use Python’s computing capabilities but want to take advantage of the Node.js application. Hopefully, through this article you can use and develop your application better from these basics.


Source :

Share the news now

Source : Viblo