Reputation: 1565
To clarify: I am running this as cgi via Apache web server. That isn't the problem My question is regarding a way to specify which function within the Python script to run when I call it via an ajax request.
I read a tutorial that said to pass the function name I want to call as a var. I did that. In the Python, I tried
Here's my ajax function
$(document).ready(function() {
$.ajax({
url: "monStat.py",
type: "post",
data: {'callFunc':'isRunning'},
success: function(response){
$('#blurg').html(response).fadeIn(1500);
}
});
});
Here's the Python
def main():
if callFunc:
funcResp = isRunning()
else:
print("No function passed")
def isRunning( process_name ):
''' the content '''
Upvotes: 3
Views: 6018
Reputation: 92569
Even though you don't mention what kind of web framework you are using (if any), I am going to assume from the naming of your url that you are trying to directly call a python script on your server.
The only way for this to work is if your monStat.py
script is structured as a CGI script, and hosted on your server accordingly (in a way that CGI scripts will be executed). Your javascript implies that you want to make a POST request to this script, so your python script will need to accept a POST request, and then read the parameters and act on them. You cannot just name a callable as a string in javascript and have the CGI script automatically know what to run. This is the place of a web framework to provide advanced URL handling.
If you are trying to just call a regular python script via a url, that is not going to work. The most basic primitive approach is using the python CGI module. This is good for just learning and getting started, but a very inefficient and dated approach. You should probably look into a web framework: Python WebFrameworks
Update
As you stated you are in fact using a CGI script...
"Routing" is something you get for free when you use web frameworks. It takes the incoming request into the main loop and decides where it should go to be handled. When you use only CGI, you will have to do this yourself. Every time you make a request to the CGI script, it executes just like a normal script, with a normal entrypoint.
In that entrypoint, you have to read the request. If the request is POST and contains "is_running" then you can forward that request off to your is_running()
handler. The "function name" is just string data. It is up to your code to read the request and determine what to do.
Here is a super rough example of what it might look like, where you map some acceptable handlers to functions you allow:
#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
def isRunning(form):
print "Content-Type: text/html\n"
print "isRunning()"
print form.getvalue('name')
def _error(*args):
print "Content-Type: text/html\n"
print "Error"
HANDLERS = {
'isRunning': isRunning
}
def main():
form = cgi.FieldStorage()
h_name = form.getvalue('callFunc')
handler = HANDLERS.get(h_name, _error)
handler(form)
if __name__ == "__main__":
main()
Upvotes: 2
Reputation: 1565
This is a start:
import cgi
fs = cgi.FieldStorage()
print "Content-type: text/plain\n"
for key in fs.keys():
print "%s = %s" % (key, fs[key].value)
Upvotes: 0
Reputation: 298196
You'll need to make your script web-capable. I haven't worked with CGI, so here's an example with Flask:
from flask import Flask, request
app = Flask(__name__)
@app.route('/is_running', methods=['POST'])
def isRunning():
process_name = request.values.get('name', None)
''' the content '''
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000)
Now, you can just send a request to /is_running
:
$.ajax({
url: "/is_running",
type: "post",
data: {'name': 'ls'},
success: function(response){
$('#blurg').html(response).fadeIn(1500);
}
});
Upvotes: 4