gpt4 book ai didi

python - 在 Meteor 中调用 Python 脚本

转载 作者:IT老高 更新时间:2023-10-28 13:33:52 26 4
gpt4 key购买 nike

让我的 meteor 应用程序调用与 meteor 服务器端代码位于同一台机器上的 python 脚本的最佳方法是什么?我想做的就是让meteor 将一个字符串传递给python 中的一个函数,然后让python 将一个字符串返回给meteor。

我在想我可以让 python 监控 mongodb 并提取值并在计算后将它们写回 mongodb,但是让 meteor 直接在 python 中调用函数似乎更干净。

我是 DDP 的新手,无法使用 python-meteor (https://github.com/hharnisc/python-meteor) 走得很远。

ZeroRPC (http://zerorpc.dotcloud.com/) 是一个好方法吗?

谢谢。

最佳答案

好问题。

我研究过使用 DDP 和 ZeroRPC,甚至让 Python 直接写入 Mongo。

对我来说,让 Meteor 和 Python 对话的最简单方法是将 Python 脚本设置为 flask 应用程序,然后将 API 添加到 flask 应用程序并让 Meteor 通过 API 与 Python 对话。

为了让这个设置正常工作,我使用了:

要对其进行测试,您可以构建类似这样的基本内容(python 脚本将文本转换为大写):

from flask import Flask
from flask.ext import restful

app = Flask(__name__)
api = restful.Api(app)

class ParseText(restful.Resource):
def get(self, text):
output = text.upper()
return output

api.add_resource(ParseText, '/<string:text>')

if __name__ == '__main__':
app.run(debug=True) # debug=True is for testing to see if calls are working.

然后在 Meteor 中使用 HTTP.get 来测试调用 API。

如果您在本地运行所有内容,那么来自 Meteor 的调用可能类似于:Meteor.http.get("http://127.0.0.1:5000/test");

关于python - 在 Meteor 中调用 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27759519/

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