1

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.

Here is the javascript:

$.ajax({

    type: "POST",
    url: "test.py",
    data: { param: " "}
    }).done(function( o ) {
        alert("OK");
});

The test.py file is in the same folder as the script/html file.

here is the script:

#!/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.

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.

Asgeir
  • 727
  • 3
  • 9
  • 20
  • Did you properly indent the f.write() call after the with open()... line in your actual file? Have you tested the python script independent from the web? – jgitter Feb 20 '14 at 18:13
  • Yup, I've tested it simply by double clicking the script file. Works fine and creates the bla.txt file. – Asgeir Feb 20 '14 at 18:17
  • Are you running a local HTTP server, or just opening a local HTML file in a browser? – Anton Feb 20 '14 at 18:40
  • I'm opening a local HTML file in a browser. – Asgeir Feb 20 '14 at 18:41
  • 1
    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. Check out the docs here: http://docs.python.org/2/howto/webservers.html – jgitter Feb 20 '14 at 18:52
  • Damn, this is a very small project and having a server is not really an option. Any other way to do this? – Asgeir Feb 20 '14 at 19:09
  • I started a simple server with CGI and it works. Hopefully it will be acceptable to launch a lightweight localhost to help with the task. – Asgeir Feb 20 '14 at 19:22

3 Answers3

5

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.

Check out the docs here: webservers

Trix
  • 587
  • 1
  • 6
  • 27
jgitter
  • 3,396
  • 1
  • 19
  • 26
1

There are three problems with your code.

First, when you call $.ajax(), it tries to parse the response as either JSON or HTML. To prevent it, use dataType: "text".

$.ajax({
    type: "POST",
    url: "111212.py",
    data: { param: " "}, 
    dataType: "text"
    }).done(function( o ) {
        alert("OK");
});

Second, fetching a local file from javascript may violate the Same Origin Policy, depending on the browser. See: Origin null is not allowed by Access-Control-Allow-Origin

An most important, fetching does not execute a file, it just reads it and returns as a string.

Community
  • 1
  • 1
Anton
  • 3,113
  • 14
  • 12
1

So apparently, as has been pointed out, this can't be done, not like this. So I'm going to start a simple CGI python sever to server the HTML file, and execute the script. I've tested it and it works great!

Asgeir
  • 727
  • 3
  • 9
  • 20