js调用python脚本_javascript – 如何从NodeJs调用python脚本
我需要在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)
因为我只使用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...");
});
任何帮助,将不胜感激
js调用python脚本_javascript – 如何从NodeJs调用python脚本相关推荐
- linux安装nodejs一键脚本,ubuntu16.04部署nodejs+vue框架脚本
#获取和安装node wget https://nodejs.org/dist/v8.9.2/node-v8.9.2-linux-x64.tar.xz xz -d node-v8.9.2-linux- ...
- c++ python混合编程 restful_简单上手nodejs调用c++(c++和js的混合编程)
因为项目的原因,最近经常使用node.js搭RESTful接口. 性能还是很不错啦,感觉比Spring Boot之类的要快.而且在不错的性能之外,只要程序结构组织好,别让太多的回调把程序结构搞乱,整体 ...
- shell脚本和python脚本和go脚本的区别_Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例-Go语言中文社区...
NodeJS 子进程提供了与系统交互的重要接口,其主要 API 有: 1)标准输入.标准输出及标准错误输出的接口: child.stdin 获取标准输入 child.stdout 获取标准输出 chi ...
- halcon可以用python吗_如何基于pythonnet调用halcon脚本
这篇文章主要介绍了如何基于pythonnet调用halcon脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 最近的项目中遇到了使用python程 ...
- halcon python 联合开发_使用pythonnet调用halcon脚本
最近的项目中遇到了使用python程序结合不同部分,其中包括使用halcon处理拍摄到的图像. halcon本身提供了c++与.NET的开发库,但无python库,网上有pyhalcon之类的库,但功 ...
- python调用ping命令_我可以创建一个脚本来测试是否可以在python服务器上远程使用SSH和PING命令吗? - python...
我可以创建脚本来测试是否可以在python中远程使用服务器上的SSH和PING命令吗? 参考方案 好的,这是Python3和Linux的示例.只需记住,您的目标主机/服务器必须正在运行SSH服务器才能 ...
- java 调用python脚本过程_通过Java调用Python脚本
在进行开发的过程中,偶尔会遇到需要使用Java调用Python脚本的时候,毕竟Python在诸如爬虫,以及科学计算等方面具有天然的优势.最近在工作中遇到需要在Java程序中调用已经写好的Python程 ...
- python 调用控制台并获取返回结果_Java调用Python脚本并获取返回值
在Java程序中有时需要调用Python的程序,这时可以使用一般的PyFunction来调用python的函数并获得返回值,但是采用这种方法有可能出现一些莫名其妙的错误,比如ImportError.在 ...
- python脚本编程实例_C程序调用Python脚本的例子
下面我们一起来看看关于C程序调用Python脚本的例子,如果你对此有兴趣的希望此教程对各位同学会有所帮助. 代码如下 复制代码 #include int main(){ PyObject * g_pM ...
最新文章
- Guass列主元消去法求解线性方程组
- django rest-framework 1.序列化 一
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- Computer:路由器、交换机、猫Modem的简介、区别之详细攻略
- 【David Silver强化学习公开课】-4:Model-Free Prediction
- Java并发编程之线程安全
- 力扣538.把二叉搜索树转换为累加树(JavaScript)
- Jedis连接Redis读写基本操作
- js ul 清缓存_JavaScript使用ul中li标签实现删除效果
- 关于限制水晶报表的导出格式
- js 如何实现拖动滑块
- git启动linux系统,gitlab 开机自动启动配置
- Java - Js 谷歌浏览器(Chrome)调用Ie浏览器
- Android App Bundle 和Unity AAB BundleTools
- 人脸表情识别 (1) 下载fer2013数据集和数据的处理
- PAT-1032 挖掘机技术哪家强
- 【机器学习】这份分类决策树算法介绍请收好!
- linux系统基础知识总结
- 服务器创建虚拟主机,服务器创建虚拟主机
- AOC冠捷显示器无法保修