简体   繁体   English

Nodejs服务器,使用Ajax在html客户端中从Python获取JSON数据

[英]Nodejs Server, get JSON data from Python in html client with Ajax

I need to get data (json) in my html page, with help of Ajax. 我需要借助Ajax在html页面中获取数据(json)。 I have a Nodejs server serving requests. 我有一个Nodejs服务器来处理请求。 I have to get the json from server, which is python code to process and produce json as output. 我必须从服务器获取json,这是处理和产生json作为输出的python代码。

  • So should i save json in db and access it? 所以我应该将json保存在数据库中并访问它吗? (seems complicated just for one single use) (似乎仅一次使用就很复杂)
  • Should i run python server, to serve the requests with json as result (call it directy from html via ajax) 我是否应该运行python服务器,以便将结果作为json服务(通过ajax从html直接调用)
  • Should i serve requests with nodejs alone, by calling python method from nodejs? 我是否应该通过从nodejs调用python方法来单独使用nodejs来处理请求? if so how to call the python method. 如果是这样,如何调用python方法。 If calling python requires it to run server ? 如果调用python要求它运行服务器? which one to prefer (zeropc, or some kind of web framework?) 哪个更喜欢(zeropc或某种Web框架?)

Which is the best solution? 哪个是最好的解决方案? or Which is preferred over other in what scenario and what factors? 或在什么情况和什么因素下,哪个比另一个更受青睐?

As I understand: 我认为:

You have a server running 您正在运行服务器

  • an app that acts as a webserver, made with nodejs. 由nodejs制作的充当网络服务器的应用程序。
  • another app in python that does not expose an HTTP api, but you would like to interact with it in some way? python中的另一个未公开HTTP api的应用程序,但是您想以某种方式与之交互?
  • You have control over the source code of both 您可以控制两者的源代码

When running big systems with parts programmed on different languages their are many ways to make it all work together. 当运行带有不同语言编程零件的大型系统时,有很多方法可以使它们全部协同工作。

The choice is yours, and you give us little information to help you decide which way is better. 选择是您的选择,您给我们的信息很少,可以帮助您确定哪种方法更好。 It all depends on how big are your python and nodejs apps, and what they do. 这完全取决于您的python和nodejs应用程序的大小以及它们的功能。

There are no magical way to call a python method from nodejs, or vice versa 从nodejs调用python方法没有神奇的方法,反之亦然

  • Spawn persistent processes, and make them communicate with sockets, or pipes. 生成持久性进程,并使它们与套接字或管道通信。 Generally this is done with enabling libraries (like ZeroMQ + a serialization/RPC format). 通常,这是通过启用库来完成的(如ZeroMQ +序列化/ RPC格式)。

  • Spawn persistent processes and make them communicate with a message queue in between (RabbitMQ, ActiveMQ, ...) 产生持久性进程,并使它们与之间的消息队列通信(RabbitMQ,ActiveMQ等)

  • Spawn your nodejs webserver, and use the "child_process" module to spawn a process in python, and interact with it throught its standard input/output. 生成nodejs Web服务器,并使用“ child_process”模块在python中生成一个进程,并通过其标准输入/输出与其进行交互。 There are helper libraries to do this: https://www.npmjs.com/package/python-shell 有帮助程序库可以执行此操作: https : //www.npmjs.com/package/python-shell

  • Just spawn the nodejs webserver and execute python scripts with "child_process" when you need them (no persistent python process). 只需生成nodejs Web服务器并在需要时使用“ child_process”执行python脚本(无需持久化python进程)。

  • Rewrite it all in python 用python全部重写

  • Rewrite it all in nodejs 在nodejs中全部重写

In your application, if you have some requirement of processing results of python server requests in your nodejs application, then you need to call the python server requests in nodejs app with request libray and then process the result. 在您的应用程序中,如果您需要在Node.js应用程序中处理python服务器请求的结果,则需要在Node.js应用程序中使用request libray调用python服务器请求,然后处理结果。 Otherwise, you should simply call python server resources through client side ajax requests. 否则,您应该只通过客户端Ajax请求调用python服务器资源。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM