gpt4 book ai didi

javascript - 如何从 NodeJs 调用 python 脚本

转载 作者:IT老高 更新时间:2023-10-28 23:23:06 30 4
gpt4 key购买 nike

我需要在 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,这里是它的 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);
});

但是当我运行这段代码时,我在 Node 端看不到任何东西。我认为当 python 脚本达到这个级别时会出现问题。

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

因为我只是使用只有 print 语句的 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...");
});

任何帮助将不胜感激

最佳答案

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

  • 第一种方法是执行 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/

关于javascript - 如何从 NodeJs 调用 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689526/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com