16

We have a requirement to run python scripts from Javascript. We have to pass an input String to the python script as an argument, and display the python output onto our web page.

Here is the Python code, this code works fine when I run this in my linux box: ./sample.py 12345, gives the output 12345 and ./sample.py would display no argument found

#!/usr/bin/env python

import os
import sys

if len(sys.argv) > 1:
  output = sys.argv[1]
else:
  output = "no argument found"

print "Hello World!!!"
print output

How do I access the 'param' from the ajax call in above python, and use that value as an argument?

Javascript:

$.ajax({
        type: 'POST',
        url: "scripts/sample.py",
        data: {param: xyz}, //passing some input here
        dataType: "text",
        success: function(response){
           output = response;
           alert(output);
        }
}).done(function(data){
    console.log(data);
    alert(data);
});

Any help would be greatly appreciated.

EDIT

As suggested, I am trying to get my code working using CGI.

#!/Python34/python

# Import modules for CGI handling 
import cgi, cgitb

# Create instance of FieldStorage 
data= cgi.FieldStorage()

# Get data from fields
output = data["param"]

# This will print to stdout for testing
print("Hello World!!!")
print(output)

Whenever the Ajax call is run, response is: [object HTMLDivElement], when opened in Firebug displays the entire text of the python script. What am I missing here?

8
  • 3
    you need your python accept http requests, not console interpritation Commented Aug 16, 2014 at 3:38
  • How do I get the param value from Ajax into Python? Apologize if my question is too 'basic'. Commented Aug 16, 2014 at 3:42
  • You either need to set up a small webserver (using something like Flask or Django) or perhaps use something like cgi to get the post parameters. Commented Aug 16, 2014 at 3:47
  • you should make your server execute python script, read this documentation docs.python.org/2/howto/webservers.html Commented Aug 16, 2014 at 3:47
  • 2
    @snotwaffle I added the answer as you suggested. Thanks. Commented Dec 18, 2014 at 8:51

2 Answers 2

8

Your Ajax call will be a web request (http) to the python script, so your python script needs to listen on the port for such requests and respond back.

Above mentioned method is recommended, to use simple web server inside your sample ,py or you can use an existing ones such as Flask (or many others).

FYI - for your simple task I would not recommend Django (its a big and heavy web framework with lots of bells you do not need).

Sign up to request clarification or add additional context in comments.

Comments

2

Getting data from the fields should be done as follows:

# Get data from fields
output = data.getvalue("param")

Also, realized there are ^M characters issues while trying to execute, which I resolved using the vi editor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.