0

I have some maintenance scripts in python, and was working to create a quick html interface to work with in order to kick the stuff off and handle configuration with a GUI.

I was thinking it would be as simple as ajax to a python file with some data params as follows:

$.ajax({
  type:"POST",
  url: "sample.py",
  data: {json: self.json, filename: self.filename},
  success: function(){ console.log("success"); },
  error: function(){ console.log("error"); }
}).done(function(){ console.log("done"); });

and it found the file, but the response was the content of the python file, not an execution.

For the ease of use, sample.py is just as follows:

#!/usr/bin/python
import os

def main():
    os.system("echo 'test file' > sampleFile.txt")


if __init__ == "__main__":
    main()

the file runs independently, but i am having 2 issues:

1: How do i get the ajax file to make the python run? 2: How do i catch and process the params.

When looking at the file permissions, it says it has full control, so it doesnt seem to be something as easy as chmod +x sample.py

I didnt want to go through all the overhead of creating a Django application just to do this, as it is a single page html file, with an angularjs javascript implementation and bootstrap. Felt creating a whole Django or Flask app would have to much overhead, but i know those frameworks likely hide the key i need to get this all working.

EDIT

Part of me is thinking the following, but maybe my logic is off. 1- Spin up a SimpleHTTPServer 2- Set up a Socket listener on target port. 3- Catch information and execute scripts.

I noticed that when i ran an html file, it was assigning it a port by way of localhost, but obviously there would need to be a different mechanism to launch the html file if i need to spin up a server and set up a file which waits to intercept requests.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

1 Answers1

0

You’re going to have to have something run that python file (options include, but are not limited to, flask, django, wsgi or some other cgi framework [Running python through fastCGI for nginx). You’re webserver is doing what it is supposed to be doing, delivering you the static content of a file. If you want to run that python file on the server via ajax (which can be insecure based on your network setup), you’d have to let your webserver know to call out to wsgi, fastcgi, etc., to process the python file when a particular route is hit.

Community
  • 1
  • 1
2ps
  • 15,099
  • 2
  • 27
  • 47
  • I see. It seems that this isnt something i could manage by way of just running an html file without a server (as the opening of the file opens at localhost:80). I would need a bit more of a framework in place in stead of a html file and a leveraged js file. – Fallenreaper May 11 '17 at 21:39
  • i was thinking maybe do something like this? http://stackoverflow.com/questions/13065306/how-to-execute-python-script-on-the-basehttpserver-created-by-python It would allow me to create and run a server, and then send requests to files in the cgi-bin? – Fallenreaper May 11 '17 at 21:47
  • The frameworks are a bit simpler to use. For example: http://flask.pocoo.org/ - The example under 'Flask is Fun' is enough to get going, just write your own function instead of hello() – Simon Fraser May 11 '17 at 21:56