1

I try to run a simple "helloWorld" python-snippet on the new MAMP 3.5, because may old xampp got problems with the update to mac osx el capitan.

The MAMP-Documentation says that mod_wsgi, actual python and mod_python is embeded. However if i place my test.py in the htdocs and try to contact it via a JavaScript-function (ajax.POST) it seems the script is found but as result i only receive the text of my test.py in the firebug-console back. Somehow it seems the python script is not executed.

headline in test.py is #!/bin/usr/python, i also tried to place the test.py in the MAMP/cgi-bin-directory or to use other existing python-paths (MAMP/"which python") Does anyone have an idea? Below are my two snippets:

    <script type="text/javascript">
        $(document).ready(function(){

            $("button").click(function(){

                var myValue = $("input:text").val();
                console.log(myValue);

                $.ajax({
                     type: "POST",
                     url: "test.py",
                     data: { myPy: myValue},

                     success: function(data) {   
                        console.log("This is my result: ", data);             
                    }        
                });
            });
       });
    </script>

This is test.py:

    #!/bin/usr/python

    def myjsfunction (myPy):
         return (myPy)
Levon
  • 138,105
  • 33
  • 200
  • 191
stew
  • 13
  • 5

1 Answers1

1

I had the exact same problem and I have managed to solve by doing the following:

Disclosure:

  • Currently running MAMP 3.5
  • Python 2.7

Steps:

  1. Add the following line in the httpd.conf file (MAMP/conf/Apache/httpd.conf) within the tag as shown below:

Line to be added:

Options FollowSymLinks +ExecCGI

Section to be added in:

    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride None
        Options FollowSymLinks +ExecCGI
    </Directory>
  1. Create a .cgi python file as follows (this file should have the .cgi extension btw, and you should call it as such from your ajax call):

     #!/usr/bin/python
     print 'Content-type: text/html\n\n'
     import cgi
     response = cgi.FieldStorage()
     print response["myPy"].value
    

That should give you the output you wanted. If you don't want to use raw CGI, check out this post for other options for the python script (i.e. django).

How are POST and GET variables handled in Python?

Hope this helps.

Community
  • 1
  • 1
atb
  • 11
  • 1