从 AJAX 或 JQuery 运行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25336688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:04:02  来源:igfitidea点击:

Run Python script from AJAX or JQuery

javascriptjquerypythonajaxparameter-passing

提问by ritusmart

We have a requirement to run python scripts from Javascript. We have to pass an input String to the python script as an argument, and display the python output onto our web page.

我们需要从 Javascript 运行 python 脚本。我们必须将输入字符串作为参数传递给 python 脚本,并将 python 输出显示到我们的网页上。

Here is the Python code, this code works fine when I run this in my linux box: ./sample.py 12345, gives the output 12345and ./sample.pywould display no argument found

这是 Python 代码,当我在我的 linux 框中运行此代码时,此代码工作正常:./sample.py 12345,给出输出12345./sample.py显示no argument found

#!/usr/bin/env python

import os
import sys

if len(sys.argv) > 1:
  output = sys.argv[1]
else:
  output = "no argument found"

print "Hello World!!!"
print output

How do I access the 'param' from the ajax call in above python, and use that value as an argument?

如何从上面的 python 中的 ajax 调用访问“param”,并将该值用作参数?

Javascript:

Javascript:

$.ajax({
        type: 'POST',
        url: "scripts/sample.py",
        data: {param: xyz}, //passing some input here
        dataType: "text",
        success: function(response){
           output = response;
           alert(output);
        }
}).done(function(data){
    console.log(data);
    alert(data);
});

Any help would be greatly appreciated.

任何帮助将不胜感激。

EDIT

编辑

As suggested, I am trying to get my code working using CGI.

按照建议,我正在尝试使用 CGI 使我的代码工作。

#!/Python34/python

# Import modules for CGI handling 
import cgi, cgitb

# Create instance of FieldStorage 
data= cgi.FieldStorage()

# Get data from fields
output = data["param"]

# This will print to stdout for testing
print("Hello World!!!")
print(output)

Whenever the Ajax call is run, response is: [object HTMLDivElement], when opened in Firebug displays the entire text of the python script. What am I missing here?

每当运行 Ajax 调用时,响应为:[object HTMLDivElement],在 Firebug 中打开时会显示 Python 脚本的完整文本。我在这里缺少什么?

回答by ritusmart

Getting data from the fields should be done as follows:

从字段中获取数据应按如下方式完成:

# Get data from fields
output = data.getvalue("param")

Also, realized there are ^M characters issues while trying to execute, which I resolved using the vieditor.

另外,意识到在尝试执行时存在 ^M 字符问题,我使用vi编辑器解决了这个问题。

回答by Vlad Gulin

Your Ajax call will be a web request (http)to the python script, so your python script needs to listen on the port for such requests and respond back.

您的 Ajax 调用将是(http)对 Python 脚本的 Web 请求,因此您的 Python 脚本需要在端口上侦听此类请求并做出响应。

Above mentioned method is recommended, to use simple web serverinside your sample ,py or you can use an existing ones such as Flask (or many others).

建议使用上述方法,在您的示例 py 中使用简单的Web 服务器,或者您可以使用现有的服务器,例如 Flask(或许多其他)。

FYI- for your simple task I would not recommend Django (its a big and heavy web framework with lots of bells you do not need).

仅供参考- 对于您的简单任务,我不会推荐 Django(它是一个庞大而沉重的 Web 框架,有很多您不需要的铃铛)。

回答by Jay Ocean

In full transparency I am a contributor to this GitHub repo. I was able to solve this problem using WayScript AJAX triggers:

在完全透明的情况下,我是这个 GitHub 存储库的贡献者。我能够使用 WayScript AJAX 触发器解决这个问题:

See GitHub Repo for instructions: https://github.com/wayscript/ajax-to-python-example

有关说明,请参阅 GitHub Repo:https: //github.com/wayscript/ajax-to-python-example