简体   繁体   English

在Mac OS X 10.11上的MAMP 3.5上运行python脚本

[英]running python-scripts on MAMP 3.5 on mac osx 10.11

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. 我尝试在新的MAMP 3.5上运行一个简单的“ helloWorld” python代码段,因为可能旧的xampp在更新mac osx el capitan时遇到了问题。

The MAMP-Documentation says that mod_wsgi, actual python and mod_python is embeded. MAMP文档说嵌入了mod_wsgi,实际的python和mod_python。 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. 但是,如果我将我的test.py放在htdocs中,并尝试通过JavaScript函数(ajax.POST)与它联系,则似乎找到了该脚本,但结果我只在firebug控制台中收到了test.py的文本。背部。 Somehow it seems the python script is not executed. 似乎python脚本未执行。

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? test.py中的标题是#!/bin/usr/python ,我也试图将test.py放在MAMP / cgi-bin-directory或使用其他现有的python-paths(MAMP /“ that python”)做有人有主意吗? 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: 这是test.py:

    #!/bin/usr/python

    def myjsfunction (myPy):
         return (myPy)

I had the exact same problem and I have managed to solve by doing the following: 我遇到了完全相同的问题,并且通过执行以下操作设法解决了:

Disclosure: 披露:

  • Currently running MAMP 3.5 当前正在运行MAMP 3.5
  • Python 2.7 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: 在标记内的httpd.conf文件(MAMP / conf / Apache / httpd.conf)中添加以下行,如下所示:

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): 如下创建一个.cgi python文件(该文件的扩展名为.cgi btw,您应该从ajax调用中这样调用它):

      #!/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 (ie django). 如果您不想使用原始的CGI,请查看此文章以获取python脚本的其他选项(例如django)。

How are POST and GET variables handled in Python? 如何用Python处理POST和GET变量?

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM