views:

114

answers:

3

I have a very simple Python file:

f = open('C:\\Temp\\test.txt', 'w')
f.write('Succeeded')
f.close()

which I wish to execute from a JavaScript file, like so:

jQuery.ajax({
   type: "POST",
   url: "/cgi-bin/python1.py",
   success: function (msg) {
       alert("Data Saved: " + msg);
   }
});

However, all that happens is that I get an alert showing me the contents of the Python script. The file C:\Temp\test.txt does not get created, so clearly the Python was not executed.

How do I persuade the code to execute the Python script instead of just reading it?

+1  A: 

Are you able to execute the script directly from the browser. This looks more like a webserver config issue than jquery's

Vinodh Ramasubramanian
No, I can't execute it directly. I'm using Apache 2.2. Any ideas what setting I need to alter?
Charles Anderson
See the answer from @Daniel
Vinodh Ramasubramanian
Okay, I've found the mistake: I used Alias in my httpd.conf file instead of ScriptAlias. I'm now returning the output of the Python file, but it still won't create the temp file. I will leave that to another question
Charles Anderson
+5  A: 

You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.

If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.


EDIT:

Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:

Daniel Vassallo
+1  A: 

If your script is that simple, you would be best off using CGI on the server side rather than mod_python or mod_wsgi as suggested by others. For details on how to set up Apache for CGI with Python and simple script examples see:

http://webpython.codepoint.net/cgi_tutorial

Graham Dumpleton