如何从NodeJs调用python脚本

时间:2021-12-19 23:58:33

I need to call this python script in NodeJs.

我需要在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"

I used python-shell, here is the NodeJs code for that

我使用了python-shell,这里是NodeJs代码

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);
});

But when I ran this code I didn't see anything in Node side. I think problem occurs when python script comes to this level.

但是当我运行此代码时,我在Node端没有看到任何内容。我认为当python脚本达到这个级别时会出现问题。

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

Because I just ran with while loop which has only print statement then it works. After that I tried another way to achieve this. But I got same problem which I have with above.Here is another method

因为我刚刚运行while循环,只有print语句然后它工作。之后我尝试了另一种方法来实现这一目标。但是我遇到了与上面相同的问题。这是另一种方法

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...");
 });

Any help would be appreciated

任何帮助,将不胜感激

1 个解决方案

#1


10  

There are multiple ways of doing this.

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

  • first way is by doing npm install python-shell
  • 第一种方法是通过做npm install python-shell

and here's the code

这是代码

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

you can send a message to python shell using pyshell.send('hello');

你可以使用pyshell.send('hello')向python shell发送消息;

you can find the API reference here- https://github.com/extrabacon/python-shell

你可以在这里找到API参考 - https://github.com/extrabacon/python-shell

  • second way - another package you can refer to is node python , you have to do npm install node-python

    第二种方式 - 你可以参考的另一个包是node python,你必须要做npm install node-python

  • third way - you can refer to this question where you can find an example of using a child process- How to invoke external scripts/programs from node.js

    第三种方式 - 您可以参考这个问题,您可以在其中找到使用子进程的示例 - 如何从node.js调用外部脚本/程序

a few more references - https://www.npmjs.com/package/python

还有一些参考文献 - https://www.npmjs.com/package/python

if you want to use service-oriented architecture - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

如果你想使用面向服务的架构 - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

#1


10  

There are multiple ways of doing this.

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

  • first way is by doing npm install python-shell
  • 第一种方法是通过做npm install python-shell

and here's the code

这是代码

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

you can send a message to python shell using pyshell.send('hello');

你可以使用pyshell.send('hello')向python shell发送消息;

you can find the API reference here- https://github.com/extrabacon/python-shell

你可以在这里找到API参考 - https://github.com/extrabacon/python-shell

  • second way - another package you can refer to is node python , you have to do npm install node-python

    第二种方式 - 你可以参考的另一个包是node python,你必须要做npm install node-python

  • third way - you can refer to this question where you can find an example of using a child process- How to invoke external scripts/programs from node.js

    第三种方式 - 您可以参考这个问题,您可以在其中找到使用子进程的示例 - 如何从node.js调用外部脚本/程序

a few more references - https://www.npmjs.com/package/python

还有一些参考文献 - https://www.npmjs.com/package/python

if you want to use service-oriented architecture - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

如果你想使用面向服务的架构 - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/