如何从NodeJs调用Python脚本

17
我需要在NodeJs中调用这个Python脚本。 Read.py
#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:

    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])

        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]

        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"

我使用了python-shell,在这里是Node.js代码:

Test.js

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

var options = {
scriptPath: '/home/pi/gpio-admin/MFRC522-python/'
};
var pyshell = new PythonShell('Read.py',options);


pyshell.on('message', function (message) {

    console.log(message);
});

但是当我运行这段代码时,在 Node 侧没有看到任何东西。 我认为问题出现在 Python 脚本到达此级别时。

   (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

因为我只用了一个包含打印语句的while循环,所以它可以正常运行。之后,我尝试了另一种方法来实现这个目标。但我遇到了与上面相同的问题。这里是另一种方法。

AltTest.js

var python = require('child_process').spawn(
 'python',
 // second argument is array of parameters, e.g.:
 ["/home/pi/gpio-admin/MFRC522-python/Read.py"]
 );
 var output = "";
 python.stdout.on('data', function(){ 

    output += data ;
    console.log(data);
});
 python.on('close', function(code){ 

   console.log("Here you are there...");
 });

希望能得到帮助


1
PyNode包允许调用Python函数并获取JS包。https://thecodinginterface.com/blog/bridging-nodejs-and-python-with-pynode/ - SciGuyMcQ
您可以使用 child_process Node.js 包中提供的以下函数来执行任何终端命令:execexecSyncspawnspawnSync - Arjun Singh
6个回答

24

有多种方法可以做到这一点。

  • 第一种方法是通过执行 npm install python-shell 命令来完成。

以下是相关代码:

var PythonShell = require('python-shell');
//you can use error handling to see if there are any errors
PythonShell.run('my_script.py', options, function (err, results) { 
//your code

您可以使用以下代码将消息发送到Python shell:

pyshell.send('hello');

您可以在此处找到API参考:

https://github.com/extrabacon/python-shell
  • 第二种方法-您可以参考另一个软件包node-python,您需要执行以下操作:npm install node-python

  • 第三种方法-您可以参考这个问题,在那里您可以找到使用子进程的示例:

    How to invoke external scripts/programs from node.js

更多参考:

https://www.npmjs.com/package/python

如果您想使用面向服务的体系结构-

http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

你好 @Tejus Prasad,我想使用第一种方法。但我的 Node JS 代码是 AWS Lambda 函数,而我的 Python 代码保存在树莓派上。我的问题是:我应该在哪里运行 Python shell?如果在 AWS 上,如何操作? - TAMIM HAIDER

11
  • 安装 python-shell:- npm install python-shell

    Index.js

let {PythonShell} = require('python-shell')

function runPy(){
    return new Promise(async function(resolve, reject){
          let options = {
          mode: 'text',
          pythonOptions: ['-u'],
          scriptPath: './test.py',//Path to your script
          args: [JSON.stringify({"name": ["xyz", "abc"], "age": ["28","26"]})]//Approach to send JSON as when I tried 'json' in mode I was getting error.
         };

          await PythonShell.run('test.py', options, function (err, results) {
          //On 'results' we get list of strings of all print done in your py scripts sequentially. 
          if (err) throw err;
          console.log('results: ');
          for(let i of results){
                console.log(i, "---->", typeof i)
          }
      resolve(results[1])//I returned only JSON(Stringified) out of all string I got from py script
     });
   })
 } 

function runMain(){
    return new Promise(async function(resolve, reject){
        let r =  await runPy()
        console.log(JSON.parse(JSON.stringify(r.toString())), "Done...!@")//Approach to parse string to JSON.
    })
 }

runMain() //run main function

test.py

    import sys #You will get input from node in sys.argv(list)
    import json
    import pandas as pd #Import just to check if you dont have pandas module you can comment it or install pandas using pip install pandas

    def add_two(a, b):
        sum = 0
        for i in range(a, b):
            sum += i
        print(sum)  

    if __name__ == "__main__":
        print("Here...!")
        # print(sys.argv)
        j = json.loads(sys.argv[1]) #sys.argv[0] is filename
        print(j)
        add_two(20000, 5000000) #I make this function just to check 
    # So for all print done here you will get a list for all print in node, here-> console.log(i, "---->", typeof i)

6
采用微服务方法。将Python脚本作为HTTP REST API服务托管。从node.js中调用API-您不需要集成这些技术; 这是不可扩展的。

3
有时候是的,这取决于上下文。 - GPrathap
这只有在Python输出可序列化数据时才是真的吗?可以使用JS代码来通知Python以某种方式更新API,然后在完成后访问更新后的API吗? - Ryan Skene

2

您可以通过以下方式在 node.js 中运行 Python 脚本。使用内置的 child_process.spawn(这是一个 node.js 库)。

router.get('/', (req, res) => {
    const {spawn} = require('child_process');
    const path = require('path');
    function runScript(){
        return spawn('python', [
              path.join(__dirname, '../../scripts/myscript.py'),
              '-some_arg',
              '--another_arg',
        ]);
    }
    const subprocess = runScript();
    // print output of script
    subprocess.stdout.on('data', (data) => {
            console.log(`data:${data}`);
    });
    subprocess.stderr.on('data', (data) => {
           console.log(`error:${data}`);
    });
    subprocess.stderr.on('close', () => {
               console.log("Closed");
    });
    // const subprocess = runScript()
    res.set('Content-Type', 'text/plain');
    subprocess.stdout.pipe(res);
    subprocess.stderr.pipe(res);
});

0

我的Node.js代码

const { spawn }=require('child_process')

const child_python=spawn('python',['hello.py']);

child_python.stdout.on('data',(data)=> {
    console.log(`stdout :${data}`);
})

child_python.stderr.on('data',(data)=>{
    console.log(`stderr : ${data}`);
})

child_python.on('close',(code)=>{
    console.log(`child process exited with code ${code}`)
})

你可以查看这个视频,从Nodejs运行你的Python脚本。它会告诉你如何传递参数。

Link https://www.youtube.com/watch?v=eN0nMuS8ur8


0
如果您想避免使用包管理器/膨胀,可以考虑编写一个shell脚本函数来运行Python,将结果流式传输到本地的txt文件中,然后通过http收集并发送。
    const { exec } = require("child_process");
    
    function runShellScript(script, callback) {
        exec(script, (error, stdOut, stderr) => {
            
            var result = {status: true};
            
            if (error) {
                result.status = false;
                result.error = error.message;
            }
            if (stderr) {
                result.status = false;
                result.stderr = stderr;
            }
    
            if(stdOut){
                result.result = stdOut;
            }
            
    
            callback(result);
        });
    }
    
    runShellScript("python3 myscript.py >> output.txt", function(res) {
        console.log(res);
fs.readFileSync('output.txt');
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接