weixin_33720452 2012-03-27 10:04 采纳率: 0%
浏览 37

从javascript调用python [重复]

This question already has answers here:
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2012-10-03 14:39:28Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

Possible Duplicate:
Calling Python from JavaScript

I have a test.py and test.js.

I want to be able to run my test.py by opening test.js. I don't know how to create an api because it's not a web app, it's just 2 files sitting on my linux mint desktop.

I don't want to use npapi, because it's just a simple task, i don't want to use pyjamas because it's too hard to install the pyjamas desktop, so how to do it?

please note that i can use php, ajax, jquery instead of javascript, if it could be done with these languages. I am also able to use C++ or C instead of python. I just want to know a simple way to do it.

I know this can be done if i use java instead of python, but i want to know if i can do it with python, C or C++.

</div>
  • 写回答

1条回答

  • weixin_33743661 2012-03-27 13:10
    关注

    If you put your test.py file where ever you put your cgi files, (I think it's /usr/lib/cgi-bin by default for apache on ubuntu linux), you should be able to run the python file just by making a request to it's address.

    If you just want to run the python file, and not use its output in the browser afterward, you could probably get away with something like:

    document.write('<img src="http://localhost/cgi-bin/test.py" />');
    

    if you want to use the output in the browser, you will probably be best served by using jQuery or some other library to do an easy ajax call.

    something like:

    jQuery.get('/cgi-bin/test.py', function(data) {
        //do stuff with the data
    })
    

    would probably do you just fine.

    the second method would require that you also use apache, or equivalent, to serve the test.js file from localhost because jQuery ajax generally requires requests to go to the same domain that the script is running on.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能