从javascript调用python [重复]


                            
            

                

                    

                        

                             此问题已在此处提供答案</ b>:
                            
                         </ div>
                     </ div>
                 </ div>
                
已关闭 7年前</ span>。</ div>
             </ div>
         </ aside>


  

可能重复:</ strong>

   从JavaScript调用Python </ p>
</ blockquote>

我有一个 test.py </ code>和 test.js </ code>。</ p>

我希望能够通过打开 test.js </ code>来运行 test.py </ code>。 我不知道如何创建一个api,因为它不是一个Web应用程序,在我的Linux Mint桌面上只有2个文件。</ p>

我不想使用npapi,因为这只是一个简单的任务,我不想使用睡衣,因为安装睡衣桌面太难了,那么该怎么做?</ p>

请注意,如果可以使用这些语言,则可以使用php,ajax,jquery代替javascript。 我也可以使用C ++或C代替python。 我只想知道一种简单的方法。</ p>

我知道如果我使用Java而不是python可以做到这一点,但是我想知道我是否可以使用python,C或C ++做到这一点。</ p>
     </ div>

展开原文

原文

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个回答


如果将test.py文件放在任何放置cgi文件的位置(我认为在ubuntu linux上默认为/ usr / lib / cgi-bin),那么您应该能够只运行python文件 通过向其地址提出要求。 </ p>

如果您只想运行python文件,而不以后再在浏览器中使用其输出,
您可能会逃脱类似的事情:</ p>

  document.write(''');
</ code> </ pre>

如果要在浏览器中使用输出,最好使用jQuery或其他库进行简单的ajax调用。</ p>

类似:</ p>

  jQuery.get('/ cgi-bin / test.py',function(data){
     //处理数据
})
</ code> </ pre>

你可能还好吧。</ p>

第二种方法将要求您还使用apache或等效的方法来从本地主机提供test.js文件,因为jQuery ajax通常要求请求转到脚本运行所在的域。</ p>
     </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐