简体   繁体   English

从Javascript调用Python脚本

[英]Call Python Script from Javascript

I am on a dreamhost server and have some HTML that calls some javascript when a button is pressed. 我在dreamhost服务器上,并且有一些按下按钮时会调用一些javascript的HTML。 I am trying to call a python script when this button is tapped. 点击此按钮时,我尝试调用python脚本。

First off, to my knowledge as I am on a shared host I cannot use AJAX as it is not supported, so I need to do this without AJAX. 首先,据我所知,由于我在共享主机上,因此无法使用AJAX,因为它不受支持,因此我需要在没有AJAX的情况下进行此操作。 Right now I am trying to do a XMLHttpRequest which is working. 现在,我正在尝试执行一个XMLHttpRequest

I also realize doing an XMLHttpRequest is not the best way since the files are both on the server there must be a way to just call the file directly? 我也意识到做XMLHttpRequest并不是最好的方法,因为文件都在服务器上,必须有一种直接调用文件的方法吗?

So if someone call tell me how to call it directly or help me fix this error in the browser console that would be great. 因此,如果有人打电话告诉我如何直接调用它,或者帮助我在浏览器控制台中解决此错误,那就太好了。 Thanks for the help 谢谢您的帮助

EDIT 编辑

I have an HTML file when a user taps a button on this file it calls some javascript that is in the HTML file. 当用户点击此文件上的按钮时,我有一个HTML文件,它会调用HTML文件中的一些javascript。 This javascript currently makes a POST Request, to a python script that is on the same server and the HTML file. 目前,此javascript向同一服务器和HTML文件上的python脚本发出POST请求。

What I want instead of making a post request to the python file that is on the server, I want to just directly call the python file, from the javascript that runs, when the button the clicked in the HTML file. 我想要的不是向服务器上的python文件发出请求,而是要从运行的javascript中直接调用python文件,只需在HTML文件中单击按钮即可。

Both the HTML file which contains the javascript and the python file are on the same server. 包含javascript和python文件的HTML文件都在同一服务器上。 And I don't want the python to run in the browser, I want it to run in the background on the server. 而且我不希望python在浏览器中运行,我希望它在服务器的后台运行。

How can I use the Javascript to call this python file? 如何使用Javascript调用此python文件?

As far as I understand your question what you are looking to do is called a "remote procedure call", or some sort of Service Oriented Architecture (SOA). 据我了解您的问题,您要执行的操作称为“远程过程调用”,或某种面向服务的体系结构(SOA)。

You are on a right track in making a POST request to the server. 向服务器发出POST请求时,您处在正确的轨道上。

You can setup a middleware like flask, or cherrypy to run the script when you send a GET, PUT, POST ... request. 您可以设置诸如flask或cherrypy之类的中间件来在发送GET,PUT,POST ...请求时运行脚本。 And inside of the middleware controller you can call your script. 在中间件控制器内部,您可以调用脚本。

Basically you have started to create a RESTful api, and its a pretty standard way these days to run logic on the backend. 基本上,您已经开始创建RESTful api,这是如今在后端运行逻辑的一种非常标准的方法。

Some examples of different frameworks for doing url routing: 进行网址路由的不同框架的一些示例:

Python: 蟒蛇:

NodeJs: NodeJ:

Also very good is this question: JSON, REST, SOAP, WSDL, and SOA: How do they all link together 这个问题也很好: JSON,REST,SOAP,WSDL和SOA:它们如何链接在一起

Another way that you could do this from the browser would be to use sockets, which opens a connection between the client and the server. 您可以通过浏览器执行此操作的另一种方式是使用套接字,该套接字将打开客户端与服务器之间的连接。

Inside the javscript you could use socketio: 在javscript中,您可以使用socketio:

<script src='/socket.io/socket.io.js'></script>
<script>
    var socket = io();
    socket.connect('http://localhost:8000')
    socket.emit('run_a_script_event', {arg1: 'hello', arg2: 'world'});
</script>

And in your python code you could use the socketio client for python ( https://pypi.python.org/pypi/socketIO-client ): 在您的python代码中,您可以将Socketio客户端用于python( https://pypi.python.org/pypi/socketIO-client ):

from your_module import thescript
from socketIO_client import SocketIO, LoggingNamespace

def run_a_script(*args):
    print('arg1 and arg2', args)
    thescript()

socketIO = SocketIO('localhost', 8000, LoggingNamespace)
socketIO.on('run_a_script_event', run_a_script)

Looks like there is also a version specifically for flask: https://flask-socketio.readthedocs.org/en/latest/ 似乎还有一个专门用于烧瓶的版本: https : //flask-socketio.readthedocs.org/en/latest/

Or you could run the python directly in the browser by converting it to javascript with a transpiler: 或者,您也可以使用翻译器将python转换为javascript,从而直接在浏览器中运行python:

Or you could use node javascript to spawn a child process which runs the python script: 或者,您可以使用节点javascript生成运行python脚本的子进程:

Or you can import the python script into node like: 或者您可以将python脚本导入节点,例如:

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

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