从AJAX或JQuery运行Python脚本

时间:2021-12-23 01:14:34

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输出显示到web页面上。

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

这是Python代码,当我在我的linux box: ./sample中运行这个代码时,它工作得很好。py 12345,输出12345和。/sample。py不会显示找到的参数

#!/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脚本的整个文本。我错过了什么?

2 个解决方案

#1


3  

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调用将是对python脚本的web请求(http),因此您的python脚本需要监听此类请求的端口并进行响应。

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

推荐使用上述方法,以便在示例中使用简单的web服务器,py或使用现有的方法,如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框架,有许多您不需要的铃声)。

#2


-1  

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 vi editor.

同时,意识到有M ^字符在试图执行问题,我解决使用vi编辑器。

#1


3  

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调用将是对python脚本的web请求(http),因此您的python脚本需要监听此类请求的端口并进行响应。

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

推荐使用上述方法,以便在示例中使用简单的web服务器,py或使用现有的方法,如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框架,有许多您不需要的铃声)。

#2


-1  

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 vi editor.

同时,意识到有M ^字符在试图执行问题,我解决使用vi编辑器。