从 Javascript 调用 Python 脚本,两个本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21915814/
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 23:51:45  来源:igfitidea点击:

Calling a Python script from Javascript, both local files

javascriptpythonajaxhttpscripting

提问by Asgeir

I'm trying to run a python script from a local javascript file (part of a locally running HTML/javascript program).

我正在尝试从本地 javascript 文件(本地运行的 HTML/javascript 程序的一部分)运行 python 脚本。

I've been googling this for hours and found a lot of solutions, none of which actually work for me.

我已经在谷歌上搜索了几个小时并找到了很多解决方案,但没有一个真正适合我。

Here is the javascript:

这是javascript:

$.ajax({

    type: "POST",
    url: "test.py",
    data: { param: " "}
    }).done(function( o ) {
        alert("OK");
});

The test.py file is in the same folder as the script/html file.

test.py 文件与 script/html 文件位于同一文件夹中。

here is the script:

这是脚本:

#!/usr/bin/python
import os
filepath = os.getcwd()
def MakeFile(file_name):
    temp_path = filepath + file_name
    with open(file_name, 'w') as f:
    f.write('''\
def print_success():
    print "sucesss"        
''')
    print 'Execution completed.'

MakeFile("bla.txt");

It works fine when run normally.

正常运行时它工作正常。

On my Firefox console I get a "not well formed" error and the script doesn't create a file. However, I can see that Firefox does fetch the script, as I can view it in my browser by clicking the file name.

在我的 Firefox 控制台上,我收到一个“格式不正确”的错误,并且脚本没有创建文件。但是,我可以看到 Firefox 确实获取了脚本,因为我可以通过单击文件名在浏览器中查看它。

采纳答案by jgitter

In order for the python script to execute it has to be deployed by a web server that supports it via CGI or WSGI, etc.

为了让 python 脚本执行它,它必须由通过 CGI 或 WSGI 等支持它的 Web 服务器部署。

Check out the docs here: webservers

在此处查看文档:网络服务器

回答by Anton

There are three problems with your code.

您的代码存在三个问题。

First, when you call $.ajax(), it tries to parse the response as either JSON or HTML. To prevent it, use dataType: "text".

首先,当您调用 时$.ajax(),它会尝试将响应解析为 JSON 或 HTML。为了防止它,请使用dataType: "text".

$.ajax({
    type: "POST",
    url: "111212.py",
    data: { param: " "}, 
    dataType: "text"
    }).done(function( o ) {
        alert("OK");
});

Second, fetching a local file from javascript may violate the Same Origin Policy, depending on the browser. See: Origin null is not allowed by Access-Control-Allow-Origin

其次,根据浏览器的不同,从 javascript 获取本地文件可能会违反同源策略。请参阅:Access-Control-Allow-Origin 不允许 Origin null

An most important, fetching does not execute a file, it just reads it and returns as a string.

最重要的是,fetching 不执行文件,它只是读取它并作为字符串返回。

回答by Asgeir

So apparently, as has been pointed out, this can't be done, not like this. So I'm going to start a simple CGI python sever to server the HTML file, and execute the script. I've tested it and it works great!

显然,正如已经指出的那样,这是不可能的,不是这样的。所以我将启动一个简单的 CGI python 服务器来处理 HTML 文件,并执行脚本。我已经测试过了,效果很好!