最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - run python script using ajax - Stack Overflow

programmeradmin1浏览0评论

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

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

This is want i have e 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>

Any help would be very much appreciated.

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

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

This is want i have e 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>

Any help would be very much appreciated.

Share Improve this question edited Aug 26, 2014 at 9:18 Daniel Roseman 600k68 gold badges905 silver badges920 bronze badges asked Aug 26, 2014 at 9:03 user3523549user3523549 331 gold badge1 silver badge4 bronze badges 4
  • What is "the website"? Are you already using a Python framework to serve the site? – Daniel Roseman Commented Aug 26, 2014 at 9:19
  • i am running my own web server using a Raspberry Pi. I want to execute the python script without requiring to refresh the page. As I have tried using a form to execute the python script via php. – user3523549 Commented Aug 26, 2014 at 10:32
  • But you didn't answer the question. What is your web server running? – Daniel Roseman Commented Aug 26, 2014 at 10:34
  • it is running php with Apache – user3523549 Commented Aug 26, 2014 at 10:57
Add a ment  | 

1 Answer 1

Reset to default 7

If you've already got an existing website

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 ]

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.

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/2/howto/webservers.html

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

http://www.linux./munity/blogs/129-servers/757148-configuring-apache2-to-run-python-scripts

If you don't have a web server, and just want to serve a python script

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())
发布评论

评论列表(0)

  1. 暂无评论