使用AJAX运行python脚本 [英] run python script using ajax

查看:198
本文介绍了使用AJAX运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有python脚本其中出口从数据库中的数据。该脚本执行exportData功能()

I have the python script which export data from a database. The script executes the exportData function()

最后,我想通过ajax点击一个按钮来运行的网站上。

I would like to run that on the website by clicking a button via ajax.

这是要我想出了,但它似乎并没有运行python脚本。

This is want i have come up with but it does not seem to run the python script.

$(document).ready(function() {

$("#open").click(function(){
 $.ajax({
      url:"export.py", success: function(){alert("DONE");}
 });
  });
});

<button type="button" class="open" id="open" onclick="#" >Button</button>

任何帮助将非常AP preciated。

Any help would be very much appreciated.

推荐答案

如果你已经有了一个现有的网站

您无法直接从网页执行服务器端Python脚本(而不是执行客户端Python脚本的话)。你必须有服务器拦截请求,然后让它执行python脚本。

You can't directly execute server-side python scripts from a web page (and not execute client side python scripts at all). You'll have to have the server intercept the request, and then make it execute the python script.

也就是说,你需要一个管道,看起来像这样:

That is, you need a pipeline that looks like this:

[ client browser ] ----> [ web server ]
[ client browser ]       [ web server ] ----> [ python myscript.py ]
[ client browser ]       [ web server ] <---- [ python myscript.py ]
[ client browser ] <---- [ web server ]

您可以编写自己的Web服务器软件,它会做到这一点,但如果你真的只是有一个python脚本,CGI常被用来让用户运行随心所欲的服务器端脚本,并接受标准输出输出结果。

You can write your own web server software that'll do this, but if you really just have a python script, CGI is often used to let users run "arbitrary" server side scripts, and receive the stdout output as a result.

根据您选择的Web服务器的,有做不同的方式,但这里的基础蟒蛇页面应该有希望给你足够的关键字找到适合你的环境的解决方案。你会发现导游的负荷,如果你谷歌搜索$ myhttpservername蟒蛇CGI。

Depending on your choice of web server, there's different ways of doing it, but here's the base python page that should hopefully give you enough keywords to find a solution that fits your environment. You'll find loads of guides if you search Google for "$myhttpservername python CGI".

https://docs.python.org/2/howto/webservers.html

请参阅下面的指导,就如何建立一个python的cgi-bin在Apache:

Please see the following guide, on how to set up a python cgi-bin on Apache:

<一个href="http://www.linux.com/community/blogs/129-servers/757148-configuring-apache2-to-run-python-scripts" rel="nofollow">http://www.linux.com/community/blogs/129-servers/757148-configuring-apache2-to-run-python-scripts

如果你没有一个Web服务器,只是想提供一个python脚本

有一个不同的方法是在承载Web服务器本身就是一个python脚本。其中一个例子是 CherryPy的。这个例子说明如何在8080端口创建一个web服务器:

A different approach would be to create a python script that hosts the web server itself. One such example is cherrypy. The example shows how to create a web server on port 8080:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!" # or whatever you want to do            
    index.exposed = True

cherrypy.quickstart(HelloWorld())

这篇关于使用AJAX运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆