如何使用python脚本连接node.js应用程序? [英] How to connect node.js app with python script?

查看:84
本文介绍了如何使用python脚本连接node.js应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Pafy 在Meteor.js和短python脚本中使用节点应用。

I've node app in Meteor.js and short python script using Pafy.

import pafy

url = "https://www.youtube.com/watch?v=AVQpGI6Tq0o"
video = pafy.new(url)

allstreams = video.allstreams
for s in allstreams:
	print(s.mediatype, s.extension, s.quality, s.get_filesize(), s.url)

连接它们的最有效方法是什么,所以python脚本从node.js app获取url并将输出返回给node.js?用Python而不是Meteor.js编写所有代码会更好吗?

What's the most effective way of connecting them so python script get url from node.js app and return back output to node.js? Would it be better to code it all in Python instead of Meteor.js?

推荐答案

嗯,有很多方法可以做这,这取决于您的要求。
有些选项可能是:

Well, there are plenty of ways to do this, it depends on your requirements. Some options could be:


  1. 只需使用stdin / stdout和子进程。在这种情况下,您只需要让Python脚本从stdin读取URL,并将结果输出到stdout,然后从Node执行脚本,也许使用 child_process.spawn 。这是我认为最简单的方法。

  2. 将Python部分作为服务器运行,让我们说HTTP,尽管它可以是任何东西,只要你可以发送请求并获得响应。当您需要来自Node的数据时,您只需向您的Python服务器发送一个HTTP请求,它将返回您在响应中所需的数据。

  1. Just use stdin/stdout and a child process. In this case, you just need to get your Python script to read the URL from stdin, and output the result to stdout, then execute the script from Node, maybe using child_process.spawn. This is I think the simplest way.
  2. Run the Python part as a server, let's say HTTP, though it could be anything as long as you can send a request and get a response. When you need the data from Node, you just send an HTTP request to your Python server which will return you the data you need in the response.

在这两种情况下,您都应该以可以轻松解析的格式返回数据,否则您将不得不编写额外的(和无用的)逻辑来获取数据。使用JSON进行此类操作非常常见且非常简单。
例如,要让程序读取stdin并将JSON写入stdout,可以按以下方式更改脚本( input()适用于Python 3,如果您使用的是Python,请使用 raw_input() 2)

In both cases, you should return the data in a format that can be parsed easily, otherwise you are going to have to write extra (and useless) logic just to get the data back. Using JSON for such things is quite common and very easy. For example, to have your program reading stdin and writing JSON to stdout, you could change your script in the following way (input() is for Python 3, use raw_input() if you are using Python 2)

import pafy
import json

url = input()
video = pafy.new(url)

data = []

allstreams = video.allstreams
for s in allstreams:
    data.append({
        'mediatype': s.mediatype,
        'extension': s.extension,
        'quality': s.quality,
        'filesize': s.get_filesize(),
        'url': s.url
    })

result = json.dumps(data)
print(result)

这是一个非常简短的例子在NodeJS中使用Python脚本

Here is a very short example in NodeJS using the Python script

var spawn = require('child_process').spawn;

var child = spawn('python', ['my_script.py']);

child.stdout.on('data', function (data) {
    var parsedData = JSON.parse(data.toString());
    console.log(parsedData);
});

child.on('close', function (code) {
    if (code !== 0) {
        console.log('an error has occurred');
    }
});

child.stdin.write('https://www.youtube.com/watch?v=AVQpGI6Tq0o');
child.stdin.end();

这篇关于如何使用python脚本连接node.js应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆