从Javascript调用Python脚本,这两个本地文件 [英] Calling a Python script from Javascript, both local files
问题描述
我正在尝试从本地javascript文件(本地运行的HTML / javascript程序的一部分)运行python脚本。
I'm trying to run a python script from a local javascript file (part of a locally running HTML/javascript program).
我一直在谷歌上搜索几个小时,发现了很多解决方案,其中没有一个真正适合我。
I've been googling this for hours and found a lot of solutions, none of which actually work for me.
这是javascript:
Here is the javascript:
$.ajax({
type: "POST",
url: "test.py",
data: { param: " "}
}).done(function( o ) {
alert("OK");
});
test.py文件与脚本/ html文件位于同一文件夹中。
The test.py file is in the same folder as the script/html file.
这是脚本:
#!/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.
在我的Firefox控制台上,我收到格式不正确的错误,并且脚本没有创建文件。但是,我可以看到Firefox确实获取了脚本,因为我可以通过单击文件名在浏览器中查看它。
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.
推荐答案
为了让python脚本执行,它必须由通过CGI或WSGI等支持它的Web服务器部署。
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.
在这里查看文档:< a href =http://docs.python.org/2/howto/webservers.html\"rel =nofollow>网络服务器
这篇关于从Javascript调用Python脚本,这两个本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!